sahajjain01

15.Implement stack using linked list.

Aug 23rd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.34 KB | None | 0 0
  1. //Program to implement stack using linked list.
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<malloc.h>
  5.  
  6. struct node {
  7.     int t_no;
  8.     char p_name[50];
  9.     struct node *link;
  10. } *temp = NULL, *top = NULL;
  11.  
  12. void insert() {
  13.     clrscr();
  14.     temp = (struct node *) malloc(sizeof(struct node));
  15.     printf("\n Enter ticket no. : ");
  16.     scanf("%d", &temp->t_no);
  17.     printf(" Enter passenger name: ");
  18.     scanf("%s", &temp->p_name);
  19.     temp->link = top;
  20.     top = temp;
  21.     printf("\n Insert Successful!");
  22.     printf("\n Press any key to continue...");
  23.     getch();
  24. }
  25. void display() {
  26.     clrscr();
  27.     if (top == NULL) {
  28.         printf("\n The stack is empty!");
  29.     } else {
  30.         temp = top;
  31.         while (temp != NULL) {
  32.             printf("\n Ticket no. : %d", temp->t_no);
  33.             printf("\n Passenger name: ");
  34.             puts(temp->p_name);
  35.             temp = temp->link;
  36.         }
  37.     }
  38.     printf("\n Press any key to continue...");
  39.     getch();
  40. }
  41. void delete_node() {
  42.     clrscr();
  43.     if (top == NULL) {
  44.         printf("\n The stack is empty!");
  45.     } else {
  46.         temp = top;
  47.         top = top->link;
  48.         printf("\n Deleted the following record: ");
  49.         printf("\n Ticket no. : %d", temp->t_no);
  50.         printf("\n Passenger name: ");
  51.         puts(temp->p_name);
  52.         free(temp);
  53.     }
  54.     printf("\n Press any key to continue...");
  55.     getch();
  56. }
  57. void main()
  58. {
  59.     int choice;
  60.     clrscr();
  61.     while(1) {
  62.         clrscr();
  63.         printf("\n\t\t\tMain Menu\n\t1.Insert\n\t2.Display\n\t3.Delete\n\t4.Exit");
  64.         printf("\n Enter your choice: ");
  65.         scanf("%d", &choice);
  66.         switch(choice) {
  67.             case 1:
  68.                 insert();
  69.                 break;
  70.  
  71.             case 2:
  72.                 display();
  73.                 break;
  74.  
  75.             case 3:
  76.                 delete_node();
  77.                 break;
  78.  
  79.             case 4:
  80.                 return;
  81.         }
  82.     }
  83. }
  84.  
  85. //Program to implement queue using linked list.
  86. #include<conio.h>
  87. #include<stdio.h>
  88. #include<malloc.h>
  89.  
  90. struct node {
  91.     int t_no;
  92.     char p_name[50];
  93.     struct node *link;
  94. } *temp=NULL, *front=NULL, *rear=NULL;
  95.  
  96. void insert() {
  97.     clrscr();
  98.     temp = (struct node *) malloc(sizeof(struct node));
  99.     printf("\n Enter ticket no.: ");
  100.     scanf("%d", &temp->t_no);
  101.     printf("\n Enter passenger name: ");
  102.     scanf("%s", &temp->p_name);
  103.     temp->link = NULL;
  104.    
  105.     if (front == NULL) {
  106.         front = temp;
  107.         rear = temp;
  108.     } else {
  109.         rear->link = temp;
  110.         rear = temp;
  111.     }
  112.     printf("\n Insert Successful!");
  113.     printf("\n Press any key to continue...");
  114.     getch();
  115. }
  116. void display() {
  117.     clrscr();
  118.     if (front == NULL) {
  119.         printf("\n The queue is empty!");
  120.     } else {
  121.         temp = front;
  122.         while (temp != NULL) {
  123.             printf("\n Ticket no. : %d", temp->t_no);
  124.             printf("\n Passenger name: ");
  125.             puts(temp->p_name);
  126.             temp = temp->link;
  127.         }
  128.     }
  129.     printf("\n Press any key to continue...");
  130.     getch();
  131. }
  132. void delete_node() {
  133.     clrscr();
  134.     if (front == NULL) {
  135.         printf("\n The queue is empty!");
  136.     } else {
  137.         temp = front;
  138.         front = front->link;
  139.         printf("\n Deleted the following record: ");
  140.         printf("\n Ticket no. : %d", temp->t_no);
  141.         printf("\n Passenger name: ");
  142.         puts(temp->p_name);
  143.         free(temp);
  144.     }
  145.     printf("\n Press any key to continue...");
  146.     getch();
  147. }
  148. void main()
  149. {
  150.     int choice;
  151.     clrscr();
  152.     while(1) {
  153.         clrscr();
  154.         printf("\n\t\t\tMain Menu\n\t1.Insert\n\t2.Display\n\t3.Delete\n\t4.Exit");
  155.         printf("\n Enter your choice: ");
  156.         scanf("%d", &choice);
  157.         switch(choice) {
  158.             case 1:
  159.                 insert();
  160.                 break;
  161.  
  162.             case 2:
  163.                 display();
  164.                 break;
  165.  
  166.             case 3:
  167.                 delete_node();
  168.                 break;
  169.  
  170.             case 4:
  171.                 return;
  172.         }
  173.     }
  174. }
Add Comment
Please, Sign In to add comment