facebook like button

09 August, 2011

Concept of Queues in C

      The queue is another type of data structure. This is analogous to a line of persons at a ticket counter. Like stack here in C queue also is not a physical variable or object which you can point out in the program and say that this is my queue variable. This is an abstraction.
      Let me explain this in a simple way. You people may have seen queues ( a line of entities or persons) in X-Ray baggage inspection machine or at ticket booking counter. In both the physical systems there is a region with two ends. We call these ends front and rear. Lets go by the example of ticket booking counter queue. In one of its ends the persons enter and exits from the other one. So you can observe that the person which was enters first will begetting out first at the other end and the person who went last will be getting out last. This was the real life example of queue. In technical area also concept is exact same. We call this First In First Out (FIFO) the concept of queues. I have already told you people that queue is an abstraction. Its just the implementation of the real world queue concept onto our programs. In programming wherever we treat a bulk of elements in the same manner as persons at ticket booking counter (in FIFO fashion) we say that this is implementation of queue. We can implement queue in two ways:
1. queue implementation in arrays.
2. queue implementation in linked lists.


     To understand the queue concept in programming perspective you people need to know 4 terms: 
1. element : is a member of queue may be of a derived or built in datatype.
2. front: is the the variable which holds the index(in case of array) or address (in case of linked list) of front element. 
3. rear: is the the variable which holds the index(in case of array) or address (in case of linked list) of rear element. 
4. enqueue() : is the user defined function to add an element in queue. The element is added to the rear end of the queue.
5. dequeue() : is the user defined function to remove an element from queue. The element is removed from the front end of the queue.


      Because the concept is same so both user defined functions enqueue() and dequeue() are used in both the implementations. The definitions of enqueue() and dequeue() are different for both implementations but the working is same as described above. Those definitions will be given in the upcoming programs.
To elaborate these implementations more programs will be put in next posts.


Remarks:
1. While programming you should not worry about the whole queue like where it resides, how to keep track of all elements etc. The queue is abstract thing. only things you need to concentrate on and to keep track are the front and rear.
2. Both the operations enqueue() or dequeue() are concerned with front and rear respectively. The element put by enqueue() in the queue always becomes the rear and the dequeue() function always removes the front element.
3. When dequeue removes the front element, the front variable points to next element which is now on the front.This is also true in real life examples of persons on the ticket booking counter. when the front person is takes the ticket and goes out of the queue, the person just after the previous one now at comes at front.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply