logow

Queue implementation using C in simple code and explanation

what is Queue in data structures ? 







Queue
forloop4.blogspot.com



So Basically Queue is opposite to Stack  (Tap on Stack to see Stack Algorithms) that means Stack works on LIFO techniques and Queue works on FIFO techniques.It is also a part of linear data structures that's store in memory sequentially.it is follow FIFO approaches that means first element insert and remove first.That's all about Queue Definition.

Examples ------
1.) Ticket counter at Railway station or outside the Cinema Halls during the Buy Movies Tickets.
2.) Doctor's Clinic 
3.) Call center 
4.) During College Admissions
5.) Exam Hall's and many more that's follows queue FIFO techniques.


Operations Perform On Queue------

Basically, there are two mainly operations perform's on Queue-----
1.) Enqueue or Insertion in Queue 
2.) Dequeue or Deletion in Queue 

1.) Enqueue----- when we insert elements in Queue that's called Enqueue or we can also say to Push something in queue.we can insert elements in queue from rear side. For Example...... See above image for more clarity.

Algorithms for Enqueue Operation--------

1.) First check Queue is Overflow or not.we declare array size=max means 5.we know Enqueue operation perform from rear side.So when rear==max-1 means when rear full then we print Queue Overflow.we cannot Insert more elements at all.

2.) If not Overflow then we increment rear=-1 to rear++ , rear=0 (-1+1=0) and Insert element at array[rear]=array[0]=Element . 

----------------------------------------------------------------------------------------------------------------

2.) Dequeue------ when we delete elements from Queue that's called Dequeue or we can also say Pop something in queue.we can delete elements in queue from front side. For Example...... See above image for more clarity.


Algorithms for Dequeue Operation----------

1.) First check Queue is Underflow or not.we declare front=-1 means if no elements present in Queue deletion not Possible.we simply print a message to user Queue Underflow.

2.) If not Underflow then we increment front=-1 to front++ , front=0 (-1+1=0) and Dequeue elements or delete elements from Queue.

Things to remember.......

Rear always Holds Last array index elements and point last Index of array.

Front always Holds Start array index elements and point last Index of array.

In starting when no operations perform on queue both front and rear points first Index of Array.




Applications of Queue...............

1.) Queue used in a Scheduling Process to allocate resources to CPU.
2.) Queue used to transfer data between Process.

Explanation of Program In C  

  1. First of all we declare int front=-1 and int rear=-1 and we already define array size globally so we create an Array size max=5 and 3 functions void insert(); for enqueue void dequeue(); for deletion and void display(); for display Queue Elements.
  2. In main Function we declare int ch; for switch case . we use Switch case and call all functions in switch case base on user's choice.we also use while(1) that's means 1 means always condition true and Menu of queue run repeatedly.
  3. In void insert() function we declare int data for Elements taken by user and we also discuss if rear==max-1 then print queue Overflow else we check if front still -1 then we do front=0 and print message to user enter elements and store it to data variable and increment rear++ and insert data successfully in Queue.
  4. In void dequeue() function if front==-1 and front>rear that's means if  queue has no data then deletion not possible and front bigger than rear that's means there is nothing in Queue. else we increase front++ to delete elements from Queue like array[front] = data deletion . 
  5. In void display() function we declare int i to initialize for loop.Again we check if front==-1 then there no elements in Queue we simply print message to user Queue is empty.else in for loop i=front that's means we already do front=0 (i=0) and i<=rear that's means i goes to the size of Array (i<=4) and Print array[i].That's all .


Linear Queue VS Circular Queue 



So why we use Circular Queue Because In Linear Queue if rear goes to the end point in Array Index we cannot Insert more elements in Queue.we need to delete elements by increasing front and shift left data in to right side for more space this is all about your time wastage.In next post we discuss Circular Queue.


Before Going to read source i request you to read carefully Explanation of program for better understanding...............Amit------

Source Code------





Download Full code Below press red line below.........


Download Full Source Code Here



OUTPUT OF PROGRAM HERE...........


                 CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::
         3. DISPLAY DATA FROM QUEUE:::::
1
         NOW ENTER ELEMENTS IN QUEUE::::
45
                 CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::
         3. DISPLAY DATA FROM QUEUE:::::
1
         NOW ENTER ELEMENTS IN QUEUE::::
78
                 CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::
         3. DISPLAY DATA FROM QUEUE:::::
1
         NOW ENTER ELEMENTS IN QUEUE::::
89
                 CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::
         3. DISPLAY DATA FROM QUEUE:::::
1
         NOW ENTER ELEMENTS IN QUEUE::::
65
                 CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::
         3. DISPLAY DATA FROM QUEUE:::::
1
         NOW ENTER ELEMENTS IN QUEUE::::
46
                 CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::
         3. DISPLAY DATA FROM QUEUE:::::
1
         QUEUE IS OVERFLOW::::
                 CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::
         3. DISPLAY DATA FROM QUEUE:::::
2

 DEQUEUED::::::45                CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::
         3. DISPLAY DATA FROM QUEUE:::::
3
         ELEMENTS ARE:::::

78
89
65
46               CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::
         3. DISPLAY DATA FROM QUEUE:::::
1
         QUEUE IS OVERFLOW::::
                 CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::
         3. DISPLAY DATA FROM QUEUE:::::
2

 DEQUEUED::::::78                CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::
         3. DISPLAY DATA FROM QUEUE:::::
3
         ELEMENTS ARE:::::

89
65
46               CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::
         3. DISPLAY DATA FROM QUEUE:::::
2

 DEQUEUED::::::89                CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::
         3. DISPLAY DATA FROM QUEUE:::::
3
         ELEMENTS ARE:::::

65
46               CHOOSE YOUR SPECIFIC OPERATION::::

         1. INSERT DATA IN QUEUE:::::
         2. DELETE DATA FROM QUEUE:::::

         3. DISPLAY DATA FROM QUEUE:::::



Post a Comment

0 Comments