Advertisement
pkbagchi

implement Queue using Array

Dec 11th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define SIZE 10
  4.  
  5. void enQueue(int);
  6. void deQueue();
  7. void display();
  8.  
  9. int queue[SIZE], front = -1, rear = -1;
  10.  
  11. void main()
  12. {
  13.    int value, choice;
  14.  
  15.    while(1){
  16.       printf("\n\n***** MENU *****\n");
  17.       printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");
  18.       printf("\nEnter your choice: ");
  19.       scanf("%d",&choice);
  20.       switch(choice){
  21.      case 1: printf("Enter the value to be insert: ");
  22.          scanf("%d",&value);
  23.          enQueue(value);
  24.          break;
  25.      case 2: deQueue();
  26.          break;
  27.      case 3: display();
  28.          break;
  29.      case 4: exit(0);
  30.      default: printf("\nWrong selection!!! Try again!!!");
  31.       }
  32.    }
  33. }
  34. void enQueue(int value){
  35.    if(rear == SIZE-1)
  36.       printf("\nQueue is Full!!! Insertion is not possible!!!");
  37.    else{
  38.       if(front == -1)
  39.      front = 0;
  40.       rear++;
  41.       queue[rear] = value;
  42.       printf("\nInsertion success!!!");
  43.    }
  44. }
  45. void deQueue(){
  46.    if(front == rear)
  47.       printf("\nQueue is Empty!!! Deletion is not possible!!!");
  48.    else{
  49.       printf("\nDeleted : %d", queue[front]);
  50.       front++;
  51.       if(front == rear)
  52.      front = rear = -1;
  53.    }
  54. }
  55. void display(){
  56.    if(rear == -1)
  57.       printf("\nQueue is Empty!!!");
  58.    else{
  59.       int i;
  60.       printf("\nQueue elements are:\n");
  61.       for(i=front; i<=rear; i++)
  62.      printf("%d\t",queue[i]);
  63.    }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement