Write a C program to implement stack using linked list

DATA STRUCTURE DEFINITION

_____________________________________________________________________

NODE: Data {To store data  in  the Node}

Next {To store link of  the next Node}

_____________________________________________________________________

Source Code:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct stack
  4. {
  5.     int data;
  6.     struct node *next;
  7. };
  8. typedef struct stack stack;
  9.  
  10. void push(int data);
  11. int pop();
  12. void display();
  13.  
  14. int top=0,size;
  15. stack *head=NULL;
  16. void main()
  17. {
  18.     int no,ch,e,r;
  19.     printf("\nEnter the size of stack  ");
  20.     scanf("%d", &size);
  21.     printf("\n 1 - Push");
  22.     printf("\n 2 - Pop");
  23.     printf("\n 3 - Dipslay");
  24.     printf("\n 0 - Exit");
  25.     do
  26.     {
  27.         printf("\n Enter choice : ");
  28.         scanf("%d", &ch);
  29.  
  30.         switch (ch)
  31.         {
  32.         case 0:
  33.             break;
  34.         case 1:
  35.             printf("\nEnter data : ");
  36.             scanf("%d", &no);
  37.             push(no);
  38.             break;
  39.         case 2:
  40.             r=pop();
  41.             printf("\n%d popped",r);
  42.             break;
  43.         case 3:
  44.             display();
  45.             break;
  46.         default :
  47.             printf(" Wrong choice, Please enter correct choice  ");
  48.             break;
  49.         }
  50.     }while(ch!=0);
  51. }
  52. void push(int v)
  53. {
  54.    if(top<size){
  55.    stack *new_node;
  56.    new_node=(stack*)malloc(sizeof(stack));
  57.    new_node->data=v;
  58.    new_node->next=NULL;
  59.    if(head==NULL)
  60.    head=new_node;
  61.    else
  62.    {
  63.                new_node->next=head;
  64.                head=new_node;
  65.    }
  66.    top++;
  67. }
  68.    else
  69.    printf("\nStack is full!");
  70. }
  71. int pop()
  72. {
  73.                stack *tmp;
  74.                int v;
  75.                if(top>=1)
  76.                {
  77.                               tmp=head;
  78.                               head=head->next;
  79.                               v=tmp->data;
  80.                               free(tmp);
  81.                               top--;
  82.                }
  83.                else
  84.                v=-999;
  85.                return v;
  86. }
  87. void display()
  88. {
  89.                stack *p;             
  90.                p=head;
  91.                if(p!=NULL)
  92.                {
  93.                               printf("\nLinked List\n");
  94.                               while(p!=NULL)
  95.                               {
  96.                                              printf("%d ",p->data);      
  97.                                              p=p->next;
  98.                               }
  99.    }
  100. }

 

Output:

Enter the size of stack  8 

 1 - Push

 2 - Pop

 3 - Dipslay

 4 - Exit

 Enter choice : 1

Enter data : 33

 Enter choice : 1

Enter data : 65

 Enter choice : 1

Enter data : 22

 Enter choice : 1

Enter data : 77

 Enter choice : 2

77 popped

 Enter choice : 3

Linked List

22 65 33

 Enter choice : 4


0 comments:

Post a Comment