• No results found

DSA 5.ppt

N/A
N/A
Protected

Academic year: 2020

Share "DSA 5.ppt"

Copied!
26
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Queue

A stack is LIFO (Last-In First Out) structure.

In contrast, a queue is a FIFO (First-In First-Out )

structure.

A queue is a linear structure for which items can

be only inserted at one end and removed at another end.

(3)

Queue Operations

The basic operations on a queue are enqueue, which inserts an element at the end of the list (called the rear), and dequeue, which

deletes (and returns) the element at the start of the list (known as the front).

Enqueue(X) – place X at the rear of the queue.

Dequeue( ) -- remove the front element and return it.

Front( ) -- return front element without removing it.

(4)

Implementing Queue

Using linked List: Recall

Insert works in constant time for either end of a

linked list.

Remove works in constant time only.

Seems best that head of the linked list be the front

of the queue so that all removes will be from the front.

(5)

Implementing Queue

Using linked List:

front

2 5 7

1 1 7 5 2

front

(6)

Implementing Queue

Using linked List:

front

2 5 7

1 1 7 5 2

front

rear rear

front

2 5

7 1 7 5 2

front

rear rear

(7)

Implementing Queue

Using linked List:

front

2 5 7

1 1 7 5 2

front

rear rear

front

2 5

7 7 5 2 9

front

rear rear

enqueue(9)

(8)

Implementing Queue

int dequeue() {

int x = front->get(); Node* p = front;

front = front->getNext();

delete p; return x; }

What is the value at front? save it in x

Pointer to front

(9)

Implementing Queue

void enqueue(int x) {

Node* newNode = new Node(); newNode->set(x);

newNode->setNext(NULL); rear->setNext(newNode); rear = newNode;

}

Create a new Node x is value of new node

Assign x to new Node

End of list

Update the rear

(10)

Implementing Queue

int front() {

return front->get(); }

int isEmpty() {

(11)

Queue using

Array

If we use an array to hold queue elements, both

insertions and removal at the front (start) of the array are expensive.

This is because we may have to shift up to “n”

elements.

For the stack, we needed only one end; for queue

we need both.

To get around this, we will not shift upon removal of

(12)

Queue using

Array

front

2 5 7 1

rear

6

5 7

0

0 1 2 3 4 front

1 7 5 2

(13)

Queue using

Array

front

2 5 7 1

rear

6

5 7

0

0 1 2 3 4 front

1 7 5 2

4 rear

enqueue(6)

(14)

Queue using

Array

front

2 5 7 1

rear

6

5 7

0

0 1 2 3 4 front

1 7 5 2

5 rear

enqueue(8)

(15)

Queue using

Array

front

2 5 7

rear

6

5 7

1

0 1 2 3 4 front

7 5 2

5 rear

dequeue()

(16)

Queue using

Array

front

2 5

rear

6

5 7

2

0 1 2 3 4 front

5 2

5 rear

dequeue()

(17)

Queue using

Array

front

2 5

rear

6

5 7

2

0 1 2 3 4 front

5 2

7 rear

enqueue(9) enqueue(12)

6 8 9 12 6 8 9 12

(18)

Queue using

Array

We have inserts and removal running in constant

time but we created a new problem.

Problem using array for queue implementation?

Cannot insert new elements even though there are

two places available at the start of the array.

Solution: allow the queue to “wrap around”.

front

2 5

rear

6

5 7

2

0 1 2 3 4 front

5 2

7 rear

(19)

Queue using

Array

Basic idea is to picture the array as a circular array.

front 2 5 rear 2 front 7 rear 6 8 9 12

(20)

Queue using

Array/Circular Array

void enqueue(int x) {

rear = (rear+1)% size; array[rear] = x;

noElements = noElements + 1; } front 2 5 rear 2 front 0 rear 6 8 9 12

(21)

Queue using Array

int isFull() {

return noElements == size; }

int isEmpty() {

return noElements == 0; } front 2 5 rear 2 front 1 rear 6 8 9 12

(22)

Queue using Array

int dequeue() {

int x = array[front];

front = (front+1)% size;

noElements = noElements - 1; return x; } front rear 4 front 1 rear 6 8 9 12

(23)

Use of Queues

Simulation

Out of the numerous uses of the queues, one of the most useful

is simulation.

A simulation program attempts to model a real-world

phenomenon.

Many popular video games are simulations, e.g., SimCity,

FlightSimulator

Each object and action in the simulation has a counterpart in

real world.

(24)

Uses of Queues

If the simulation is accurate, the result of the program

should mirror the results of the real-world event.

Thus it is possible to understand what occurs in the

real-world without actually observing its occurrence. Printer:

(25)

Uses of Queues

Real-life: Virtually every real-life line is (supposed to be) a queue. For instance, lines at ticket counters are queues, because service is first-come first-served.

Computer Networks: There are many network setups of personal computers in which the disk is attached to

one machine, known as the file server. Users on other

machines are given access to files on a come first-served basis, so the data structure is a queue.

(26)

Home Work: Lecture Book

Simulation of a Bank

A customer enters the bank at a specific time (t1) desiring to

conduct a transaction.

Any one of the four tellers can attend to the customer.

The transaction (withdraw, deposit) will take a certain period of time (t2).

If a teller is free, the teller can process the customer’s

References

Related documents

• Excellent mechanical stability and corrosion protection • Excellent oxidation stability, helping extend grease life • Excellent extreme pressure, anti-rust and water

Fluorophores at intermediate distances (~10 – 15 nm) from the metal layer have shown a coupling of excitation energy with the plasmonic states of the metal, resulting

In this study it is aimed to investigate the total lipid and the fatty acid composition of pearl mullet which is the only fish species occurring in Lake Van and

A basic task in sentiment analysis is classifying the polarity of a given text at the document, sentence, or feature/aspect level—whether the expressed opinion in

Berdasarkan penjelasan diatas bahwa tidak sesuai dengan teori yang sudah dikemukakan oleh keown et al (2001 :157) “bahwa apabila perputaran piutang dalam suatu perusahaan dalam

National Conference on Technical Vocational Education, Training and Skills Development: A Roadmap for Empowerment (Dec. 2008): Ministry of Human Resource Development, Department

However, obtaining bacterial genomic information is not always trivial: the target bacteria may be difficult-to-culture or uncultured, and may be found within samples containing

than coarse, punctures scattered between the setose punctures of the pronotum (Fig. 1) Ventral view of left hindleg of Parochodaeus biarmatus. Arrow indicating toothed