GeeksforGeeks » Linked List specific questions
Stack with max(), push() and pop() operations in O(1) time
(8 posts)-
Implement a Stack such that push(), pop() and max() can be done in O(1) time. Stack should be implemented using linked list and max() should return the node(not the value) containing max value
-
See http://geeksforgeeks.org/?p=5009 for stack implementation using linked list with O(1) push and pop() operations.
I am not sure if it is possible to do max() also in O(1) time
-
ya...max() can be done in O(1)...as follows..we are performing push() operation in O(1) time...so if we use another maxstack...and if we compare every time when we are introducing another push() operation the element we are introducing with the previous elements in max_stack...and with exchanging if necessary it can be achieved in constant time...i thnk this can be the solution...if its wrong just lemme know....
thank you...
-
can not we use doubly link list and just keeping track of head and tail? for insertion we use add to head and for deletion from tail and update tail correspondingly?
I think we dont need two link lists in case if we are not restricted to use any link list. -
Augment the link node with a field "maxsofar". When a node is inserted in the stack, make its "maxsofar" field points to the current maximum node of the stack. Using previous node's maxsofar, new maxsofar can be computed in O(1) time.
-
Every operation asked above can be done in O(1).
To do the max operation, just have a max_pointer and reset it while push operation into stack. Like
max_pointer -> first_vale(first push)
if(max_pointer->value < second_value)
max_pointer -> second_value. (Second Push)....and so on.
Like that you will always have the Max_value pointer. ;-)Thanks
itengineer -
I have a doubt. What if the max is removed after the pop() . Then we have to traverse the entire list to get the next max.
-
I have implemented this stack with min operation. You can change the "push" method for implementing max.
#include<iostream> using namespace std; struct Node{ int _element; int _currentMinElement; Node *next; Node() { _element = 0; _currentMinElement = 0; } }; class MyStack{ Node *topNode; int _noOfElements; public: void push(int elem); int pop(); bool getCurrentMinElement(int &elem); bool top(int &elem); MyStack(){ topNode = NULL; _noOfElements = 0; } ~MyStack(){ int cnt = 0; while(topNode) { pop(); } } }; void MyStack::push(int elem) { Node *node = new Node(); node->_element = elem; int x = 0; node->_currentMinElement = elem; if(getCurrentMinElement(x)) { if(x < elem) { node->_currentMinElement = x; } } node->next = topNode; topNode = node; _noOfElements++; } int MyStack::pop() { if(!topNode && !_noOfElements) return -1; Node *deleteNode = topNode; int data = deleteNode->_element; topNode = topNode->next; delete deleteNode; _noOfElements--; return data; } bool MyStack::top(int &elem) { if(!topNode && !_noOfElements) return false; elem = topNode->_element; return true; } bool MyStack::getCurrentMinElement(int &elem) { if(!topNode && !_noOfElements) return false; elem = topNode->_currentMinElement; return true; }
Reply
You must log in to post.