Advertisement
Shailrshah

Array Implementation of Queues

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