Give linked list implementation of stack operation.
Linked List Implementation of Stacks: the PUSH operation It’s very similar to the insertion operation in a dynamic singly linked list. The only difference is that here you'll add the new element only at the end of the list, which means addition can happen only from the TOP. Since a dynamic list is used for the stack, the Stack is also dynamic, means it has no prior upper limit set. So, we don't have to check for the Overflow condition at all!
In Step  we create the new element to be pushed to the Stack.
In Step  the TOP most element is made to point to our newly created element.
In Step  the TOP is moved and made to point to the last element in the stack, which is our newly added element.
Linked List Implementation of Stacks: the POP Operation
This is again very similar to the deletion operation in any Linked List, but you can only delete from the end of the list and only one at a time; and that makes it a stack. Here, we'll have a list pointer, "target", which will be pointing to the last but one element in the List (stack). Every time we POP, the TOP most element will be deleted and "target" will be made as the TOP most element.
In step we got the "target" pointing to the last but one node. In step we freed the TOP most element.
In step we made the "target" node as our TOP most element.
Supposing you have only one element left in the Stack, then we won't make use of "target" rather we'll take help of our "bottom" pointer. See how...
· Step-1: If the Stack is empty then give an alert message "Stack Underflow" and quit; or else proceed
· Step-2: If there is only one element left go to step-3 or else step-4
· Step-3: Free that element and make the "stack", "top" and "bottom" pointers point to NULL and quit
· Step-4: Make "target" point to just one element before the TOP; free the TOP most element;
make "target" as your TOP most element
struct node *next;
struct node *stack = NULL; /*stack is initially empty*/
struct node *top = stack;
int newvalue, delval;
delval = pop(); /*POP returns the deleted value from the stack*/
int pop( )
int pop_val = 0;
struct node *target = stack; if(stack == NULL) /*step-1*/ cout<<"Stack Underflow"; else
if(top == bottom) /*step-2*/
pop_val = top -> nodeval; /*step-3*/ delete top;
stack = NULL;
top = bottom = stack;
while(target->next != top) target = target ->next; pop_val = top->nodeval;
delete top; top = target;
target ->next = NULL;