DS | Practical 5 | C Program to Implement Queue using Array

 

GTU DS Practical-5

Write a C program to implement Queue using arrays that perform the following
operations. 

INSERT ,DELETE ,DISPLAY.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define n 5
  4. int main()
  5. {
  6. int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
  7. printf("\n--------------------\n");
  8. printf("Queue using Array");
  9. printf("\n--------------------\n");
  10. printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit\n");
  11. printf("\n--------------------\n");
  12. while(ch)
  13. {
  14. printf("\nEnter the Choice:");
  15. scanf("%d",&ch);
  16. switch(ch)
  17. {
  18. case 1:
  19. if(rear==x)
  20. printf("\n Queue is Full");
  21. else
  22. {
  23. printf("\n Enter no %d:",j++);
  24. scanf("%d",&queue[rear++]);
  25. }
  26. break;
  27. case 2:
  28. if(front==rear)
  29. {
  30. printf("\n Queue is empty");
  31. }
  32. else
  33. {
  34. printf("\n Deleted Element is %d",queue[front++]);
  35. x++;
  36. }
  37. break;
  38. case 3:
  39. printf("\n Queue Elements are:\n ");
  40. if(front==rear)
  41. printf("\n Queue is Empty");
  42. else
  43. {
  44. for(i=front; i<rear; i++)
  45. {
  46. printf("%d",queue[i]);
  47. printf("\n");
  48. }
  49. break;
  50. case 4:
  51. exit(0);
  52. default:
  53. printf("Wrong Choice: please see the options");
  54. }
  55. }
  56. }
  57. }

Output:


Comments