Title: CSCI 240 Abstract Data Types
1Department of Computer and Information
Science,School of Science, IUPUI
CSCI 240
Abstract Data Types
Queues
Dale Roberts, Lecturer IUPUI droberts_at_cs.iupui.edu
2Queues
- Queue
- Similar to a supermarket checkout line
- First-in, first-out (FIFO)
- Nodes are removed only from the head
- Nodes are inserted only at the tail
- Insert and remove operations
- Enqueue (insert) and dequeue (remove)
- Queue Model
- queue is a list, with insertion done only at one
end and deletion done at the other end. - Linked list implementation of queues
- operating as a list
- constant time for enqueue dequeue (keeping
pointer to both the head and tail of the list)
3DYNAMICALLY LINKED STACKS AND QUEUES
Figure 4.10 Linked Stack and queue (p.147)
4FIFO queue ADT interface
- template ltclass Itemgt
- class QUEUE
-
- private
- // Implementation-dependent code
- public
- QUEUE(int)
- int empty()
- void put(Item)
- Item get()
-
5FIFO queue linked-list implementation
- template ltclass Itemgt
- class QUEUE
-
- private
- struct node
- Item item node next
- node(Item x)
- item x next 0
-
- typedef node link
- link head, tail
- public
- QUEUE(int)
- head 0
- int empty() const
- return head 0
- void put(Item x)
- link t tail
- tail new node(x)
6FIFO queue array implementation
We can implement get and put operations for the
FIFO queue ADT in constant time, using either
arrays or linked-lists.
- template ltclass Itemgt
- class QUEUE
-
- private
- Item q int N, head, tail
- public
- QUEUE(int maxN)
- q new ItemmaxN1
- N maxN1 head N tail 0
- int empty() const
- return head N tail
- void put(Item item)
- qtail item tail tail N
- Item get()
- head head N return qhead
-
If head tail, then empty if put would make
them equal, then full. Array is 1 larger to
allow checks.
7First-class ADT
Our Fraction ADT is a first-class ADT.
- Sedgewick Definition 4.4
-
-
-
- A first-class data type is one for which we
can have potentially many different instances,
and which we can assign to variables which we
declare to hold the instances.
8First-class Queue ADT
- template ltclass Itemgt
- class QUEUE
-
- private
- // Implementation-dependent code
- public
- QUEUE(int)
- QUEUE(const QUEUE)
- QUEUE operator(const QUEUE)
- QUEUE()
- int empty() const
- void put(Item)
- Item get()
-
Notice how each and every interface operations
now includes a references to a particular Q. We
can create as many queues as we need.
9Acknowledgements
- All of this code is from Horowitz, Sahni, and
Anderson-Freed, Fundamentals of Data Structures
in C. - Some slides were originally developed by Chen,
Hsin-His.