0% found this document useful (0 votes)
43 views

Ueues and Inked Ists: - Queues - Linked Lists - Double-Ended Queues

The document discusses queues, linked lists, and double-ended queues (deques). It describes how queues follow a FIFO principle with elements inserted at the rear and removed from the front. Linked lists can implement queues by adding elements to the tail and removing from the head. Deques support insertion and removal from both ends. The document shows how to implement stacks and queues using deques through adapter patterns. Deques can be implemented efficiently using doubly linked lists.

Uploaded by

Albert Pena
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Ueues and Inked Ists: - Queues - Linked Lists - Double-Ended Queues

The document discusses queues, linked lists, and double-ended queues (deques). It describes how queues follow a FIFO principle with elements inserted at the rear and removed from the front. Linked lists can implement queues by adding elements to the tail and removing from the head. Deques support insertion and removal from both ends. The document shows how to implement stacks and queues using deques through adapter patterns. Deques can be implemented efficiently using doubly linked lists.

Uploaded by

Albert Pena
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

QUEUES AND LINKED LISTS

Queues Linked Lists Double-Ended Queues

head

tail

Rome Seattle Toronto Zurich

Queues and Linked Lists

Queues
A queue differs from a stack in that its insertion and removal routines follows the rst-in-rst-out (FIFO) principle. Elements may be inserted at any time, but only the element which has been in the queue the longest may be removed. Elements are inserted at the rear (enqueued) and removed from the front (dequeued)

front a0 a1 a2

rear

...

an-1

Queues and Linked Lists

The Queue Abstract Data Type


The queue has two fundamental methods: - enqueue(o): Insert object o at the rear of the queue

- dequeue(): Remove the object from the front of the queue and return it; an error occurs if the queue is empty These support methods should also be dened: - size(): Return the number of objects in the queue

- isEmpty(): Return a boolean value that indicates whether the queue is empty

- front():

Return, but do not remove, the front object in the queue; an error occurs if the queue is empty

Queues and Linked Lists

An Array-Based Queue
Create a queue using an array in a circular fashion A maximum size N is specied, e.g. N = 1,000. The queue consists of an N-element array Q and two integer variables: - f, index of the front element - r, index of the element after the rear one normal conguration
Q 0 1 2

... f r
N1

wrapped around conguration ...


0 1 2

N1

What does f=r mean? How do we compute the number of elements in the queue from f and r?

Queues and Linked Lists

An Array-Based Queue (contd.)


Pseudo-Code (contd.)
Algorithm size(): return (N - f + r) mod N Algorithm isEmpty(): return (f = r) Algorithm front(): if isEmpty() then throw a QueueEmptyException return Q[f] Algorithm dequeue(): if isEmpty() then throw a QueueEmptyException temp Q[f] Q[f] null f (f + 1) mod N return temp Algorithm enqueue(o): if size = N - 1 then throw a QueueFullException Q[r] o
Queues and Linked Lists 5

Implementing a Queue with a Singly Linked List


nodes connected in a chain by links
head tail

Rome Seattle Toronto

the head of the list is the front of the queue, the tail of the list is the rear of the queue why not the opposite?

Queues and Linked Lists

Removing at the Head


head tail

Baltimore Rome Seattle Toronto

advance head reference


head tail

Baltimore Rome Seattle Toronto

inserting at the head is just as easy

Queues and Linked Lists

Inserting at the Tail


create a new node
head tail

Rome Seattle Toronto Zurich

chain it and move the tail reference


head tail

Rome Seattle Toronto Zurich

how about removing at the tail?

Queues and Linked Lists

Double-Ended Queues
A double-ended queue, or deque, supports insertion and deletion from the front and back. The Deque Abstract Data Type - insertFirst(e): Insert e at the deginning of deque. - insertLast(e): Insert e at end of deque - removeFirst(): Removes and returns rst element - removeLast(): Removes and returns last element Additionally supported methods include: - rst() - last() - size() - isEmpty()

Queues and Linked Lists

Implementing Stacks and Queues with Deques


Stacks with Deques:
Stack Method size() isEmpty() top() push(e) pop() Deque Implementation size() isEmpty() last() insertLast(e) removeLast()

Queues with Deques:


Queue Method size() isEmpty() front() enqueue() dequeue() Deque Implementation size() isEmpty() rst() insertLast(e) removeFirst()

Queues and Linked Lists

10

The Adaptor Pattern


Using a deque to implement a stack or queue is an example of the adaptor pattern. Adaptor patterns implement a class by using methods of another class In general, adaptor classes specialize general classes Two such applications: - Specialize a general class by changing some methods. Ex: implementing a stack with a deque. - Specialize the types of objects used by a general class. Ex: Dening an IntegerArrayStack class that adapts ArrayStack to only store integers.

Queues and Linked Lists

11

Implementing Deques with Doubly Linked Lists


Deletions at the tail of a singly linked list cannot be done in constant time. To implement a deque, we use a doubly linked list. with special header and trailer nodes.
header trailer

Baltimore

New York

Providence

A node of a doubly linked list has a next and a prev link. It supports the following methods: - setElement(Object e) - setNext(Object newNext) - setPrev(Object newPrev) - getElement() - getNext() - getPrev() By using a doubly linked list, all the methods of a deque run in O(1) time.
Queues and Linked Lists 12

Implementing Deques with Doubly Linked Lists (cont.)


When implementing a doubly linked lists, we add two special nodes to the ends of the lists: the header and trailer nodes. - The header node goes before the rst list element. It has a valid next link but a null prev link. - The trailer node goes after the last element. It has a valid prev reference but a null next reference. The header and trailer nodes are sentinel or dummy nodes because they do not store elements. Heres a diagram of our doubly linked list:
header trailer

Baltimore

New York

Providence

Queues and Linked Lists

13

Implementing Deques with Doubly Linked Lists (cont.)


Heres a visualization of the code for removeLast().
header secondtolast last trailer

Baltimore

New York

Providence

San Francisco

last header secondtolast trailer

Baltimore

New York

Providence

San Francisco

header

trailer

Baltimore

New York

Providence

Queues and Linked Lists

14

You might also like