sandeshMC

STACK USING LINKED LIST

Nov 3rd, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. /*
  2. ********************************
  3. ********************************
  4. ****STACK USING LINKED LIST ****
  5. ********************************
  6. ********************************
  7. */
  8. #include<stdio.h>
  9. #include<conio.h>
  10. #include<malloc.h>
  11. typedef struct node {
  12.     int data;
  13.     struct node *next;
  14. }node;
  15. node *top;
  16. void push() {
  17.     node *p;
  18.     int x;
  19.     p=(node *)malloc(sizeof(node));
  20.     printf("enter the data \t\n");
  21.     scanf("%d",&x);
  22.     p->data=x;
  23.     p->next=top;
  24.     top=p;
  25. }
  26. void pop() {
  27.     int x;
  28.     node *p;
  29.     p=top;
  30.     top=p->next;
  31.     x=p->data;
  32.     printf("THE POPPED ELEMENT IS %d ", x);
  33.     free(p);
  34. }
  35. void display() {
  36.     node *p;
  37.     p=top;
  38.     while(p!=NULL) {
  39.         printf("\t%d",p->data);
  40.         p=p->next;
  41.     }
  42. }
  43. main() {
  44.     int i;
  45.     do{
  46.     printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT");
  47.     scanf("%d",&i);
  48.     switch(i) {
  49.     case 1:
  50.         push(); break;
  51.     case 2:
  52.         pop(); break;
  53.     case 3:
  54.         display(); break;
  55.  
  56.     }
  57. }while(i!=4);
  58.     getch();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment