27
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13- 140909-3 1 Queues Chapter 8 May 26, 2015

Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

Embed Size (px)

Citation preview

Page 1: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

1Adapted from instructor resources

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Queues

Chapter 8

May 26, 2015

Page 2: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

2

Project #1

• Questions?• Can add own helper functions, etc, but note

that the functions in the header file must work as indicated to get full credit. My test class is assuming that your class works as provided

• When emailing me a question, you can call me Kristina ;)

Page 3: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

3

Quiz?

• Review Answers….

Page 4: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

4

Introduction to Stacks

• A stack is a last-in-first-out (LIFO) data structure

• Adding an item– Referred to as pushing it onto the stack

• Removing an item– Referred to as

popping it fromthe stack

Page 5: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

5

A Stack

• Definition: – An ordered collection of data items– Can be accessed at only one end (the top)

• Operations:– construct a stack (usually empty)– check if it is empty– Push: add an element to the top– Top: retrieve the top element– Pop: remove the top element

Page 6: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

6

Selecting Storage Structure

• A better approach is to let position 0 be the bottom of the stack

• Thus our design will include– An array to hold the stack elements– An integer to indicate the top of the stack

Page 7: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

7

Dynamic Array to Store Stack Elements

• Same issues regarding static arrays for stacks as for lists– Can run out of space if stack set too small– Can waste space if stack set too large

• As before, we demonstrate a dynamic array implementation to solve the problems

• Need destructor, copy constructor and assignment operator

Page 8: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

8

Further Considerations

• What if dynamic array initially allocated for stack is too small?– Could terminate– Better to replace with larger array

• Creating a larger array– Allocate larger array– Use loop to copy elements into new array– Delete old array– Point _items variable at this new array

Page 9: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

9

Linked Stacks

• Another alternative to allowing stacks to grow as needed

• Linked list stack needs only one data member– Pointer myTop– Nodes allocated (but not

part of stack class)

Page 10: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

10

Application of Stacks

Consider events when a function begins execution

• Activation record (or stack frame) is created • Stores the current environment for that

function.

• Contents:

Page 11: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

11

Use of Run-time StackWhen a function is called …• Copy of activation record pushed onto run-

time stack• Arguments copied into parameter spaces• Control transferred to starting address of

body of function

Page 12: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

12

Use of Run-time StackWhen function terminates• Run-time stack popped

– Removes activation record of terminated function

– exposes activation record of previously executing function

• Activation record used to restore environment of interrupted function

• Interrupted function resumes execution

Page 13: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

13

Evaluation of Postfix

• Note thechangingstatus of the stack

Page 14: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

14

Chapter Contents

8.1 Introduction to Queues

8.2 Designing and Building a Queue Class – Array Based

8.3 Linked Queues

8.4 Application of Queues: Buffers and Scheduling

Page 15: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

15

Chapter Objectives

• To study a queue as an ADT• Build a dynamic-array-based implementation of

queues• Show how queues are used in I/O buffers and

scheduling in a computer system

Page 16: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

16

Introduction to Queues

• A queue is a waiting line – seen in daily life– A line of people waiting for a bank teller– A line of cars at a toll both– "This is the captain, we're 5th in line for takeoff"

• What other kinds of queues can you think of

Page 17: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

17

The Queue As an ADT

• A queue is a sequence of data elements• In the sequence

– Items can be removed only at the front– Items can be added only at the other end, the back

• Basic operations– Construct a queue– Check if empty– Enqueue (add element to back)– Front (retrieve value of element from front)– Dequeue (remove element from front)

Page 18: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

18

Example

• Describe the contents of a queue after the following operations

1. enqueue(8)

2. enqueue(3)

3. dequeue()

4. enqueue(2)

5. enqueue(5)

6. dequeue()

7. dequeue()

8. enqueue(9)

9. enqueue(1)

88 333 23 2 52 555 95 9 1

Page 19: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

19

Using Dynamic Array to Store Queue Elements

• Similar problems as with list and stack– Fixed size array can be specified too large or too

small• Dynamic array design allows sizing of array

for multiple situations• Results in structure as shown

Page 20: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

20

Designing and Building a Queue Class Array-Based

• Consider an array in which to store a queue

• Note additional variables needed– myFront, myBack

• Picture a queueobject like this

Page 21: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

21

Designing and Building a Queue Class Array-Based

• Problems– We quickly "walk off the end" of the array

• Possible solutions– Shift array elements (bad)– Use a circular queue

– Note that both emptyand full queuegives myBack == myFront

Page 22: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

22

Linked Queues

• Even with dynamic allocation of queue size– Array size is still fixed– Cannot be adjusted during run of program

• Could use linked list to store queue elements– Can grow and shrink to fit the situation– No need for upper bound

Page 23: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

23

Linked Queues

• Constructor initializes myFront, myBack

• Front– return myFront->data

• Dequeue– Delete first node (watch for empty queue)

• Enqueue– Insert node at end of list

Page 24: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

24

Circular Linked List

• Possible to treat the linked list as circular

– Last node points back to first node– Alternatively keep pointer to last node rather

than first node

Page 25: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

25

Application of Queues: Buffers and Scheduling

• Important use of queues is I/O scheduling– Use buffers in memory to improve program

execution

– Buffer arrangedin FIFO structure

Page 26: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

26

Application of Queues: Buffers and Scheduling

• Also times when insertions, deletions must be made from both ends– Consider a scrolling

window on the screen• This requires a double

ended queue– Called a deque (pronounced "deck")– Could also be considered a double ended stack

(or "dack")

Page 27: Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights

27

Dequeue

• Double Ended Queue == DEQueue• Supports insertions and deletions at both ends

Operations• insertFirst() and insertLast()• removeFirst() and removeLast()

• A doubly-linked list is a natural implementation of the dequeue ADT