Data Structures(Queues using array)

 

  1. #include<stdio.h>   
  2. #include<stdlib.h>  
  3. #define maxsize 5  
  4. void insert();  
  5. void delete();  
  6. void display();  
  7. int front = -1, rear = -1;  
  8. int queue[maxsize];  
  9. void main ()  
  10. {  
  11.     int choice;   
  12.     while(choice != 4)   
  13.     {     
  14.         printf("\n*************************Main Menu*****************************\n");  
  15.         printf("\n=================================================================\n");  
  16.         printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");  
  17.         printf("\nEnter your choice ?");  
  18.         scanf("%d",&choice);  
  19.         switch(choice)  
  20.         {  
  21.             case 1:  
  22.             insert();  
  23.             break;  
  24.             case 2:  
  25.             delete();  
  26.             break;  
  27.             case 3:  
  28.             display();  
  29.             break;  
  30.             case 4:  
  31.             exit(0);  
  32.             break;  
  33.             default:   
  34.             printf("\nEnter valid choice??\n");  
  35.         }  
  36.     }  
  37. }  
  38. void insert()  
  39. {  
  40.     int item;  
  41.     printf("\nEnter the element\n");  
  42.     scanf("\n%d",&item);      
  43.     if(rear == maxsize-1)  
  44.     {  
  45.         printf("\nOVERFLOW\n");  
  46.         return;  
  47.     }  
  48.     if(front == -1 && rear == -1)  
  49.     {  
  50.         front = 0;  
  51.         rear = 0;  
  52.     }  
  53.     else   
  54.     {  
  55.         rear = rear+1;  
  56.     }  
  57.     queue[rear] = item;  
  58.     printf("\nValue inserted ");  
  59.       
  60. }  
  61. void delete()  
  62. {  
  63.     int item;   
  64.     if (front == -1 || front > rear)  
  65.     {  
  66.         printf("\nUNDERFLOW\n");  
  67.         return;  
  68.               
  69.     }  
  70.     else  
  71.     {  
  72.         item = queue[front];  
  73.         if(front == rear)  
  74.         {  
  75.             front = -1;  
  76.             rear = -1 ;  
  77.         }  
  78.         else   
  79.         {  
  80.             front = front + 1;  
  81.         }  
  82.         printf("\nvalue deleted ");  
  83.     }  
  84.       
  85.       
  86. }  
  87.       
  88. void display()  
  89. {  
  90.     int i;  
  91.     if(rear == -1)  
  92.     {  
  93.         printf("\nEmpty queue\n");  
  94.     }  
  95.     else  
  96.     {   printf("\nprinting values .....\n");  
  97.         for(i=front;i<=rear;i++)  
  98.         {  
  99.             printf("\n%d\n",queue[i]);  
  100.         }     
  101.     }  
  102. }  

Comments

Popular posts from this blog

PHP Array Functions

IMPLEMENTATION OF LRU PAGE REPLACEMENT ALGORITHM

Tableau(Line Graphs)