Custom stack-pop-push-min-time complexity is O (1)

Source: Internet
Author: User

1. Brief Introduction

First, we need to decide whether to implement the chain table structure or the ordered structure. For the implementation of the ordered structure, when the data is full, we need to open up new Arrays for replication, therefore, the time complexity of push cannot be O (1). the linked list structure ensures that the time complexity of push and pop is O (1 ). The time complexity of Min also requires O (1). the linked list is recorded by changing the space for time. When a node is included, the node with the minimum value is located. When there are two nodes, the node where the minimum value is located, and so on until all nodes are located. This is equivalent to two linked lists. One linked list stores values, and the other stores the minimum value of the corresponding node range in the first linked list.

2. Implementation

# Include <iostream>
Using namespace STD;

// Youyuan, Link Structure
Template <class T>
Class stack;

Template <class T>
Class node {
Friend class Stack <t>;
PRIVATE:
T data;
Node <t> * link;
};

Template <class T>
Class minnode {
Friend class Stack <t>;
PRIVATE:
Node <t> * min_node;
Minnode <t> * link;
};

Template <class T>
Class Stack {
PRIVATE:
Node <t> * head;
Minnode <t> * min_head;
Public:
Stack (): Head (0), min_head (0 ){}
~ Stack (){
Node <t> * TMP;
While (head ){
TMP = head-> link;
Delete head;
Head = TMP;
}
TMP = 0;
Minnode <t> * min_tmp;
While (min_head ){
Min_tmp = min_head-> link;
Delete min_head;
Min_head = min_tmp;
}
Min_tmp = 0;
}
Stack <t> & push (const T & Data ){
Node <t> * TMP = new node <t>;
TMP-> DATA = data;
TMP-> link = head;
Head = TMP;
MinNode <T> * min_tmp = new MinNode <T>;
If (min_head> 0 & min_head-> min_node-> data Min_tmp-> min_node = min_head-> min_node;
Else
Min_tmp-> min_node = head;
Min_tmp-> link = min_head;
Min_head = min_tmp;
Return * this;
}
Stack <T> & Pop (){
Assert (head> 0 );
Node <T> * tmp = head-> link; delete head; head = tmp; tmp = 0;
MinNode <T> * min_tmp = min_head-> link; delete min_head; min_head = min_tmp; min_tmp = 0;
Return * this;
}
Void Min (T & data ){
Assert (head> 0 );
Data = min_head-> min_node-> data;
}
};

Int main (){
Stack <int> stack;
Int min;
Int array [10] = };
For (int I = 0; I <10; I ++ ){
Stack. Push (array [I]);
Stack. Min (min );
Cout <min <endl;
}
System ("PAUSE ");
Return 0;

}

3. Description

· For Node classes such as Node and MinNode, the Stack is declared as a friend to facilitate the direct use of private members of the Node class.
· The type of Push and Pop return values is Stack <int> &. This is used to implement stack. push (1). push (2). push (3); so that the call method is used.
· In the references, deque is used to implement the internal structure.

4. Reference

Programmer interview questions 100 (02)-design a stack containing min Functions
Http://zhedahht.blog.163.com/blog/static/25411174200712895228171/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.