Virajsinh

Queue_01_LQueue

Dec 4th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.82 KB | None | 0 0
  1. //Linear Queue
  2.  
  3. #include<conio.h>
  4. #include<stdio.h>
  5. #define MAX 3
  6.  
  7. //Global Variable
  8. int queue [MAX];
  9. int front=-1, rear=-1;
  10.  
  11. //UDF Function
  12. void insert();
  13. int Delete_Element();
  14. void display();
  15.  
  16. void main()
  17. {
  18.     int op, val;
  19.     clrscr();
  20.     do
  21.     {
  22.         getch();
  23.         clrscr();
  24.  
  25.         printf("\n -------------------------");
  26.         printf("\n -------Linear Queue------");
  27.         printf("\n -------------------------");
  28.         printf("\n 1. Insert");
  29.         printf("\n 2. Delete");
  30.         printf("\n 3. Display");
  31.         printf("\n 4. Exit");
  32.         printf("\n -------------------------");
  33.         printf("\n Maximum 3 Value Get");
  34.         printf("\n -------------------------");
  35.         printf("\n Enter Choice : ");
  36.         scanf("%d", &op);
  37.         printf(" -------------------------");
  38.         switch(op)
  39.         {
  40.             case 1:
  41.                 insert();
  42.             break;
  43.  
  44.             case 2:
  45.                 val=Delete_Element();
  46.                 if(val !=-1)
  47.                 {
  48.                     printf("\n The Number Deleted : %d", val);
  49.                 }
  50.             break;
  51.  
  52.             case 3:
  53.                 display();
  54.             break;
  55.         }
  56.     }while(op!=4);
  57.     getch();
  58. }
  59.  
  60. //UDF Function Declation
  61. void insert()
  62. {
  63.     int num;
  64.     printf("\n Enter The Number to Be Inserted in Queue : ");
  65.     scanf("%d", &num);
  66.  
  67.     if(rear == MAX-1)
  68.     {
  69.         printf("\n Linear Queue is Overflow");
  70.     }
  71.     else
  72.     {
  73.         if(front == -1 && rear == -1)
  74.         {
  75.             front = rear = 0;
  76.         }
  77.         else
  78.         {
  79.             rear++;
  80.         }
  81.         queue[rear]=num;
  82.     }
  83. }
  84.  
  85. int Delete_Element()
  86. {
  87.     int val;
  88.     if(front == -1 || front > rear)
  89.     {
  90.         printf("\n Linear Queue is Underflow");
  91.         return -1;
  92.     }
  93.     else
  94.     {
  95.         val=queue[front];
  96.         front++;
  97.         if(front > rear)
  98.         {
  99.             front = rear = -1;
  100.         }
  101.         return val;
  102.     }
  103. }
  104.  
  105. void display()
  106. {
  107.     int i;
  108.     //printf("\n ");
  109.     if(front == -1 || front > rear)
  110.     {
  111.         printf("\n Linear Queue is Empty.");
  112.     }
  113.     else
  114.     {
  115.         for(i=front; i<=rear; i++)
  116.         {
  117.             printf("\n Element : %d", i);
  118.             printf(" -> %d", queue[i]);
  119.         }
  120.     }
  121. }
Add Comment
Please, Sign In to add comment