• Tidak ada hasil yang ditemukan

Lecture 8

N/A
N/A
Protected

Academic year: 2025

Membagikan "Lecture 8"

Copied!
24
0
0

Teks penuh

(1)

Lecture 8

Queues and Deques

Slides modified from © 2010 Goodrich, Tamassia

(2)

Queues

First-in First-out

(3)

Main Operations

1.   enqueue (object) at the rear 2.   dequeue (object) from front

rear

front

(4)

Auxiliary Operations

•  object front():

•   integer size():

•   boolean empty():

(5)

Queue Interface in C++

template <typename E>

class Queue { public:

int size() const;

bool empty() const;

const E& front() const;

void enqueue (const E& e);

void dequeue()

};

(6)

Operation Output Queue

enqueue(5) – (5) enqueue(3) – (5, 3) enqueue(4) – (5, 3, 4) dequeue() – (3, 4)

enqueue(7) – (3, 4, 7) dequeue() – (4, 7)

front() 4 (4, 7) size() 2 (4, 7) dequeue() – (7) dequeue() – ()

empty() true ()

(7)

Applications

•  Direct applications

–  shared resources (e.g., printer) –  multi-threaded programming

•  Indirect applications

–  auxiliary data structure for algorithms

(8)

Queue

Implementation

(9)

Array-based Implementation

Q 0 1 2 f r

normal configuration

f index of the front element

r index immediately past the rear element

n number of items in the queue

(10)

Wrapped-around Configuration

Q 0 1 2 r f

f index of the front element

r index immediately past the rear element

n number of items in the queue

(11)

enqueue()

Algorithm enqueue(e) Q[r] ← e

r ← (r + 1) mod N n ← n + 1

Q 0 1 2 r f

Q 0 1 2 f r

(12)

dequeue()

Algorithm dequeue() f ← (f + 1) mod N n ← n - 1

Q 0 1 2 f r

Q 0 1 2 r f

(13)

size() and empty()

Algorithm size() return n

Algorithm empty() return (n == 0)

Q 0 1 2 f r

Q 0 1 2 r f

(14)

Array-based Queue Limitation

-fixed size!

(15)

Linked list

Implementation of

Queues

(16)

Circular Linked List for Queue

“main” — 2011/1/13 — 9:10 — page 214 — #236

214 Chapter 5. Stacks, Queues, and Deques

as the front of the queue and the front of the circular list as the rear of the queue?) Also recall that CircleList supports the following modifier functions. The func- tion add inserts a new node just after the cursor, the function remove removes the node immediately following the cursor, and the function advance moves the cursor forward to the next node of the circular list.

In order to implement the queue operation enqueue, we first invoke the function add, which inserts a new element just after the cursor, that is, just after the rear of the queue. We then invoke advance , which advances the cursor to this new element, thus making the new node the rear of the queue. The process is illustrated in Figure 5.5.

LAX MSP ATL

(front) (rear) cursor

(a)

LAX MSP ATL BOS

(front) (rear) cursor

(b)

LAX MSP ATL BOS

(front) (rear) cursor

(c)

Figure 5.5: Enqueueing “BOS” into a queue represented as a circularly linked list:

(a) before the operation; (b) after adding the new node; (c) after advancing the cursor.

In order to implement the queue operation dequeue , we invoke the function remove , thus removing the node just after the cursor, that is, the front of the queue.

The process is illustrated in Figure 5.6.

The class structure for the resulting class, called LinkedQueue , is shown in Code Fragment 5.18. To avoid the syntactic messiness inherent in C++ templated classes, we have chosen not to implement a fully generic templated class. Instead, we have opted to define a type for the queue’s elements, called Elem . In this ex- ample, we define Elem to be of type string . The queue is stored in the circular list

Queue front back

Circular List

front è

rear è

(17)

“main” — 2011/1/13 — 9:10 — page 214 — #236

214 Chapter 5. Stacks, Queues, and Deques

as the front of the queue and the front of the circular list as the rear of the queue?) Also recall that CircleList supports the following modifier functions. The func- tion add inserts a new node just after the cursor, the function remove removes the node immediately following the cursor, and the function advance moves the cursor forward to the next node of the circular list.

In order to implement the queue operation enqueue, we first invoke the function add, which inserts a new element just after the cursor, that is, just after the rear of the queue. We then invoke advance, which advances the cursor to this new element, thus making the new node the rear of the queue. The process is illustrated in Figure 5.5.

LAX MSP ATL

(front) (rear) cursor

(a)

LAX MSP ATL BOS

(front) (rear) cursor

(b)

LAX MSP ATL BOS

(front) (rear) cursor

(c)

Figure 5.5: Enqueueing “BOS” into a queue represented as a circularly linked list:

(a) before the operation; (b) after adding the new node; (c) after advancing the cursor.

In order to implement the queue operation dequeue, we invoke the function remove, thus removing the node just after the cursor, that is, the front of the queue.

The process is illustrated in Figure 5.6.

The class structure for the resulting class, called LinkedQueue, is shown in Code Fragment 5.18. To avoid the syntactic messiness inherent in C++ templated classes, we have chosen not to implement a fully generic templated class. Instead, we have opted to define a type for the queue’s elements, called Elem. In this ex- ample, we define Elem to be of type string. The queue is stored in the circular list

Enqueue

(18)

Dequeue

“main” — 2011/1/13 — 9:10 — page 215 — #237

5.2. Queues 215

LAX MSP ATL BOS

(front) (rear) cursor

(a)

MSP ATL BOS

(front) (rear) cursor

LAX

(b)

Figure 5.6: Dequeueing an element (in this case “LAX”) from the front queue rep- resented as a circularly linked list: (a) before the operation; (b) after removing the node immediately following the cursor.

data structure C. In order to support the size function (which CircleList does not provide), we also maintain the queue size in the member n.

typedef string Elem; // queue element type

class LinkedQueue { // queue as doubly linked list public :

LinkedQueue(); // constructor

int size() const; // number of items in the queue

bool empty() const; // is the queue empty?

const Elem& front() const throw(QueueEmpty); // the front element void enqueue(const Elem& e); // enqueue element at rear void dequeue() throw(QueueEmpty); // dequeue element at front

private: // member data

CircleList C; // circular list of elements

int n; // number of elements

} ;

Code Fragment 5.18: The class LinkedQueue, an implementation of a queue based on a circularly linked list.

In Code Fragment 5.19, we present the implementations of the constructor and

the basic accessor functions, size, empty, and front. Our constructor creates the

initial queue and initializes n to zero. We do not provide an explicit destructor,

relying instead on the destructor provided by CircleList. Observe that the func-

tion front throws an exception if an attempt is made to access the first element of

an empty queue. Otherwise, it returns the element referenced by the front of the

circular list, which, by our convention, is also the front element of the queue.

(19)

Round Robin Schedulers

Repeat 1-3

1.   e = Q.front(); Q.dequeue() 2.   Service element e

3.   Q.enqueue(e)

Shared Service

Queue

Enqueue

Dequeue

(20)

Double-Ended Queue : Deque

back front

(21)

Fundamental operations

–   InsertFirst(e) –  InsertLast(e) –  RemoveFirst() –   RemoveLast() –   First()

–   Last()

(22)

Deque implementations

•  Doubly linked list

Tail

A B C

•  Singly linked list

(23)

Maximum of all

subarrays of size k

•  Input: [ 9 0 8 1 5 7 19 21 3 64 18]

•  Output:[9 8 8 19 21 21 64 64]

(24)

D = (1,2,3,4,5,6,7,8) Q = ()

Change D to

D = (1,2,3,5,4,6,7,8)

Referensi

Dokumen terkait

Today, identifiable front fenders live on in all body configurations, but rear fenders only in three-box saloons: in hatchbacks, the panel which accommodates the rear wheel

And each element of this matrix would be a polynomial inY of degree at most n wheren= degXf.Therefore, the degree of the resultant polynomial is at most2mn+ 2.And if2kis larger than2mn+

A Hotchkiss front suspension with pitman arm, drag link steering is modeled in ADAMS using hard points at flat spring condition and the leaf spring model.. All necessary forces and

Now, if looked at from the perspective of very serious and heinous offences, like the Delhi gang rape case, people may feel that it is better to inflict such retributive punishments, so

When a part of the structure has crossed the void and the number of robots at the finish area again satisfies the structural stability, the front robot detaches itself from the

For example: when an individual takes birth, its body parts are the immense element which is being formed before taking the birth, similarly, when a company is formed, before that it

charge density: σM = -∂γ/∂Eμ Surface tension γ = ∂G/∂A: a measure of the energy required to produce a unit area of new surface Electrochemical potential μi = ∂G/∂ni: const at

For each model, record for each frequency step the free space forward gain, the rear gain, the 180E front-to-back ratio, the source impedance, and the 50- VSWR.. Use the table at the