Data structure definition:-

 The data structure Node represents a single node in the linked list]

Structure Node

        (   data to hold the relevant data value in a Node,

            next that holds the address of the next Node      

       )      




Source Code:   

  1. #include<conio.h>
  2. #include<stdio.h>
  3. #define max 5
  4. int queue[max],front=0,rear=0;
  5. int menu();
  6. void enqueue();
  7. void dequeue();
  8. void display();
  9. void main()
  10. {
  11. int ch;
  12. clrscr();
  13. printf("\nQueues using Arrays\n");
  14. do
  15. {
  16.     ch=menu();
  17.     switch(ch)
  18.     {
  19.     case 1: enqueue();
  20.     break;
  21.     case 2: dequeue();
  22.     break;
  23.     case 3: display();
  24.     break;
  25.     case 4: exit();
  26.     break;
  27.     default:printf("\n Please enter a valid choice!!");
  28.     }
  29. }while(1);
  30. }
  31.  
  32. int menu()
  33.     {
  34.     int ch;
  35.     printf("\n1.ENQUEUE \n2.DEQUEUE \n3.DISPLAY \n4.EXIT");
  36.     printf("\nEnter your Choice:");
  37.     scanf("%d",&ch);
  38.     return ch;
  39.     }
  40.  
  41. void enqueue()
  42. {
  43.     int element;
  44.     if(rear==max)
  45.     {
  46.         printf("\nOverflow!!");
  47.     }
  48.     else
  49.     {
  50.         printf("\nEnter Element:");
  51.         scanf("%d",&element);
  52.         queue[rear++]=element;
  53.         printf("\n %d Enqueued at %d",element,rear);
  54.     }
  55.  
  56. }
  57.  
  58. void dequeue()
  59. {
  60.     if(rear==front)
  61.     {
  62.         printf("\nUnderflow!!");
  63.     }
  64.     else
  65.     {
  66.         front++;
  67.         printf("\nElement is Dequeued from %d",front);
  68.    }
  69. }
  70. void display()
  71. {
  72.     int i;
  73.     if(front==rear)
  74.     {
  75.         printf("\nQueue is Empty!!!");
  76.     }
  77.     else
  78.     {
  79.         printf(" \n");
  80.         for(i=front;i<max;i++)
  81.         {
  82.             printf(" | %d ",queue[i]);
  83.         }
  84.             printf("|");
  85.     }
  86. }

Output:

Queues using Arrays
 
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:12
 
 12 Enqueued at 1
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:23
 
 23 Enqueued at 2
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:
23 Enqueued at 2
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:34
 
 34 Enqueued at 3
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:45
 
 45 Enqueued at 4
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:1
 
Enter Element:56
 
 56 Enqueued at 5
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:2
 
Element is Dequeued from 1
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:2
 
Element is Dequeued from 2
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice:3
 
 | 34  | 45  | 56 |
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your Choice: 4
 
 
 

0 comments:

Post a Comment