Learning Computer Programming: Wednesday, July 04, 2007
Learning Computer Programming: Wednesday, July 04, 2007
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.
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;
} } }
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()); } }