Stack with find-min/find-max in O(1) time

Problem Stack with find-min/find-max in O(1) time Solution Method 1 - using extra stacks for min and max The basic idea of the solution can be found here. Here instead of 2 stacks we will be using 3 stacks -1 stack with normal elements, 1 with only max elements and 1 with min element. Now the operations will be following : push: - If the element being pushed is less than the top element of ‘min_stack’ then push it on ‘min_stack’ as well. - Else, push the top element of ‘min_stack’ again on ‘min_stack’. Similarly for max_stacks ...

Circular Stack

There is no diffeence in circular list stack and non circular list stack. The insertion, deletion operation takes place in the same place. So it won’t create any modification in the circular list. Here is the implementation of circular stack in CPP Program in cpp #include <iostream.h> #include <stdlib.h> using namespace std; struct node { int info; struct node \*next; }; int c=0; struct node \*top;int empty() { return((top == NULL)? 1:0); } void print() { int i =0; struct node \*temp; cout<<"\\n\\t\\t"; if(c==0) cout<<"\\n\\t\\tNo elements\\n"; else { temp = top->next; while(i!=c) { int info = temp->info; cout<<info<<" "; temp=temp->next; i++; } cout<<"END PRINT"; } } void push(int n) { struct node \*p; p=new node; if(p!=NULL) { c++; p->info=n; if(empty()) top=p; else p->next=top->next; top->next=p; } else cout<<"Not inserted,No memory available"; } int pop() { c--; struct node \*temp; int x; temp=top->next; x=temp->info; if(temp==top) top=NULL; else top->next=temp->next; free(temp); return(x); } int main() { int s,n; cout<<"\\n\\t\\tCIRCULAR STACK IMPLEMENTATION\\n"; while(1) { cout<<"\\n\\t\\t1>Push\\n"; cout<<"\\t\\t2>Pop\\n"; cout<<"\\t\\t3>Print\\n"; cout<<"\\t\\t4>Exit\\n"; cout<<"\\t\\tEnter your choice:"; cin>>s; switch(s) { case 1: cout<<"\\n\\t\\tEnter the number:"; cin>>n; push(n); break; case 2: if(empty()) { cout<<"\\n\\t\\tStack underflow\\n"; } else { n=pop(); cout<<"\\n\\t\\tThe pop value is:"<<n; } break; case 3: print(); break; case 4: exit(1); break; } } } ```Let us know your feedback. ...

Stack DS to implement constant time Minimum element lookup

Problem: You need to implement a stack data structure that can also report the minimum element in constant-time. Solution Method 1 - Using extra stack This is not all that difficult if you think about it. Regular stacks support push() and pop() functions. We need to add a new read-only property, Minimum. You can’t just add a new variable to track the minimum because when the stack is popped you wouldn’t be know how to update the minimum variable without scanning through all items in the stack which would require you to pop and re-push each item, which is downright ugly. So we need to keep some auxiliary list of the stack items so we can update the minimum variable after we pop items. ...

Array implementation of stack

Here will discuss the array implementation of stack. Problems with array implementation Underflow - Array may be empty but people may try to pop the element Overflow - Array is full. To over come we have to use re-szing of array. We can see how we can solve this problem here - http://k2code.blogspot.com/2013/09/resizing-array-implementation-of-stack.html Null items - Can nulls be added - Yes in this case, nulls can be added in stack Loitering (java specific) - Holding a reference to an object wen it is no longer needed. To over come this we should explicitly set array index to null. For eg. ...

Array implementation of stack

Here will discuss the array implementation of stack. Problems with array implementation Underflow - Array may be empty but people may try to pop the element Overflow - Array is full. To over come we have to use re-szing of array. We can see how we can solve this problem here - http://k2code.blogspot.com/2013/09/resizing-array-implementation-of-stack.html Null items - Can nulls be added - Yes in this case, nulls can be added in stack Loitering (java specific) - Holding a reference to an object wen it is no longer needed. To over come this we should explicitly set array index to null. For eg. ...

Stack implementation using linked list

We will be understanding the stack implementation using linked list. So, please understand the link list before proceeding. Lets understand how we can implement the different operation using linked list. CPP implementation Here is how we use linked list to implement stack in cpp: #include <iostream> using namespace std; struct node { int info; struct node \*next; }; struct node \*top; int empty() { return((top == NULL)? 1:0); } void push(int n) { struct node \*p; p=new node; if(p!=NULL) { p->info=n; p->next=top; top=p; } else cout<<"Not inserted,No memory available"; } int pop() { struct node \*temp; int x; x=top->info; temp=top; top=top->next; free(temp); return(x); } void print() { int i =0; struct node \* temp; temp = top; cout<<"\\n\\t\\t"; if(temp==NULL) cout<<"\\n\\t\\tNo elements\\n"; else { while(temp!=NULL) { int info = temp->info; cout<info<<" "; temp=temp->next; } cout<<"End of List<<"\\n"; } } ...

Stack ADT

Stacks are an elementary Data Structure where all interaction with the data is done through the looking at the first element. Stacks are last in first out (LIFO) data structures. It is named on the basis of how the elements are removed from it. Opposite to stack is queue, in which the last element is removed, the element which was most old item, and hence it is FIFO or first in first out data structure. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy