Advertisement
sanpai

Circular Queue

Sep 16th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. #include<stdio.h>
  2. #define MAX 5
  3. void insert(int,int *,int q[],int *r);
  4. void display(int *,int a[],int *);
  5. int delete(int *,int *,int q[]);
  6. int main()
  7. {
  8. int choice,item,q[5];
  9. int front=0,rear=-1,count=0;
  10.  
  11.  
  12. for(;;)
  13. {
  14. printf("\n Enter the choice : 1-Insert 2- Display");
  15. printf(" \n 3-Delete 4-Exit : ");
  16. scanf("%d",&choice);
  17. switch (choice)
  18. {
  19.  
  20. case 1 :printf(" \n Enter the number to be added to queue :");
  21. scanf("%d",&item);
  22. insert(item,&rear,q,&count);
  23. break;
  24. case 2 :display(&front,q,&rear);
  25. break;
  26. case 3 :item=deleteq(&front,&count,q);
  27. if(item==0)
  28. printf("\n The queue is empty \n");
  29. else
  30. printf(" \n The deleted item : %d",item);
  31. break;
  32. default:exit(0);
  33. break;
  34. }
  35. }
  36.  
  37. }
  38.  
  39. void insert(int item,int *rear,int a[5],int *count)
  40. {
  41. if(*count==MAX)
  42. {
  43.  
  44. printf(" \n The queue is full \n");
  45. }
  46.  
  47. else
  48. {
  49. *rear=(*rear+1)%MAX;
  50. a[*rear]=item;
  51. *count=*count+1;
  52. }
  53. }
  54.  
  55. void display(int *f,int a[5],int *r)
  56.  
  57. {
  58. int i;
  59. for(i=*f;i<=*r;i++)
  60.  
  61. {
  62. printf("\n %d",a[i]);
  63. }
  64. }
  65.  
  66. int deleteq(int *f,int *c,int q[5])
  67. {
  68. int item;
  69. if(*c==0)
  70. {
  71.  
  72.  
  73. return 0;
  74. }
  75. else
  76.  
  77. {
  78. item=q[*f];
  79. *f=(*f+1)%MAX;
  80. *c=*c-1;
  81. return item;
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement