Title: Data structures and Algorithms
1Data structures and Algorithms
23.0 Queues and Lists
3Queue Definition
- A queue is an ordered collection of items from
which items may be deleted at one end ( called
front of the queue) and into which items may be
inserted at the other end (called the rear of the
queue)
rear
front
A
B
C
D
Removed
Inserted
4The queue data structure
- A queue is used in computing in much the same way
as it is used in every day life allow a sequence
of items to be processed on a first-come-first-ser
ved basis. - In most computer installations, for example, one
printer is connected to several different several
machines. So that more than one user can submit
printing jobs to the same printer,it maintains a
queue
5Basic array implementation of queues
rear
front
A
B
C
D
removal
insertion
6Queue initialisation
For an empty queue rear must be initialised to
front-1, Front0,rear1 and size0
Front,rear
7Queue implementation.
- The queueAr class has four data fields
- A dynamically expanding array
- The number of items currently in the
- queue
- The array index of the front item
- The array index of the rear item
8Public queue operations
- // void enqueue(x) gt Insert x
- //object getfront() -gt Return leased recently
- inserted item
- //object dequeue() -gt Return and remove
- leased recently inserted item
- //Boolean isempty() -gtReturn true if empty, false
- otherwise
- //void makeEmpty()-gtRemove all items
9Array implementation of queues
Eg
Size0,front0,rear-1
Enqueue(A)
A
Size1,front0,rear0
10Enqueue(B)
A
B
Size2,front0,rear1
Dequeue()
B
Size1,front1,rear1
11Dequeue
Size0,front2,rear1
After 7 Enqueus
C
D
E
F
G
H
I
Size7,front2,rear8
12We cannot add any more items even through the
queue is not full
- Array doubling does not solve the problem. This
is because even if the size of the array is
1000,after 1000 operations enqueue there is no
room in the queue, regardless of its actual size.
Even if 1000 dequeue operations has been
performed, thus abstractly making the queue is
empty,we cannot add to it.
13- There is plenty of extra space All the
positions before the front are unused and can
thus be recycled.When either rear or front
reaches the end of the array,we reset it to the
beginning. This operation is called a circular
array implementation.
14Circular array implementation
15Circular array implementation (Array
implementation of the queue with wraparound)
After 7 Enqueus
C
D
E
F
G
H
I
Size7,front2,rear8
Enqueus(J)
C
D
E
F
G
H
I
J
Size8,front2,rear0
16Implementation of array based Public queue
operations
- Data structure definition
- private Object thearray
- private int currentsize
- private int front
- private int rear
- static final int DEFAULT_CAPACITY10//(example)
17Constructor for array based Queue
- // Construct the Queue.
- Public QueueAr( )
-
- thearraynew object DEFAULT_CAPACITY
- makeEmpty( )
-
18makeEmpty routine for the array based queue class.
- //make the queue logically empty
- Public void makeEmpty( )
-
- currentSize0
- Front0
- Rear-1
-
19Enqueue for the array-based queue class
- //Insert a new item(x) into the queue
- Public void enqueue(Object x)
-
- If(currentSize thearray.length)
- Doublequeue( )
- rearincrement(rear)
- theArrayrearx
- currentsize
-
20Wraparound routine(increment)
- //internal method to increment with wraparound
- Private int increment( int x)
-
- If (xtheArray.length)
- x0
- return x
21Wraparound routine(increment)
- The increment routine adds 1 to its parameter and
returns the new value. Since it implements
wraparound, if the result would equal the array
size it is wraparound to zero.
22Dequeue for array based queue class
- //Return and remove the least recently inserted
item from the queue - //exception underflow if the queue is empty.
- Public Object dequeue( ) throws Underflow
-
- If(isEmpty( ) )
- throw new underflow (queue Dequeue)
23Dequeue for array based queue class
- currentszize--
- Object returnvaluetheArrayfront
- frontincrement(front)
- return returnvalue
24getfront for array based queue class
- //return the least recently inserted item in the
queue - //exception underflow if the queue is empty
- Public Object getfront( ) throws Underflow
-
- If(isEmpty( ))
- throw new Underflow (queue getfront)
- return theArrayfront
25Linked list representation of queues.
- The queue can be implemented by a linked list,
provided references are kept to both the front
and rear of the list.
26Example
front
B
A
C
D
E
rear
The queue is almost identical to the stack
routines. The queueLi maintains two references
such as front and rear.
27Public Queue Operations
- Public operations
- //void enqueue(x)- Insert x
- //Object getfront( )-Return least recently
inserted - item
- //object Dequeue( )-gt Return and remove least
- recent item
- //Boolean idEmpty( ) -gt Return true if empty,
false otherwise - //void MakeEmpty( )-gt Remove all items
28isEmpty( )
- public Boolean isEmpty( )
-
- return frontnull
29makempty()
- Public void makeEmpty( )
-
- frontbacknull
30Dequeue for the linked list based queue class
rear
front
frontfront.next
31Dequeue for the linked list based queue class
- Public Object dequeue( ) throws Underflow
-
- //return and remove the least recently inserted
item from the queue - //exception Underflow if the queue is empty
- If(isempty( )) throw new underflow (queue
dequeue) - Object returnvaluefront.element
- frontfront.next
- return returnvalue
-
32getfront for the linked list based queue class
- Public Object getfront( ) throws underflow
- //get the least recently inserted item in the
queue. Does not alter the queue - //exception underflow if queue is empty
- If(isempty( ) )
- Throw new underflow (queue getfront)
- Return front.element
-
33enqueue for the linked list based queue class
- Public void enqueue(Object x)
- //insert a new item into the queue
-
- If(isempty( ))
- frontrearnew ListNode(x)
- else
- rearrear.nextnew ListNode(x)
ExComparison of array based queue and linked
list based queues
34Linked list
- The linked list can be used to store items
non-contiguously. - In this section we will see
- How to access to any item using a general linked
list - The general algorithms used for the linked list
operations - Doubly linked list and circular linked list.
35ListNode declaration
- Class ListNode
-
- Object element
- ListNode next
-
36B
A
Fronttolist
C
D
E
The first node in the linked list is accessible
through by a reference,we can print or search in
the linked list by starting at the first item and
following the chain of the references. Insertion
and deletion can be performed arbitrary.
37For example, how to insert element x after item A
in the linked list
B
A
current
x
tmp
We must perform the following steps Tmpnew
ListNode( )// create a new node Tmp.elementx
//place x in the element field. Tmp.nextcurrent.n
ext // xs next node is B Current.nexttemp
//as next node is x
38Deletion from a linked list
B
x
A
current
The remove command can be executed in one
reference change. To remove element x from the
linked listwe set current to be the node prior
to x and then have currents next reference by
pass x.
current.nextcurrent.next.next
The list A,X,B now appear as A,B
39Header Nodes
- If you want to delete item x, then we set current
to be node prior to x and then have currents
next reference by pass x. - If you are trying to delete 1st element it
becomes a special case - Special cases are always problematic in algorithm
design and frequently leads to bugs in the code. - It is generally preferable to write code that
avoids special cases.
40- One way to do that here is to introduce the
header node - A header node is an extra node in the linked list
that holds no data serves to satisfies the
requirement that every node that contains an item
have a previous node in the list
41Example
B
A
Header
Moving to the front now means setting the current
position to header node,and so-on, with a header
node, a list is empty then header.next is null.
Ex implementation of primitive operations with a
header node.
42Doubly linked list and circular linked lists.
- There are two references one for the forward and
other one for the backward direction.We should
have not only a header but also a tail. -
header
If list is empty, then list consists of head and
tail connected together.
43Tail
Header
Empty doubly linked list
If doubly linked list is empty then
Header.nexttail Or Tail.prevHeader
44The doubly linked list class is shown below
- Class DoublyLinkedListNode
-
- Object data //some element
- DoublyLinkedListNode next
- DoublyLinkedListNode prev
-
45Insert operation
A
B
x
current
newnode
46Algorithm
- Newnodenew DoublyLinkedListNode(x)
- Newnode.prevcurrent
- Newnode.nextcurrent.next
- Newnode.prev.nextnewnode
- Newnode.next.prevnewnode
47Delete operation
Before deletion
B
A
X
current
After deletion
B
A
48Delete operation
- Current.prev.nextcurrent.next
- Current.next.prevcurrent.prev
- Currenthead
49Circular doubly linked list.
- A popular convention is to create a circular
doubly linked list , in which the last cell keeps
a reference back to the first and first cell
keeps a back reference to the last cell. - This can be done with or without a header.
50Circular doubly linked list
B
A
X
Ex implementation of circular doubly liked list
operations