C Program For Stack Implimentation Using Linked List

Ram Pothuraju
#include< stdio.h>
#include< conio.h>

#define NULL 0
void main()
{
struct node{
 int data;
 struct node *link;
};
typedef struct node node;
int val,ch;
node *top=NULL,*temp;
char c='y';
clrscr();
 while(c=='y')
 {
  printf("\n1.Insert \n2.Delete \n3.Display \n4.Exit");
  printf("\n Enter your choice:");
  scanf("%d",&ch);

  switch(ch)
  {
   case 1:
    printf("\n Enter value:");
    scanf("%d",&val);
    temp=(node*)malloc(sizeof(node));
    temp->data=val;
    if(top==NULL)
     temp->link=NULL;
    temp->link=top;
    top=temp;
    break;


   case 2:
    if(top==NULL)
    printf("\n Stack is empty.");
    else
    {
     temp=top;
     printf("\n Deleted element is:%d",temp->data);
     top=top->link;
    }
    break;
   case 3:
    if(top==NULL)
    printf("\n Stack is empty.");
    else
    {
     temp=top;
     printf("\n Stack content:");
     while(temp!=NULL)
     {
      printf("\n %d",temp->data);
      temp=temp->link;
     }
    }
    break;
   case 4:
    exit(0);
  }
 }

getch();
}


OUTPUT


1.Insert
2.Delete
3.Display
4.Exit
 Enter your choice:2

 Stack is empty.
1.Insert
2.Delete
3.Display
4.Exit
 Enter your choice:1

 Enter value:11

1.Insert
2.Delete
3.Display
4.Exit
 Enter your choice:1

 Enter value:22

1.Insert
2.Delete
3.Display
4.Exit
 Enter your choice:1

 Enter value:33






1.Insert
2.Delete
3.Display
4.Exit

 Enter your choice:3

 Stack content:
 33
 22
 11
1.Insert
2.Delete
3.Display
4.Exit
 Enter your choice:2

 Deleted element is:33
1.Insert
2.Delete
3.Display
4.Exit
 Enter your choice:2

 Deleted element is:22
1.Insert
2.Delete
3.Display
4.Exit
 Enter your choice:2

 Deleted element is:11
1.Insert
2.Delete
3.Display
4.Exit
 Enter your choice:3

 Stack is empty.

Post a Comment

0Comments

Post a Comment (0)