Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights."— Presentation transcript:

1 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

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 ;) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 2

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

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 from the stack 4

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 5

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 6

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 7

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 8

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) 9

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: 10

11 Use of Run-time Stack When 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 11

12 Use of Run-time Stack When 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 12

13 Evaluation of Postfix Note the changing status of the stack 13

14 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

15 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

16 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 5 th in line for takeoff" What other kinds of queues can you think of

17 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)

18 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 3 33 2 3 2 52 5 55 9 5 9 1

19 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

20 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 queue object like this

21 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 empty and full queue gives myBack == myFront

22 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

23 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

24 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

25 25 Application of Queues: Buffers and Scheduling Important use of queues is I/O scheduling –Use buffers in memory to improve program execution –Buffer arranged in FIFO structure

26 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")

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 27


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

Similar presentations


Ads by Google