Sunteți pe pagina 1din 3

Lecture Notes: 7

Introduction A queue is a particular kind of collection in which the entities in the collection are kept in order of entry and the principal operations on the collection are the addition and removal of entities at the terminal positions. In these contexts, the queue performs the function of a buffer. The most well known operation of the queue is the First-In-First-Out process. In a FIFO queue, the first element in the queue will be the first one out; this is equivalent to the requirement that whenever an element is added, all elements that were added before have to be removed before the new element can be invoked. There are two basic operations associated with a queue: enqueue and dequeue. Enqueue means adding a new item to the rear of the queue while dequeue refers to removing the front item from the queue and returning it to the calling entity. Real world Queue

Features of a queue: 1. 2. 3. 4. A list structure with two access points called the front and rear. All insertions (enqueue) occur at the rear and deletions (dequeue) occur at the front. Homogeneous components Has a First-In, First-Out characteristic (FIFO)

Implementation

DS.H
#include<iostream.h> #include<conio.h> #include<stdlib.h> #define MaxSize 10 class Queue { private: int Array[MaxSize]; int Front, Rear;

Notes Compiled for III Sem B.Tech CSE C & D batch

Page 1

Lecture Notes: 7

public: Queue() { Front = 0; Rear = -1; } void Enqueue(int); int Dequeue(); void Display(); }; void Queue::Enqueue(int Data) { if(Rear < MaxSize-1) Array[++Rear] = Data; else { cout<<"\n\t Queue is full..."; getch(); } } int Queue::Dequeue() { if(Front<=Rear) return Array[Front++]; else return -1; } void Queue::Display() { if(Front>Rear) cout<<"\n\tQueue Empty..."; else { cout<<"\n\tElements in Queue are : \n\n"; for(int i=Front;i<=Rear;i++) cout<<"\t"<<Array[i]<<"\n"; } }

Queue.cpp
#include "DS.h" void main() { Queue s; int Choice,Data; do{
Notes Compiled for III Sem B.Tech CSE C & D batch

Page 2

Lecture Notes: 7

clrscr();
cout<<"Menu\n~~~~\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n\tYour choice: ";

cin>>Choice; switch(Choice) { case 1: cout<<"\nEnter the data to Enqueue: "; cin>>Data; s.Enqueue(Data); break; case 2: int Res = s.Dequeue(); if(Res>=0) cout<<"\n\t"<<Res<<" Dequeued"; else cout<<"\n\tQueue empty..."; getch(); break; case 3: s.Display(); getch(); break; case 4: cout<<"\n\tApplication closing.. \n\tPress any key to exit..."; getch(); break; default: cout<<"\n\tError in choice...\n\tEnter correct choice.."; getch(); break; } }while(Choice!=4); } A practical application of the Queue class A queue is a useful data structure for holding data which should be processed in the order it is created, but which cannot always be processed straight away. A typical application might be a messaging system. In the following example, messages are received in the order they were sent. The classes involved are Message, MessageSender and MessageReceiver: A Message object has a sender, a recipient, a content string and a date. A Message is placed in a Queue by a MessageSender object. A Message is removed from the queue by a MessageReceiver object, which can also display the contents of the Queue.

Notes Compiled for III Sem B.Tech CSE C & D batch

Page 3

S-ar putea să vă placă și