Lec3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Data Structure

Control & Systems/ Fourth Class

Lecture 3: Queue and its applications


A queue is a data structure that is best described as "first in, first out". A real world example of a queue is people waiting in line at the bank. As each person enters the bank, he or she is "enqueued" at the back of the line. When a teller becomes available, they are "dequeued" at the front of the line. Perhaps the most common use of a queue within a TopCoder problem is to implement a Breadth First Search (BFS). BFS means to first explore all states that can be reached in one step, then all states that can be reached in two steps, etc. A queue assists in implementing this solution because it stores a list of all state spaces that have been visited.

A queue implementation may be as simple as an array, and a pointer to the current position within the array. For instance, if you know that you are trying to get from point A to point B on a 50x50 grid, and have determined that the direction you are facing (or any other details) are not relevant, then you know that there are no more than 2,500 "states" to visit. Thus, your queue is programmed like so:

17

Data Structure

Control & Systems/ Fourth Class

18

Data Structure

Control & Systems/ Fourth Class

#include<iostream.h> #include<stdlib.h> const int size=5; typedef int ItemType; class QueueType { public: QueueType (); void Enqueue (ItemType item); ItemType Dequeue() ; bool IsFull(); bool IsEmpty(); private: int f, r ; ItemType q[size]; }; QueueType::QueueType()

19

Data Structure { r=f=-1;} bool QueueType::IsFull() { return( (r==size-1));} bool QueueType::IsEmpty() { return( (f == -1));} void QueueType::Enqueue(ItemType item) { if (IsFull()) { cout<<"error.......the Queue is full"; exit(0); } else { if(f==-1) f=f+1; r=r+1; q[r]=item; } } ItemType QueueType::Dequeue() { int item; if( IsEmpty()) { cout<<"Error......the is empty"; exit(0); } else { item=q[f]; f=f+1; } if(f==size) { f =-1; r =-1; } return item;} void main() { QueueType queue; ItemType x; for( int i=0;i<size;i++) { cin>>x; queue.Enqueue(x); } for( i=0;i<size;i++) cout<<" "<<queue.Dequeue(); }

Control & Systems/ Fourth Class

20

Data Structure

Control & Systems/ Fourth Class

21

Data Structure

Control & Systems/ Fourth Class

22

Data Structure

Control & Systems/ Fourth Class

Circular Queue

23

Data Structure

Control & Systems/ Fourth Class

24

Data Structure

Control & Systems/ Fourth Class

/* Circular Queues */ #include<iostream.h> #include<conio.h> const int MAX = 5; class cqueue { int a[MAX],front,rear; public : cqueue() { front=rear=-1; } void insert(int ); int deletion(); void display(); }; void cqueue :: insert(int val) { if((front==0 && rear==MAX-1) || (rear+1==front)) cout<<" Circular Queue is Full"; else { if(rear==MAX-1) rear=0; else rear++; a[rear]=val; } if(front==-1) front=0; } int cqueue :: deletion() { int k; if(front==-1) cout<<"Circular Queue is Empty"; else { k=a[front]; if(front==rear) front=rear=-1; else { if(front==MAX-1) front=0; else

25

Data Structure front++;

Control & Systems/ Fourth Class

} } return k; } void cqueue :: display() { int i; if(front==-1) cout<<"Circular Queue is Empty"; else { if(rear < front) { for(i=front; i<=MAX-1;i++) cout<<a[i]<<" "; for(i=0;i<=rear;i++) cout<<a[i]<<" "; } else { for(i=front;i<=rear;i++) cout<<a[i]<<" "; cout<<endl; } }} void main() { cqueue c1; int ch,val; char op; do { // clrscr(); cout<<"-----------Menu-------------"<<endl; cout<<"1.Insertion"<<"\n"<<"2.Deletion"<<"\n"<<"3.Display"<<"\n"<<"4.Exit "; cout<<"Enter Your Choice <1..4> ?"; cin>>ch; switch(ch) { case 1 : cout<<"Enter Element to Insert ?"; cin>>val; c1.insert(val); break; case 2 : val=c1.deletion(); cout<<"Deleted Element :"<<val<<endl; break; case 3 : c1.display(); break; } cout<<"Do you want to continue<Y/N> ?"; cin>>op; }while(op=='Y' || op=='y'); getch(); }

26

You might also like