Page 173 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 173

Fundamentals of Data Structures




                    Notes          NULL value for it, then its a “queue overflow” state, so quit; else go to step (b)
                                   (b) add the new value for the queue position pointed by the “rear”
                                   Algorithm for deletion of an element from the circular queue:
                                   Step-1: If the queue is empty then say “queue is empty” and quit; else continue

                                   Step-2: Delete the “front” element
                                   Step-3: If the “front” is pointing to the last position of the queue then go to step-4 else go to
                                   step-5

                                   Step-4: Make the “front” point to the first position in the queue and quit
                                   Step-5: Increment the “front” position by one.

                                   10.3.1 Array Implementation of a Circular Queue

                                   A circular queue can be implemented using arrays or linked lists. Program given below gives
                                   the array implementation of a circular queue.
                                   #include “stdio.h”
                                   void add(int);
                                   void deleteelement(void);
                                   int max=10; /*the maximum limit for queue has been set*/
                                   static int queue[10];
                                   int front=0, rear=-1; /*queue is initially empty*/
                                   void main()
                                   {
                                   int choice,x;
                                   printf (“enter 1 for addition and 2 to remove element front the queue
                                   and 3 for exit”);
                                   printf(“Enter your choice”);
                                   scanf(“%d”,&choice);
                                   switch (choice)
                                   {
                                   case 1 :
                                   printf (“Enter the element to be added :”);
                                   scanf(“%d”,&x);
                                   add(x);
                                   break;
                                   case 2 :
                                   deleteelement();
                                   break;
                                    }
                                   }
                                   void add(int y)
                                   {
                                    if (rear == max-1)




          166                               LOVELY PROFESSIONAL UNIVERSITY
   168   169   170   171   172   173   174   175   176   177   178