project ds

9
Introduction Linear Queue A linear queue models the FIFO (first in first out) data structure, much like a line in real life. The first person in line will be the first person served, in queues the first element to be added is the first that can be removed. The only adding point is to the end of the list and the only removal point is the beginning of the list. The defining attribute of a queue data structure is the fact that allows access to only the front and back of the structure. Furthermore, elements can only be removed from the front and can only be added to the back. Figure 1: Concept for linear queue. Our concept for linear queue is a patient waiting in queue to assign an appointment with the doctor. Only ten persons can assign an appointment in every session. In this case, the patient at the front of the queue was the first one to enter, while at the end of the queue is the last to have entered. Every time a patient finishes an appointment with the doctor are removed from the assembly queue, that person leaves the queue from the front. This represents the queue “dequeue” function. Every time person enters the line to wait, they join the end of the line and represent the “enqueue” function. The queue “size” function would return the length of the line, and the 1

Upload: radensue-raden-abd-muin

Post on 12-May-2017

215 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Project DS

Introduction

Linear Queue

A linear queue models the FIFO (first in first out) data structure, much like a line in real life. The first person in line will be the first person served, in queues the first element to be added is the first that can be removed. The only adding point is to the end of the list and the only removal point is the beginning of the list. The defining attribute of a queue data structure is the fact that allows access to only the front and back of the structure. Furthermore, elements can only be removed from the front and can only be added to the back.

Figure 1: Concept for linear queue.

Our concept for linear queue is a patient waiting in queue to assign an appointment with the doctor. Only ten persons can assign an appointment in every session. In this case, the patient at the front of the queue was the first one to enter, while at the end of the queue is the last to have entered. Every time a patient finishes an appointment with the doctor are removed from the assembly queue, that person leaves the queue from the front. This represents the queue “dequeue” function. Every time person enters the line to wait, they join the end of the line and represent the “enqueue” function. The queue “size” function would return the length of the line, and the “empty” function would return true only if there was nothing in the line.

1

Page 2: Project DS

Algorithm to Add an Element

case 1:if(alq.isFull())cout<<"The queue is full"<<endl;else{cout<<"Please enter the element"<<endl;cin.ignore();getline(cin,e);alq.enQueue(e);}

Algorithm to Delete an Element

case 2: if(alq.deQueue()){cout<< e <<" is dequeued"<<endl;}elsecout<<"queue is empty"<<endl;break;

Representation of Linear Queue

q[0] q[1] q[2]

SUSAN

ANNA

JAY

Delete Insert

Front Rear

Items

Figure 2: Representation of Linear Queue

2

Page 3: Project DS

How the algorithm applied in Linear Queue

0 1 2 3 4

f r

Figure 3: Insert Operation

8 0 1 2 3 4

f r

Figure 4: Insert Operation

8 4 0 1 2 3 4

f r

Figure 5: Insert Operation

3

f r Item Q[r]0 -1 - -

f r Item Q[r]

00

-10

-8

-8

f r Item Q[r]

000

-101

-84

-84

Page 4: Project DS

8 4 5 0 1 2 3 4

f r

Figure 6: Insert Operation

8 4 5 7 0 1 2 3 4

r

f

Figure 7: Insert Operation

8 4 5 7 9 0 1 2 3 4

f r

Figure 8: Queue Full

Summary4

f r Item Q[r]00000

-1012

-845

-845

f r Item Q[r]

00000

-10123

-8457

-8457

f r Item Q[r]000000

-101234

-84579

-84579

Page 5: Project DS

Like stacks, queues have many applications. Items enter a queue at the rear and leave a queue at the front. Queues can be implemented using and array or using a linked list.

Appendix

Source Code

#include<iostream>#include<string>using namespace std;

//array queuetemplate<class T>class ArrayLinearQueue{T * queue; //array queue of type Tint size,front,rear;

public:ArrayLinearQueue(int x=10){size = x;front=0;rear=-1;queue = new T[size];}

bool isEmpty(){return (front==rear);}

bool isFull(){return (rear==(size-1));}

bool enQueue(T e){if(isFull()) return false;

queue[++rear]=e;return true;}

bool deQueue(){if(isEmpty()) return false;front++;return true;}

T Front(){return queue[front];}

T Rear(){return queue[rear];}

void display(){5

Page 6: Project DS

for(int i=front;i<=rear;i++){cout<<queue[i]<<" <-- ";}cout<<endl;}};

main(){ArrayLinearQueue<string> alq;int ch;string e; cout<<"\nOnly 10 person allowed to waiting queue in every session. "<<endl; do{cout<<"\n\nEnter your choice:\n\n1.Enter the name on the list.\n2.Remove the names that have received treatment.\n3.Display the name still on the waiting list."<<endl;cin>>ch;switch(ch){case 1:if(alq.isFull())cout<<"\nThe queue is full"<<endl;else{cout<<"\nPlease enter the name of patient."<<endl;cin.ignore();getline(cin,e);alq.enQueue(e);}break;case 2: e = alq.Front();if(alq.deQueue()){cout<< e <<" had received treatment."<<endl;}elsecout<<"queue is empty"<<endl;break;case 3:

alq.display();break;// case 0: exit(0);}}while(ch);}

Output

6

Page 7: Project DS

7