Advertisement
Shailrshah

Circular Queue using Array

Nov 11th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int *array;
  4. int front, rear, size, count;
  5. void display(){
  6.     int counter = count, i = front;
  7.     while(counter > 0){
  8.         if (i == size) i = 0;
  9.         printf("%d\t", array[i++]);
  10.         counter--;
  11.     }
  12.     if(count == 0) printf("\nThe Queue is empty.");
  13.     else if(count == size) printf("\nThe Queue is full.");
  14. }
  15. void insert(int value){
  16.     array[rear] = value;
  17.     if(++rear == size) rear = 0;
  18. }
  19. void delet(){
  20.     if(++front == size) front = 0;
  21. }
  22. int main(){
  23.     int value, choice;
  24.     printf("Enter the size of the queue: ");
  25.     scanf("%d", &size);
  26.     array = (int*) malloc(sizeof(int)*size);
  27.     while(1){
  28.         display();
  29.         printf("\n\n1.Insert 2.Delete 3.Exit: ");
  30.         scanf("%d", &choice);
  31.         switch(choice){
  32.             case 1: if(count < size){
  33.                     printf("\n\nEnter the element to insert: ");
  34.                     scanf("%d", &value);
  35.                     insert(value);
  36.                     count++;
  37.                 } else printf("Error: Overflow.");
  38.                 break;
  39.             case 2: if(count > 0){
  40.                     delet();
  41.                     count--;
  42.                 }
  43.                 else printf("Error: Underflow.");
  44.                 break;
  45.             case 3: return 0;
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement