tampurus

3.4 Fixed Deque

Nov 27th, 2021 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. #include<stdio.h>
  2. #define q_s 10
  3. int rear,front;
  4. int queue[q_s];
  5. void Insertion_R(int ele){
  6.     if(rear+1 == q_s) {
  7.         printf("\nQueue is full");
  8.         return;
  9.     }
  10.     else if (rear==-1 && front == -1){
  11.         rear = front = 0;
  12.     }
  13.     else rear++;
  14.     queue[rear] = ele;
  15. }
  16. void Insertion_F(int ele){
  17.     if(front == 0) printf("\nInsertion is not possible");
  18.     else if (front == -1){
  19.         rear = front = 0;
  20.         queue[front] = ele;
  21.     }
  22.     else{
  23.         queue[--front] = ele;
  24.     }
  25. }
  26. void Deletion_F(){
  27.     if(rear==-1) printf("\nQueue is already empty");
  28.     else if (rear == front){
  29.         queue[front]=0;
  30.         rear = front = -1;
  31.     }
  32.     else queue[front++] = 0;
  33. }
  34. void Deletion_R(){
  35.     if(rear == -1)printf("\nQueue is already empty");
  36.     else if (rear == front){
  37.         queue[rear] = 0;
  38.         rear = front = -1;
  39.     }
  40.     else{
  41.         queue[rear--] = 0;
  42.     }
  43. }
  44. void Print(){
  45.     if(rear == -1) {
  46.         printf("\nQueue is empty");
  47.         return;
  48.     }
  49.     for(int i = front ; i<=rear ; i++){
  50.         printf("%d ",queue[i]);
  51.     }
  52. }
  53. int main()
  54. {
  55.     rear = front = -1;
  56.     int key,choice,element,id_choice;
  57.     while(1){
  58.         printf("\n1 for insertion \n2 for deletion\nAny key to just print the queue\n");
  59.         scanf("%d",&choice);
  60.         if(choice==1){
  61.             printf("\nWhich insertion 1 - front and 2 - rear ");
  62.             scanf("%d",&id_choice);
  63.             if(id_choice == 1){
  64.                 printf("Enter the element ");
  65.                 scanf("%d",&element);
  66.                 Insertion_F(element);
  67.             }
  68.             else if(id_choice == 2){
  69.                 printf("Enter the element ");
  70.                 scanf("%d",&element);
  71.                 Insertion_R(element);
  72.             }
  73.         }
  74.         else if(choice==2){
  75.              printf("\nWhich deletion 1 - front and 2 - rear ");
  76.             scanf("%d",&id_choice);
  77.             if(id_choice == 1){
  78.                 Deletion_F();
  79.             }
  80.             else if(id_choice == 2){
  81.                 Deletion_R();
  82.             }
  83.         }
  84.         else Print();
  85.        
  86.         printf("\n1 - for repeat and any key to exit ");
  87.         scanf("%d",&key);
  88.         if(key!=1)break;
  89.     }
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment