You are on page 1of 7

Learning Computer Programming

A resource for Learning Computer Programming. I post articles, tips, tricks, techniques, algorithms etc. related to C++, PHP and other programming language. Many of the things that I discuss here applies to most languages.

Wednesday, July 04, 2007


Data Structures: Introduction to Queues
Labels: Algorithms, Class, Data Structures, Example Programs, Methods, OOP, Queues Queue is a linear data structure in which data can be added to one end and retrieved from the other. Just like the queue of the real world, the data that goes first into the queue is the first one to be retrieved. That is why queues are sometimes called as First-In-First-Out data structure. In case of queues, we saw that data is inserted both from one end but in case of Queues; data is added to one end (known as REAR) and retrieved from the other end (known as FRONT). The data first added is the first one to be retrieved while in case of queues the data last added is the first one to be retrieved. A few points regarding Queues: 1. Queues: It is a linear data structure; linked lists and arrays can represent it. Although representing queues with arrays have its shortcomings but due to simplicity, we will be representing queues with arrays in this article. 2. Rear: A variable stores the index number in the array at which the new data will be added (in the queue). 3. Front: It is a variable storing the index number in the array where the data will be retrieved. Let us have look at the process of adding and retrieving data in the queue with the help of an example. Suppose we have a queue represented by an array queue [10], which is empty to start with. The values of front and rear variable upon different actions are mentioned in {}. queue [10]=EMPTY {front=-1, rear=0} add (5) Now, queue [10] = 5 {front=0, rear=1} add (10) Now, queue [10] = 5, 10 {front=0, rear=2}

retrieve () [It returns 5] Now, queue [10] = 10 {front=1, rear=2} retrieve () [now it returns 10] Now, queue [10] is again empty {front=-1, rear=-1} In this way, a queue like a stack, can grow and shrink over time. Now have a look at the following example program that illustrates all this stuff:
// -- A Queue Class in C++ -// example program in C++ to // illustrate queues represented // by arrays #include<iostream.h> // macro to hold the max // number of elements // in the queue #define MAX 10 // queue class class queue { int arr[MAX]; int front, rear; public: void add(int); int retrieve(void); queue(); }; // queue class ends // member functions queue::queue() { // initialize index // variables front=-1; rear=0; } void queue::add(int data) { if(rear==MAX-1) { cout<<"QUEUE FULL!"; return; } arr[rear]=data; // increase index // variable rear++;

if(front=-1) front=0; } int queue::retrieve() { int data; if(front==-1) { cout<<"QUEUE EMPTY!"; return NULL; } data=arr[front]; arr[front]=0; // if both index variables // point to the same location // then start afresh if(front==rear-1) { front=-1; rear=0; } else front++; return data; } // member functions ends void main(void) { queue obj; int ch; int num; while(ch!=3) { cout<<"1> ADD"; cout<<"\n2> RETRIVE"; cout<<"\n3> QUIT\n"; cin>>ch; switch(ch) { case 1: cout<<"enter element:"; cin>>num; obj.add(num); break; case 2: cout<<"\n\nRetrieved: "; cout<<obj.retrieve(); cout<<"\n\n"; break;

} } }

Good-Bye Please do check back for updates!

Queue Data Structure


Queue is a data structure that maintain "First In First Out" (FIFO) order. And can be viewed as people queueing up to buy a ticket. In programming, queue is usually used as a data structure for BFS (Breadth First Search).

Queue operations

Operations on queue Q are : 1. enqueue - insert item at the back of queue Q 2. dequeue - return (and virtually remove) the front item from queue Q 3. init - intialize queue Q, reset all variables. There are other operations such as full and empty, but usually we don't use this when doing Valladolid Online Judge or Programming Contest Problems, since we know the largest size of queue. You can implement those operations by yourself :) Fairly easy!

Implementation in C

/* Problem : Queue - Data Structure * Author : Stephanus * Lang. : C * Date : 20 January 2004 */ #include < stdio.h > #define QUEUE_SIZE 100 typedef struct { int q[QUEUE_SIZE]; int first,last; int count; } queue; void init_queue(queue *q) { q->first = 0; q->last = QUEUE_SIZE - 1; q->count = 0; } void enqueue(queue *q,int x) { q->last = (q->last + 1) % QUEUE_SIZE; q->q[ q->last ] = x; q->count = q->count + 1; } int dequeue(queue *q)

{ int x = q->q[ q->first ]; q->first = (q->first + 1) % QUEUE_SIZE; q->count = q->count - 1; return x; } int main() { queue q; init_queue(&q); enqueue(&q,1); enqueue(&q,2); enqueue(&q,3); while (q.count) printf("%d\n",dequeue(&q)); return 0; }

Implementation in C++
/* Problem : Queue - Data Structure * Author : Stephanus * Lang. : C++ * Date : 20 January 2004 */ #include < iostream > #define QUEUE_SIZE 100 using namespace std; class Queue { int q[QUEUE_SIZE]; int first,last; int count; public: Queue(); void enqueue(int x); int dequeue(); int getSize(); }; Queue::Queue() { first = 0; last = QUEUE_SIZE - 1; count = 0; } void Queue::enqueue(int x) { last = (last + 1) % QUEUE_SIZE; q[last] = x; count++; } int Queue::dequeue() { int x = q[first]; first = (first + 1) % QUEUE_SIZE; count--; return x; } int Queue::getSize() { return count; } int main() {

Queue q; q.enqueue(1); q.enqueue(2); q.enqueue(3); while (q.getSize()) cout << q.dequeue() << endl; return 0; }

Implementation in JAVA
/* Problem : Queue - Data Structure * Author : Stephanus * Lang. : JAVA * Date : 20 January 2004 */ class Queue { final int QUEUE_SIZE = 100; private int[] q = new int[QUEUE_SIZE]; private int first,last; private int count; Queue() { first = 0; last = QUEUE_SIZE - 1; count = 0; } public void enqueue(int x) { last = (last + 1) % QUEUE_SIZE; q[last] = x; count++; } public int dequeue() { int x = q[first]; first = (first + 1) % QUEUE_SIZE; count--; return x; } public int getSize() { return count; } } public class queue { public static void main(String[] args) { Queue q = new Queue(); q.enqueue(1); q.enqueue(2); q.enqueue(3); while (q.getSize() != 0) System.out.println(q.dequeue()); } }

You might also like