Blog Moved

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

Search This Blog

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 (i = 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

26 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
  3. I need the author of this blog, kindly respond to me on whatsapp @ 7042028396 or email @ abhinav.kakku@gmail.com

    for http://www.ethicalhackx.com/

    ReplyDelete
  4. Ethical Hacking Tutorials Coling AUthor
    The author of this blog please contact me @ abhinav.kakku@gmail.com or whatsapp 7042028396

    ReplyDelete
  5. Can you explain the display_queue() module if front > rear.
    Thank You

    ReplyDelete
    Replies
    1. front>rear is not possible in queue. we are printing all elements from front to rear.

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. For usful codes of C , Batch Script etc visit

    http://codeitoff.blogspot.in/

    ReplyDelete
  8. thanks it will be useful to every person

    ReplyDelete
  9. Another easy program to implementing a queue using arrays
    #include
    #include
    void main()
    {
    int queue[8];
    int rear=0;
    while(1)
    {
    printf("enter elements");
    scanf("%d",&queue[rear];
    rear++;
    if(rear==7)
    {
    printf(queue is full");
    break;
    }
    }
    prinf("elements of queue are");
    rear=0;
    while(1)
    {
    printf("%d",queue[rear]);
    rear++;
    if(rear==7);
    break;
    }
    }

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. This comment has been removed by the author.

      Delete
    4. dear srinivasulu srinu,
      your code have some syntax errors,but the concept was good.
      i has modify this as better.

      Thank you.

      code:

      #include <stdio.h>
      #define MAXQUEUE 5
      int main() {
      int queue[MAXQUEUE]={},rear=0;
      printf("Enter %d Integer:\n",MAXQUEUE);

      while(rear<MAXQUEUE) //enqueue
      scanf("%d",&queue[rear++]);

      printf("Queue full,contents are\n");
      printf("\nRear\t\tfront\n");

      while(rear--) //dequeue all
      printf(" %d ",queue[rear]);

      return 0;
      }

      Delete
    5. This program is ... wrong ...
      Upto inserting 10 element in the Queue programm is write ..
      but if we delete front element & try to insert new element in the queue ,, then program is wrong

      Delete
  10. Great idea for an article! Looking forward to the next part.

    Tally Training

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. In delete()
    Under front=max-1
    Rear also needs to be on 0
    So, front=rear=0

    ReplyDelete
  13. In delete()
    Under front=max-1
    Rear also needs to be on 0
    So, front=rear=0

    ReplyDelete


  14. Very informative article.Thank you author for posting this kind of article .



    http://www.wikitechy.com/view-article/explain-in-details-about-array-implementation-of-linked-list-in-c



    Both are really good,.
    Cheers,
    Venkat

    ReplyDelete
  15. Very informative article.Thank you author for posting this kind of article .

    http://www.wikitechy.com/view-article/static-block-in-java-with-example-and-explanation


    Both are really good,
    Cheers,
    Venkat

    ReplyDelete
  16. Thanks, This C Programming Examples will be helpful.

    ReplyDelete
  17. Wow, I was writing a program to represent a queue using R- programming software and this blog has helped me to write the program correctly and I am very grateful to the author for sharing this information with us. Find time and read my article by clicking on Useful Tips for Writing a Literature Review.

    ReplyDelete
  18. superb easy way of understanding

    ReplyDelete
  19. I write in tears and yet joy in my heart ‘cos I brought that scumbag of a BF down after hiring WISETECHHACKER@GMAIL.COM He was my savior, he is so good at what he does, though quite expensive but IT WAS WORTH IT AT THE END. Hey ladies! Let’s say NO to cheats, expose your partner today by hiring wisetechhacker@gmail.com or Whatsapp:+19175085328.   He is very reliable as I choose…

    ReplyDelete