Page 190 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 190

Unit 11: Operations and Applications of Queues




          5.   printf(“Queue Underflow\n”); return ;                                            Notes
          6.   }
          7.   else
          8.   {

          9.   printf(“Element deleted from queue is : %d\n”, queue[front]); front++;
          10.  }
          11.  }
          Line 1 marks the beginning of the queue function. Line 3 states the condition of queue underflow.



             Did u know? Whenever we are deleting elements the queue is incremented.

          Gradually after deleting all elements if the value of queue has exceeded rear, than it means that
          the queue is empty. Line 5 prints the appropriate message. The other condition displays the
          element that is deleted and we know that the first element to be inserted is deleted. The front
          variable holds the index of the first element and it’s deleted. Line 11 increments the front
          variable eventually.

                 Example: The source code below implements the enqueue and dequeue operations on a
          queue.
          # include<stdio.h>
          # define MAX 5
          int queue[MAX];
          int rear = -1,front = -1;
          void main()
          {
            int ch;
            while(1)
            {
              printf(“1.Enqueu\n”);
              printf(“2.Dequeue\n”);
              printf(“3.Display\n”);
              printf(“4.EXIT\n”);
              printf(“Enter your choice:”);
              scanf(“%d”,&ch);
              switch(ch)
              {
                case 1 :
                enqueue();
                break;
                case 2 :
                dequeue();





                                           LOVELY PROFESSIONAL UNIVERSITY                                   183
   185   186   187   188   189   190   191   192   193   194   195