Stack using SLL(Singly Linked List)

Program Description

The program demostrates the implementation of stack data structure using singly linked list.

Stack is a FIFO(First In First Out)data-structure. Basic stack operations like push,pop,empty are implemented using classes.

Program
/**********************************************
* 
*    Stack using SLL(Singly Linked List)
*
*	 code.cheraus.com
*
**********************************************/


#include<iostream>
#include<cstdlib>
using namespace std;


//declaration of node class for linked list
class node
{
 public:
    int data;
    node *next;   //pointer to next node
	
	//constructor
    node(int x)
   { 
      data=x;
      next=NULL;
   }
};



//declaration of class stack
class stack
{
  public:
   node *top,*root;    //top holds the top of stack
   
   //constructor 
   stack()
    {
	 top=NULL;
	 }
	 
   //function declarations 
   node *fo(node *t);
   void push(int x);
   int pop();
   int empty();
   void display();
};


/*
Function definition for push() function.

This function pushes an integer data element on top of stack.

Input: The function takes only one integer argument,which is to be pushed on stack.
Returns: The function does not return anything.

*/
void stack::push(int x)
{
   node *p;
   p = new node(x);
   if(top==NULL)
    {
      top=root=p; 
    }
   else
    {
      top->next=p;
      top=top->next;
    }
}


/*
Function definition for pop() function.

This function pops an integer data element from the top of stack.

Input: The function does not take any input.
Returns: The function returns the popped integer element.

*/
int stack::pop()
{
     int x;

     if(top==NULL)
        return -1010;
	 
	 x=top->data;
	 
     if(x==root->data)
     {
        top=NULL;
        root=NULL;
     }
     else{
           top=fo(top);
     }
     return x;
}


/*
Function definition for fo() function.

This function returns the address of the new top element.

Input: The function takes the current top of stack pointer.
Returns: The function returns the the new top of stack after popping one element from the stack.

*/
node *stack::fo(node *t)
{
 node *p=root;
  while(p->next!=t)
    p=p->next;
   return p;
}



/*
Function definition for display() function.

This function displays all the elements currently in the stack.

Input: The function does not take any input.
Returns: The function does not return anything.

*/
void stack::display()
{
   if(top!=NULL)
   { 
     cout<<"\n";
     node *p=root;
     while(p!=top)
      {
        cout<<p->data<<"\t";
        p=p->next;
      }
     cout<<p->data<<"\t";
   }
  else
  {
    cout<<"\nStack is empty";
  }
}



int main()
{
   int opt,x;
   stack s;
 do{   
   cout<<"\n\t\tmain menu\n1.Insert\n2.Delete\n3.Display\n4.Exit";
   cout<<"\nEnter the choice : ";
   cin>>opt;
   switch(opt)
   {
     case 1:
              cout<<"\nEnter the data : ";
              cin>>x; 
              s.push(x);
              s.display();
              break;
     case 2:
              if(s.top!=NULL)
              {
                x=s.pop();
                cout<<"\nPopped element is "<<x;
                s.display();
               }
               else
                cout<<"\nStack is Empty";
              break;
    case 3:
             s.display();
             break;
   case 4:
             break;

   }
}while(opt!=4);
return 0;
}

Output

main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 1

Enter the data : 1

1
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 1

Enter the data : 2

1 2
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 1

Enter the data : 3

1 2 3
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 1

Enter the data : 4

1 2 3 4
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 1

Enter the data : 5

1 2 3 4 5
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 1

Enter the data : 6

1 2 3 4 5 6
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 1

Enter the data : 6

1 2 3 4 5 6 6
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 2

Popped element is 6
1 2 3 4 5 6
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 2

Popped element is 6
1 2 3 4 5
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 2

Popped element is 5
1 2 3 4
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 3

1 2 3 4
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 2

Popped element is 4
1 2 3
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 2

Popped element is 3
1 2
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 1

Enter the data : 4

1 2 4
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 2

Popped element is 4
1 2
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 2

Popped element is 2
1
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 2

Popped element is 1
Stack is empty
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 2

Stack is Empty
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 2

Stack is Empty
main menu
1.Insert
2.Delete
3.Display
4.Exit
Enter the choice : 4ΓΏ

Comments
comments powered by Disqus