Advertisement
aurko96

Queue

Jun 19th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.55 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. //#include"queue.h"
  4.  
  5. struct node{
  6.     int data;
  7.     struct node *next;
  8. };
  9.  
  10. struct list{
  11.     struct node *head;
  12. };
  13. struct node* createNode()
  14. {
  15.    int a;
  16.    struct node* x=malloc(sizeof(struct node));
  17.    scanf("%d",&a);
  18.    x->data=a;
  19.    x->next=NULL;
  20.    return x;
  21. }
  22. int enqueue(struct list* l,struct node* newnode)
  23. {
  24.     struct node* x;
  25.     if(l->head==NULL)
  26.     {
  27.         l->head=newnode;
  28.         x=l->head;
  29.     }
  30.     else
  31.     {
  32.         x->next=newnode;
  33.         x=x->next;
  34.     }
  35. }
  36. void displayList(struct list* l)
  37. {
  38.     struct node* x = l->head;
  39.     while(x!=NULL)
  40.     {
  41.         printf("\n %d \n",x->data);
  42.         x = x->next;
  43.     }
  44.     getch();
  45. }
  46. struct node* dequeue(struct list* l)
  47. {
  48.     struct node* x;
  49.     if(l->head==NULL)
  50.     {
  51.         return 0;
  52.     }
  53.     else
  54.     {
  55.         x=l->head;
  56.         l->head=(l->head)->next;
  57.     }
  58.     return x;
  59. }
  60.  
  61. int getChoice(int choice){
  62.     char ch=10;
  63.     while(ch!=13){
  64.         system("cls");
  65.         if(choice==1){
  66.             printf("\n   -->\tE N Q U E U E");
  67.         }
  68.         else printf("\n\tEnque");
  69.         if(choice == 2)
  70.             printf("\n   -->\tD E Q U E U E");
  71.         else printf("\n\tDeque");
  72.         if(choice == 3)
  73.             printf("\n   -->\tD I S P L A Y");
  74.         else printf("\n\tDisplay");
  75.         if(choice == 4)
  76.             printf("\n   -->\tQ U I T");
  77.         else printf("\n\tQuit");
  78.         printf("\n\n[ Use UP and DOWN arrow to select an ooption ]");
  79.         ch = getch();
  80.         if(ch == 72)
  81.             choice--;
  82.         else if(ch == 80)
  83.             choice++;
  84.         if(choice<1)
  85.             choice = 1;
  86.         else if(choice>4)
  87.             choice = 4;
  88.     }
  89.     return choice;
  90. }
  91.  
  92. int main()
  93. {
  94.     int choice=1;
  95.     char ch;
  96.     struct node* tempnode;
  97.     struct list l;
  98.     l.head = NULL;
  99.  
  100.     while(choice!=4){
  101.         choice = getChoice(choice);
  102.         switch(choice){
  103.             case 1: tempnode = createNode();
  104.                     enqueue(&l,tempnode);
  105.                     printf("\n\n New node added SUCCESSFULLY.");
  106.                     displayList(&l);
  107.                     break;
  108.             case 2: tempnode = dequeue(&l);
  109.                     if(tempnode!=NULL)
  110.                         printf("\n\n Node deleted SUCCESSFULLY\n Deleted value: %d",tempnode->data);
  111.                     displayList(&l);
  112.                     break;
  113.             case 3: displayList(&l);
  114.                     break;
  115.             case 4: printf("\n\n");
  116.         }
  117.     }
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement