Data Structures through C & C++ for beginners

If the code doesn't work, please replace the single quotes and double quotes(Actually these are not proper single and double quotes) in the code with single quotes and double quotes using your keyboard..

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();

}

About these ads

31 Responses to “STACK IMPLEMENTATION USING LINKED LIST”

  1. 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

  2. farhan said

    tank you i need this for my 2morrow’s quiz.

  3. Pavithra said

    Hi ,
    This web site is gr8 just said for a DS beginner..

  4. Rucha said

    hai,
    ur website is gr8
    It is a good source for engineering students.
    Thanks

  5. sbabamca said

    gr8 vinod

  6. saman said

    thanks it will b very helpful for my assignment

  7. Celia said

    Thank you so much! Keep up the good work!

  8. 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………..

  9. aarti said

    thnx a lot.

  10. hadel ali said

    tank you

  11. Adeeba said

    not understandable really difficult for the beigners. use with structure instead of class.

  12. solomon said

    ineed help to get the c++ program

  13. 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

  14. hewan said

    wow i like your website.

  15. veerkumar said

    THE PROGRAM IS DONE LIKE A BEGINNER. U SHOULD PRACTICE PROFESSIONAL PROGRAMMING STYLE.

  16. 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.

  17. 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.

  18. 512407 said

    its Best way 2 stdy abt data structure

  19. Callen said

    this helps a lot!

  20. sai said

    what are advantages and disadvantages in this implementation

  21. 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 ?

  22. Dhina Dhayalan.R said

    Dude,Thnk u 4 tis gr8 website,it helped me throughout my 3rd semester(DS and OOP’s lab).

  23. telecom news…

    [...]STACK IMPLEMENTATION USING LINKED LIST « Data Structures through C & C++ for beginners[...]…

  24. luky boy said

    where is the out put of above progrme plz upload the output of these programes

  25. Casio Privia PX-130 vs PX-330…

    [...]STACK IMPLEMENTATION USING LINKED LIST « Data Structures through C & C++ for beginners[...]…

  26. Abdulraheem sawand said

    Written go0d about stack array and there is also written codes i enjoyed it……

  27. Thanks to my father who shared with me on
    the topic of this website, this weblog is actually awesome.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 102 other followers

%d bloggers like this: