Advertisement
namkongkirat

Enqueue Dequeue in C

Jul 8th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <windows.h>
  4. #define MAX 100
  5. struct queue
  6. {
  7.     int item[MAX];
  8.     int front, rear;
  9. };
  10. typedef struct queue queue;
  11.  
  12. void enqueue (queue *q, int a)
  13. {
  14.     if (q->rear == MAX - 1)
  15.     printf("Queue is full.\n");
  16.    
  17.     else if (q->rear == -1)
  18.     {
  19.         (q->rear)++;
  20.         (q->front)++;
  21.         q->item[q->rear] = a;
  22.     }
  23.     else
  24.     {
  25.         (q->rear)++;
  26.         q->item[q->rear] = a;
  27.     }
  28. }
  29.  
  30. int dequeue (queue *q)
  31. {
  32.     int elt = 0;
  33.     if(q->front== -1 || q->front > q->rear)
  34.     printf("Queue is empty.\n");
  35.    
  36.     else
  37.     {
  38.         elt = q -> item [q -> front];
  39.         (q->front)++;
  40.     }
  41.     return elt;
  42. }
  43.  
  44. void traverse (queue *q)
  45. {
  46.     int i;
  47.     for(i=q->front; i<=q->rear; i++)
  48.     {
  49.         printf("%d\t", q -> item[i]);
  50.     }
  51. }
  52.  
  53. int main()
  54. {
  55.     queue q;
  56.     q.rear = -1;
  57.     q.front = -1;
  58.     int el,ch;
  59.     while(1)
  60.     {
  61.         printf("\n\n1.Enqueue\n");
  62.         printf("2.Dequeue\n");
  63.         printf("3.Display all\n");
  64.         printf("4.Exit\n\n");
  65.         printf("Enter choice:");
  66.         scanf("%d",&ch);
  67.         switch(ch)
  68.         {
  69.             case 1:
  70.                 printf("Enter element you want to Enqueue");
  71.                 scanf("%d",&el);
  72.                 enqueue(&q,el);
  73.                 break;
  74.            
  75.             case 2:
  76.                 el=dequeue(&q);
  77.                 printf("%d is Dequeued from the stack",el);
  78.                 break;
  79.    
  80.             case 3:
  81.                 traverse(&q);
  82.                 break;
  83.                
  84.             case 4:
  85.                 exit(0);
  86.                            
  87.             default:  
  88.                 printf("Invalid choice.");
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement