This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

10 Nov 2012

C PROGRAM TO IMPLEMENT QUEUE OPERATIONS USING ARRAY

C PROGRAM TO IMPLEMENT QUEUE OPERATIONS USING ARRAY



  1. #include<stdio.h>
  2. #include<conio.h>
  3. #define MAX 10
  4. int queue[MAX], front = -1, rear = -1;
  5. void Insert_Element();
  6. void Delete_Element();
  7. void Display_Queue();
  8. void Empty_Queue();
  9.  
  10. int main()
  11. {
  12. int option;
  13. printf(">>> c program to implement queue operations <<<");
  14. do
  15. {
  16. printf("\n\n 1.Insert an element");
  17. printf("\n 2.Delete an element");
  18. printf("\n 3.Display queue");
  19. printf("\n 4.Empty queue");
  20. printf("\n 5.Exit");
  21. printf("\n Enter your choice: ");
  22. scanf("%d", &option);
  23. switch (option)
  24. {
  25. case 1: Insert_Element();
  26. break;
  27. case 2: Delete_Element();
  28. break;
  29. case 3: Display_Queue();
  30. break;
  31. case 4: Empty_Queue();
  32. break;
  33. case 5: return 0; /*program ends*/
  34. }
  35.  
  36. } while (option != 5);
  37. }
  38.  
  39. void Insert_Element()
  40. {
  41. int num;
  42. if (rear < MAX - 1)
  43. {
  44. if (front == -1)
  45. /*when queue is initially empty */
  46. front = 0;
  47. printf("\n Enter the number to be inserted: ");
  48. scanf("%d", &num);
  49. rear = rear + 1;
  50. queue[rear] = num;
  51. }
  52. else
  53. {
  54. printf("\n Queue OverFlow Occured");
  55. }
  56. }
  57.  
  58. void Delete_Element()
  59. {
  60. int element;
  61.  
  62. if (front == -1 || front > rear)
  63. {
  64. printf("\n Queue Underflow occured.\n");
  65. return;
  66. }
  67. else
  68. {
  69. element = queue[front];
  70. printf("\n Element deleted from queue is : %d", element);
  71. front = front + 1;
  72. }
  73. }
  74.  
  75. void Display_Queue()
  76. {
  77. int i;
  78. if (front == -1 || front > rear)
  79. printf("\n No elements to display");
  80. else
  81. {
  82. printf("\n The queue elements are:\n ");
  83. for (= front; i <= rear; i++)
  84. {
  85. printf("\t %d", queue[i]);
  86. }
  87. }
  88. }
  89.  
  90. void Empty_Queue()
  91. {
  92. /*Reset queue or Creates Empty queue*/
  93. front = -1;
  94. rear = -1;
  95. printf("\n New Queue created successfully.");
  96. }

Sample output :

>>> c program to implement queue operations <<<

 1.Insert an element
 2.Delete an element
 3.Display queue
 4.Empty queue
 5.Exit
 Enter your choice: 1

 Enter the number to be inserted: 10


 1.Insert an element
 2.Delete an element
 3.Display queue
 4.Empty queue
 5.Exit
 Enter your choice: 1

 Enter the number to be inserted: 20


 1.Insert an element
 2.Delete an element
 3.Display queue
 4.Empty queue
 5.Exit
 Enter your choice: 1

 Enter the number to be inserted: 30


 1.Insert an element
 2.Delete an element
 3.Display queue
 4.Empty queue
 5.Exit
 Enter your choice: 2

 Element deleted from queue is : 10

 1.Insert an element
 2.Delete an element
 3.Display queue
 4.Empty queue
 5.Exit
 Enter your choice: 3

 The queue elements are:
         20      30

 1.Insert an element
 2.Delete an element
 3.Display queue
 4.Empty queue
 5.Exit
 Enter your choice:

Download Source Code

2 comments:

  1. Good job! I followed this link searching for the ways to highlight code on blogger. I'm going to try ideone.com

    Thanks for sharing

    ReplyDelete
  2. @horizon : keep visiting my blogs

    ReplyDelete

Thanks for your comments.
-Sameer