C++ programs to implement the Stack ADT using a singly linked list

Wednesday, March 10th, 2010

/* Write C++ programs to implement the Stack ADT using a singly linkedlist*/

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
      public:
             class node *next;
             int data;
};
 
class stack : public node
{
            node *head;
            int tos;
      public:
             stack()
               {
                    tos=-1;
               }
             void push(int x)
              {        
              	if (tos < 0 )
                  {
                     head =new node;
                     head->next=NULL;
                     head->data=x;
                     tos ++;
                   }
             else
                  {
                   	 node *temp,*temp1;
                     temp=head;
                     if(tos >= 4)
                     	{
                      		cout <<"stack over flow";
                        	return;
                         }
                     tos++;
                     while(temp->next != NULL)
                          temp=temp->next;
                     temp1=new node;
                     temp->next=temp1;
                     temp1->next=NULL;
                     temp1->data=x;
                   }
                }
             void display()
               {
                  node *temp;
                  temp=head;
                  if (tos < 0)
                    {
                        cout <<" stack under flow";
                        return;
                     }
                  while(temp != NULL)
                     {
                        cout <<temp->data<< " ";
                        temp=temp->next;
                     }
               }
              void pop()
                {
                   node *temp;
                   temp=head;
                   if( tos < 0 )
                    {
                       cout <<"stack under flow";
                       return;
                    }                  
                    tos--;
                    while(temp->next->next!=NULL)
                      {
                     	temp=temp->next;
                       }
                    temp->next=NULL;
                 }
};
main()
{
	stack s1;
	int ch;
	while(1)
	{
	cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n enter ur choice:";
	cin >> ch;
	switch(ch)
	      {
		case 1:   cout <<"\n enter a element";
                 		  cin >> ch;
                 		  s1.push(ch);
                 		  break;
		case 2:   s1.pop();break;
 
 
 
 
case 3:   s1.display();
                 		   break;
		case 4:   exit(0);
		}
     }
return (0);
}

OUTPUT
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element23

1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element67

1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:3
23 67
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2

1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:3
23
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2

1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
stack under flow
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:4

Avatar of Ranjith

Author Name :
Ranjith

Total : 8 Comments


8 Responses to “C++ programs to implement the Stack ADT using a singly linked list”

  1. Hari says:

    this contains 4 errors…… please rectify it

  2. srikanth says:

    in the above program what is tos indicates

  3. ravi says:

    wat errors sir plz rectify it soon……………….

  4. ravi says:

    sir plz provide program widout errors it i dffcul to rectify!!!!!!!!!!!!!!!!!!!!!!!!plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

  5. kavya says:

    plz keep prog with out any errosssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss’\”S”

  6. srikanth says:

    TOS=TOP OF STACK?

  7. Sanku Saikat says:

    Hey friends the errors are easily corrected… just add a .h after #include
    and delete the include namespace line… rest are the warnings of keeping the functions inline

  8. Joshua says:

    You just made a queue… A stack is First in Last out. A queue is First in First out. If I run this, and push 1, 2, and 3 in that order, when I display the data, I should get 3, 2, 1. That is how a stack works. Yours gives me 1,2,3. While the order of data is irrelevant to me, I’m using my program for my portfolio and I’d look pretty dumb if I submitted a stack and it worked like a queue…

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: