CSCI 240 Abstract Data Types

1 / 9
About This Presentation
Title:

CSCI 240 Abstract Data Types

Description:

Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer IUPUI droberts_at_cs.iupui.edu –

Number of Views:57
Avg rating:3.0/5.0
Slides: 10
Provided by: DaleRo1
Learn more at: http://cs.iupui.edu
Category:
Tags: csci | abstract | data | queues | stacks | types

less

Transcript and Presenter's Notes

Title: CSCI 240 Abstract Data Types


1
Department of Computer and Information
Science,School of Science, IUPUI
CSCI 240
Abstract Data Types
Queues
Dale Roberts, Lecturer IUPUI droberts_at_cs.iupui.edu
2
Queues
  • 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)

3
DYNAMICALLY LINKED STACKS AND QUEUES
Figure 4.10 Linked Stack and queue (p.147)
4
FIFO queue ADT interface
  • template ltclass Itemgt
  • class QUEUE
  • private
  • // Implementation-dependent code
  • public
  • QUEUE(int)
  • int empty()
  • void put(Item)
  • Item get()

5
FIFO 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)

6
FIFO 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.
7
First-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.

8
First-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.
9
Acknowledgements
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com