Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 102 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
102
Dung lượng
1,91 MB
Nội dung
Chapter 17: Stacks and Queues Objectives • In this chapter, you will: – – – – – Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a stack as a linked list Learn about infix, prefix, and postfix expressions, and how to use a stack to evaluate postfix expressions C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition Objectives (cont’d.) – – – – – – Learn how to use a stack to remove recursion Learn about queues Examine various queue operations Learn how to implement a queue as an array Learn how to implement a queue as a linked list Discover how to use queues to solve simulation problems C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition Stacks • Stack: a datastructure in which elements are added and removed from one end only – Addition/deletion occur only at the top of the stack – Last in first out (LIFO) datastructure • Operations: – Push: to add an element onto the stack – Pop: to remove an element from the stack C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition Stacks (cont’d.) C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition Stacks (cont’d.) C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition Stacks (cont’d.) C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition Stack Operations • In the abstract class stackADT: – – – – – – initializeStack isEmptyStack isFullStack push top pop C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition Implementation of Stacks as Arrays • First element goes in first array position, second in the second position, etc • Top of the stack is index of the last element added to the stack • Stack elements are stored in an array, which is a random access datastructure – Stack element is accessed only through top • To track the top position, use a variable called stackTop C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition Implementation of Stacks as Arrays (cont’d.) • Can dynamically allocate array – Enables user to specify size of the array • class stackType implements the functions of the abstract class stackADT C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 10 Empty and Full Queue • Queue is empty if queueFront is nullptr • Queue is never full – Unless the system runs out of memory • Note: must provide isFullQueue function definition because it is an abstract function in parent class queueADT C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 88 Initialize Queue • Initializes queue to an empty state – Must remove all existing elements, if any – Deallocates memory occupied by elements C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 89 addQueue, front, back, and deleteQueue Operations • addQueue operation: adds new element to end of queue • front operation: returns first element of queue • back operation: returns last element of queue • deleteQueue operation: removes first element of queue C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 90 Queue Derived from the class unorderedLinkedListType • Linked implementation of queue: similar to implementation of a linked list created in a forward manner – – – – – – addQueue similar to insertFirst initializeQueue is like initializeList isEmptyQueue similar to isEmptyList deleteQueue can be implemented as before queueFront is same as first queueRear is same as last C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 91 Application of Queues: Simulation • Simulation: a technique in which one system models the behavior of another system • Computer models are used to study the behavior of real systems • Queuing systems: computer simulations using queues as the datastructure – Queues of objects are waiting to be served C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 92 Designing a Queuing System • Server: object that provides the service • Customer: object receiving the service • Transaction time: service time, or the time it takes to serve a customer • Model: system that consists of a list of servers and a waiting queue holding the customers to be served – Customer at front of queue waits for the next available server C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 93 Designing a Queuing System (cont’d.) • Need to know: – – – – Number of servers Expected arrival time of a customer Time between the arrivals of customers Number of events affecting the system • Performance of system depends on: – How many servers are available – How long it takes to serve a customer – How often a customer arrives C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 94 Designing a Queuing System (cont’d.) • If it takes too long to serve a customer and customers arrive frequently, then more servers are needed – System can be modeled as a time-driven simulation • Time-driven simulation: the clock is a counter – The passage of one unit of time can be implemented by incrementing a counter by – Simulation is run for a fixed amount of time C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 95 Customer C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 96 Server C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 97 Server List • Server list: a set of servers – At any given time, a server is either free or busy C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 98 Waiting Customers Queue • When customer arrives, he/she goes to end of queue • When a server becomes available, customer at front of queue leaves to conduct the transaction • After each time unit, the waiting time of each customer in the queue is incremented by • Can use queueType but must add an operation to increment waiting time C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 99 Main Program • Algorithm for main loop: C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 100 Summary • Stack: items are added/deleted from one end – Last In First Out (LIFO) datastructure – Operations: push, pop, initialize, destroy, check for empty/full stack – Can be implemented as array or linked list – Middle elements should not be accessed directly • Postfix notation: operators are written after the operands (no parentheses needed) C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 101 Summary (cont’d.) • Queue: items are added at one end and removed from the other end – First In First Out (FIFO) datastructure – Operations: add, remove, initialize, destroy, check if queue is empty/full – Can be implemented as array or linked list – Middle elements should not be accessed directly – Is a restricted version of array and linked list C++ Programming: ProgramDesignIncludingData Structures, Seventh Edition 102 ... (cont’d.) C++ Programming: Program Design Including Data Structures, Seventh Edition Stacks (cont’d.) C++ Programming: Program Design Including Data Structures, Seventh Edition Stacks (cont’d.) C++ Programming: ... C++ Programming: Program Design Including Data Structures, Seventh Edition 16 Push (cont’d.) C++ Programming: Program Design Including Data Structures, Seventh Edition 17 Push (cont’d.) C++ Programming: ... stack C++ Programming: Program Design Including Data Structures, Seventh Edition 20 Pop (cont’d.) C++ Programming: Program Design Including Data Structures, Seventh Edition 21 Pop (cont’d.) C++ Programming: