Stack using Linked List

19. Write a program to implement stack in a linked list?

Program


#include<stdio.h>
#include<conio.h>
struct node                      //declaring the structure of linked list
          {
          int data;
          struct node *link;
          }*top=NULL,*newnode,*temp,*k,*temp1,*prev;
void push()                                         //Push operation
          {
          int item;
          printf("Enter the item:\t");
          scanf("%d",&item);
          if(top==NULL)                           //list is empty
                   {
                   newnode=(struct node *)malloc(sizeof(struct node));
                   newnode->data=item;
                   newnode->link=NULL;
                   top=newnode;
                   k=top;
                   }
          Else                                           //list is not empty
                   {
                   newnode=(struct node *)malloc(sizeof(struct node));
                   top->link=newnode;
                   newnode->data=item;
                   newnode->link=NULL;
                   top=newnode;
                   }
          }
void pop()                                 //pop operation
          {
          if(top==NULL)                  //if list is empty
                   {
                   printf("\tSTACK UNDERFLOW\n");
                   }
          Else                                 //if list contain elements
                   {
                   temp=top;
                   printf("The poped item is %d\n",temp->data);
                   if(top==k)
                             {
                             top=NULL;
                             free(top);
                             }
                   else
                   {
                   temp1=k;
                   while(temp1->link!=NULL)
                             {
                             prev=temp1;
                             temp1=temp1->link;
                             }
                   prev->link=NULL;
                   top=prev;
                   free(temp);
                   }
                   }
          }
void display()                                      //display function
          {
          if(top==NULL)
                   printf("Stack is empty\n");
          else
                   {
                   temp=k;
                   printf("Stack elements are:\n");
                   while(temp!=NULL)
                             {
                             printf("%d\t",temp->data);
                             temp=temp->link;
                             }
                   }
          }
void main()
          {
          int ch;
          char c;
          clrscr();
          do
          {
          printf("Enter the choice:\t1.Push\t2.Pop\t3.Display\n");
          scanf("%d",&ch);
          switch(ch)
                   {
                   case 1:
                             push();         //calling push function
                             break;
                   case 2:
                             pop();          //calling pop function
                             break;
                   case 3:
                             display();     //calling display function
                             break;
                   default:
                             printf("\nWrong choice\n");
                             break;
                   }
          printf("\nDo you want to continue:(y/n)\n");
          scanf(" %c",&c);
          }
          while((c=='y')||(c=='Y'));
          getch();
          }
        
 Output


Enter the choice:       1.Push  2.Pop   3.Display
1                                  
Enter the item: 11

Do you want to continue:(y/n)
y
Enter the choice:       1.Push  2.Pop   3.Display
1
Enter the item: 12

Do you want to continue:(y/n)
y
Enter the choice:       1.Push  2.Pop   3.Display
3
Stack elements are:
11      12
Do you want to continue:(y/n)
y
Enter the choice:       1.Push  2.Pop   3.Display
2
The poped item is 12

Do you want to continue:(y/n)
y
Enter the choice:       1.Push  2.Pop   3.Display
2
The poped item is 11

Do you want to continue:(y/n)
y
Enter the choice:       1.Push  2.Pop   3.Display
3
Stack is empty

Do you want to continue:(y/n)
y
Enter the choice:       1.Push  2.Pop   3.Display
2
          STACK UNDERFLOW

Do you want to continue:(y/n)
n

No comments:

Post a Comment