STACK IMPLEMENTATION USING LINKED LIST
Posted by Vinod on December 17, 2006
/*
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();
}
letormente said
how to push multiple data such that
stack->push(“may”, 15);
and how to pop such multiple data onto a stack
Source: Data Structures Demystified by Keogh and Davidson
farhan said
tank you i need this for my 2morrow’s quiz.
Ainee khan said
hello farhan,
how,s the experience about programming.
its difficult or not?
Ali Bacha said
boring :p
Pavithra said
Hi ,
This web site is gr8 just said for a DS beginner..
Rucha said
hai,
ur website is gr8
It is a good source for engineering students.
Thanks
sbabamca said
gr8 vinod
saba said
aslam o alikum
hay howz r u
kiya ap ko maira rply a raha hai
kiya ap data structure main mairi help akr sakty hain
saman said
thanks it will b very helpful for my assignment
Celia said
Thank you so much! Keep up the good work!
kaviraj said
ya..u r 100% rite frndzzz…………tis site is very much useful for my seminar………………………………frndzzzz…..if u have any details regarding data structure.pls mail me kavi.skr.raj@gmail.com
i m in need…..pls………..
aarti said
thnx a lot.
hadel ali said
tank you
Adeeba said
not understandable really difficult for the beigners. use with structure instead of class.
tabassum said
it’s so easy to understand
solomon said
ineed help to get the c++ program
sunil said
I found an easy-to-understand implementation of Stack in C++ with visuals at the below link
http://www.rawkam.com/?p=719
hewan said
wow i like your website.
veerkumar said
THE PROGRAM IS DONE LIKE A BEGINNER. U SHOULD PRACTICE PROFESSIONAL PROGRAMMING STYLE.
Murari kumar said
Hi ! This websit is helpful but not too much for beginner be cause not use of com ment AND dnt to give the basic idea about linkelist,pointer,stack,etc.
Rajendra said
@:Vinod (Owner of Post):
Problem:
A stack’s primitive operations (push and pop) are O(1) by definition. Your program violates this by inserting at the end every-time you push an element and also when popping, you are traversing whole list to push an element and to pop also it’s same. So, basically your program makes the stack’s push, pop complexity to be O(n) in worst case.
Solution: Instead of pushing at end, push at beginning, this operation will be O(1), maintain top-of-stack pointer also, which will be head. When you pop, you pop it from the top-of-stack pointer and this operation will also be O(1). This approach keeps pace with the stack’s primitive operation’s complexity definition.
@Letormente: You can have one more data element in the stack class, say “string month;”, then your required push (and corresponding pop) operation can be handled. When popping, you return pointer to node structure.
512407 said
its Best way 2 stdy abt data structure
Callen said
this helps a lot!
sai said
what are advantages and disadvantages in this implementation
angel! said
hey i need to know this!
1) user enters a mathematical expression with parenthesis ( all 3 types (),{},[]) 2) The program checks if the parenthesis are correct in the mathematical expression
can anyone help me with it ?
Dhina Dhayalan.R said
Dude,Thnk u 4 tis gr8 website,it helped me throughout my 3rd semester(DS and OOP’s lab).
telecom news said
telecom news…
[...]STACK IMPLEMENTATION USING LINKED LIST « Data Structures through C & C++ for beginners[...]…
luky boy said
where is the out put of above progrme plz upload the output of these programes
Casio Privia PX-130 vs PX-330 said
Casio Privia PX-130 vs PX-330…
[...]STACK IMPLEMENTATION USING LINKED LIST « Data Structures through C & C++ for beginners[...]…
Abdulraheem sawand said
Written go0d about stack array and there is also written codes i enjoyed it……
disabled dating website said
Thanks to my father who shared with me on
the topic of this website, this weblog is actually awesome.