Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4.  
  5. struct node
  6. {
  7.     char info[20];
  8.     struct node *ptr;
  9. }*front,*rear,*temp,*front1;
  10.  
  11.  
  12. void enq(char data[]);
  13. void deq();
  14. void empty();
  15. void display();
  16. void create();
  17. void queuesize();
  18.  
  19. int count = 0;
  20.  
  21. void main()
  22. {
  23.     int ch;
  24.     char no[20];
  25.     create();
  26.  
  27.     while (1)
  28.     {
  29.  
  30.         printf("\n 1 - Enque");
  31.         printf("\n 2 - Deque");
  32.         printf("\n 3 - Empty");
  33.         printf("\n 4 - Exit");
  34.         printf("\n 5 - Display");
  35.         printf("\n 6 - Queue size");
  36.    
  37.         printf("\n Enter choice : ");
  38.         scanf("%d", &ch);
  39.         switch (ch)
  40.         {
  41.             case 1:
  42.             printf("Please enter the Vehical ID : ");
  43.             scanf("%s", &no);
  44.             enq(no);
  45.             break;
  46.             case 2:
  47.             deq();
  48.             break;
  49.             case 3:
  50.             empty();
  51.             break;
  52.             case 4:
  53.             exit(0);
  54.             case 5:
  55.             display();
  56.             break;
  57.             case 6:
  58.             queuesize();
  59.             break;
  60.             default:
  61.             printf("Wrong choice, Please enter correct choice ");
  62.             break;
  63.         }
  64.     }
  65. }
  66.  
  67. /* Create an empty queue */
  68. void create()
  69. {
  70.     front = rear = NULL;
  71. }
  72.  
  73. /* Returns queue size */
  74. void queuesize()
  75. {
  76.     printf("\n Queue size : %d", count);
  77. }
  78.  
  79. /* Enqueing the queue */
  80. void enq(char data[])
  81. {
  82.     if (rear == NULL)
  83.     {
  84.         rear = (struct node *)malloc(1*sizeof(struct node));
  85.         rear->ptr = NULL;
  86.         strcpy(rear->info, data);
  87.         front = rear;
  88.     }
  89.     else
  90.     {
  91.         temp=(struct node *)malloc(1*sizeof(struct node));
  92.         rear->ptr = temp;
  93.         strcpy(temp->info, data);
  94.         temp->ptr = NULL;
  95.  
  96.         rear = temp;
  97.     }
  98.     count++;
  99. }
  100.  
  101. /* Displaying the queue elements */
  102. void display()
  103. {
  104.     front1 = front;
  105.  
  106.     if ((front1 == NULL) && (rear == NULL))
  107.     {
  108.         printf("Queue is empty");
  109.         return;
  110.     }
  111.     while (front1 != rear)
  112.     {
  113.         printf("%s ", front1->info);
  114.         front1 = front1->ptr;
  115.     }
  116.  
  117.     if (front1 == rear)
  118.         printf("%s", front1->info);
  119. }
  120.  
  121. /* Dequeing the queue */
  122. void deq()
  123. {
  124. front1 = front;
  125.  
  126. if (front1 == NULL)
  127. {
  128.     printf("\n Error: Trying to display elements from empty queue");
  129.     return;
  130. }
  131. else if (front1->ptr != NULL)
  132. {
  133.     front1 = front1->ptr;
  134.     printf("\n Dequed value : %s", front->info);
  135.     free(front);
  136.     front = front1;
  137. }
  138. else
  139. {
  140.     printf("\n Dequed value : %s", front->info);
  141.     free(front);
  142.     front = NULL;
  143.     rear = NULL;
  144. }
  145. count--;
  146. }
  147.  
  148.  
  149. /* Display if queue is empty or not */
  150. void empty()
  151. {
  152.     if ((front == NULL) && (rear == NULL))
  153.     printf("\n Queue empty");
  154.     else
  155.     printf("Queue not empty");
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement