Title: CS5539 Data Structures and Algorithms
1CS5539Data Structures and Algorithms
- Lecture 15
- Linear Structures
2Queues
- A queue is a sequence of elements where
- Nodes can only be removed from the head/front
- Nodes can only be added to the tail/end
- A queue is thus a first-in, first-out structure
(FIFO) - A queue has a length
- the number of elements it contains
- If a queue is empty it has length zero
3Reading
-
- Watt and Brown Chapter 7 Queues
- Watt and Brown Chapter 8 Lists
4Queue Behaviour
no queue
front back 0 elements
Fred
Fred joins queue
front back Fred 1 element
Joe joins queue
front Fred back Joe 2 elements
Harry joins queue
front Fred back Harry 3 elements
Fred catches bus
front Joe back Harry 2 elements
Barney joins queue
front Joe back Barney 3 elements
5Typical Operations
- OPERATION PRE-CONDITION POST-CONDITION
- append (Object item)q not full queue1,
- item added at end queue
- remove() q not empty queue 1, item at
front removed - front() q not empty queue same
- isEmpty() none queue same
- back() q not empty queue same
- isFull() none queue same
6Specification of a Queue ADT
- Associated ADTs Boolean, Element
- Operations
- create Queue
- append Element Queue Queue
- front Queue Element
- remove Queue Queue
- isEmpty Queue Boolean
7Specification of a Queue ADT
- Requirements
- isEmpty(create) true
- For every x,q isEmpty(append(x,q)) false
- front(append(x,create)) x
- For every x,q!create front(append(x,q))
front(q) - remove(append(x,create)) create
- For every x,q!create remove(append(x,q))
append(x,remove(q))
- Exceptions
- front(create) is illegal
- remove(create) is illegal
8Linked List Implementation
- public class Queue
- private class SLLnode
- Object element
- SLLnode succ
- private SLLnode(Object element, SLLnode succ)
- this.element element
- this.succ succ
-
-
- private SLLnode front, rear
- // The methods go here ...
-
9Array Implementation
- public class Queue
- private Object items
- private int length, front, back
- public queue (int maxsize)
- items new Object maxsize
- length 0
- front 0
- rear 0
10Methods (Array Implementation)
- public void append(Object item)
- if (length items.length) makeLonger()
- itemsrear item
- if (rear items.length)
- rear 0
- length
11Methods (Array Implementation)
- public Object remove()
- if (length 0)
- throw new NoSuchElementException()
- Object firstItem itemsfront
- itemsfront null
- if (front items.length)
- front 0
- length--
- return firstItem
12Methods (Array Implementation)
- public Object front()
- if (length 0)
- throw new NoSuchElementException()
- return itemsfront
-
- public boolean isEmpty()
- return (length 0)
13Array Implementation
14Lists
- Operations on a list vary according to
application - There may be more than one operation of the same
name but with different number and types of
parameters - eg
- adding a new item at a specific position
- this needs 2 parameters position and element
- adding a new item at the end
- this needs 1 parameter element
15Lists Implementation
- Array
- Means maximum size bounded list
- Linked List
- Any size unbounded list but requires links to
both first and last elements
16Time Complexities and Implementations
Array O(1) O(1) O(1) O(n) O(n)
Linked list O(n) O(n) O(1) O(n) O(n)
- Operation
- get
- set
- add at end
- addanywhere
- remove