Stack Implementation Using Single Linked List
Posted by Vinod on May 25, 2006
/*
Author:B.Vinod Kumar
email: vinod_cse2008@yahoo.com
This is a program for implementation of the stack using single linked list.
The operations performed on a stack are
1)push(): This is the function which is for insertion(pushing)of an element into stack
It is similar to the insertion of an element at the end of a single linked list
see the function insert_end() in the program for operations of single linked list
2)pop(): This is the function which is for deletion(popping up) of an element from the stack
It is similar to the deletion of an element at the end of a single linked list
see the function delete_end() in the program for operations of single linked list
3)stack_display():This is the function which is for displaying the elements of a stack
It is similar to the forward traversal of a single linked list
see the function ftraverse() in the program for operations of single linked list
note:
Stack is a list of elements in which insertions and deletions are at the same end of the list called top.
The other end is known as Bottom.Insertion is also known as push,deletion is also known as pop
*/
#include<iostream.h>
#include<stdlib.h>
class stack
{
int element;
stack* next;
public:
stack* push(stack*,int);
stack* pop(stack*);
void stack_display(stack*);
}*head,object;
stack* stack::push(stack* head,int key)
{
stack* temp,*temp1;
temp1=head;
temp=new stack;
temp->element=key;
temp->next=NULL;
if(head==NULL)
head=temp;
else
{
while(head->next!=NULL)
head=head->next;
head->next=temp;
head=temp1;
}
return head;
}
stack* stack::pop(stack* head)
{
stack* temp;
if(head!=NULL)
{
temp=head;
if(head->next==NULL)
{
cout<<"\nthe pooped element from the stack is: "<<head->element<<endl;
return NULL;
}
while(head->next->next!=NULL)
head=head->next;
cout<<"the popped element from the stack is "<<head->next->element;
cout<<endl;
head->next=head->next->next;
head=temp;
return head;
}
else
{
cout<<"\nit is impossible to pop an element from the stack as ";
return head;
}
}
void stack::stack_display(stack* head)
{
if(head!=NULL)
{
while(head->next!=NULL)
{
cout<<head->element<<"->";
head=head->next;
}
cout<<head->element;
cout<<endl;
}
else
cout<<"the stack is empty\n";
}
void choice()
{
int key,ch;
cout<<"\nchoose the operation to be performed\n";
cout<<"\n1.push\t2.pop\t3.exit\n\n";
cin>>ch;
head=NULL;
while(1)
{
switch(ch)
{
case 1:
cout<<"——————————————————————————–\n";
cout<<"\nenter the element to be pushed\n";
cin>>key;
head=object.push(head,key);
cout<<"\nthe stack after push operation is \n";
object.stack_display(head);
cout<<"——————————————————————————–\n";
break;
case 2:
cout<<"\n“““““““““““““““““““““““““““““““““““““““\n";
head=object.pop(head);
cout<<"\nthe stack after pop operation is :\n";
object.stack_display(head);
cout<<"\n“““““““““““““““““““““““““““““““““““““““\n";
break;
case 3:
exit(1);
default:
cout<<"\nenter the correct choice\n";
break;
}
cout<<"\nchoose the operation to be performed\n";
cout<<"\n1.push\t2.pop\t3.exit\n";
cin>>ch;
}
}
void main()
{
choice();
}



anirban said
found it very useful.thanks a lot.will be obliged if u can send me a program on stack using linked list in c language.i cant do the pop operation.
thanks.
regards.
takkarivicky said
thank u very much………[:)]
jbragadeesh said
I have done an implementation with illustrative pictures.
http://www.technicalypto.com/2010/02/stack-using-linked-lists-in-java.html
accommodation Wellington said
accommodation Wellington…
[...]Stack Implementation Using Single Linked List « data structures for beginners[...]…