Chapter 18: Stacks and Queues

Slides:



Advertisements
Similar presentations
TK1924 Program Design & Problem Solving Session 2011/2012
Advertisements

DATA STRUCTURES USING C++ Chapter 5
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
DATA STRUCTURE & ALGORITHMS
Data Structures & Algorithms
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 18: Stacks and Queues
Data Structures Using C++ 2E
Chapter 17: Stacks and Queues
Objectives of these slides:
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Pointer Data Type and Pointer Variables
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Chapter 7 Stacks Dr. Youssef Harrath
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Data Structures Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Chapter 18: Stacks and Queues
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
CIS 068 Welcome to CIS 068 ! Lesson 11: Data Structures 2.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Data Structures Using C++
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
1 Chapter 17: Stacks and Queues Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a.
CHP-3 STACKS.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Chapter 17: Stacks and Queues Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Chapter 16: Linked Lists.
Data Structures Using C++ 2E
Review Array Array Elements Accessing array elements
C++ Programming:. Program Design Including
Data Structures Using C++ 2E
CC 215 Data Structures Queue ADT
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Objectives In this lesson, you will learn to: Define stacks
Stacks and Queues.
Data Structures Interview / VIVA Questions and Answers
Data Structures Array Based Stacks.
CMSC 341 Lecture 5 Stacks, Queues
Stacks Data structure Elements added, removed from one end only
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using C++ 2E
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

Chapter 18: Stacks and Queues C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 18: 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 Discover stack applications Learn how to use a stack to remove recursion C++ Programming: Program Design Including Data Structures, Fifth Edition

Objectives (cont'd.) 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 queue applications C++ Programming: Program Design Including Data Structures, Fifth Edition

Stacks Stack: list of homogenous elements Operations: Addition and deletion occur only at one end, called the top of the stack Example: in a cafeteria, the second tray can be removed only if first tray has been removed Last in first out (LIFO) data structure Operations: Push: to add an element onto the stack Pop: to remove an element from the stack C++ Programming: Program Design Including Data Structures, Fifth Edition

Stacks (cont’d.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Stacks (cont’d.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Stack Operations In the abstract class stackADT: initializeStack isEmptyStack isFullStack push top pop C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Stacks as Arrays First element can go in first array position, the second in the second position, etc. The top of the stack is the index of the last element added to the stack Stack elements are stored in an array Stack element is accessed only through top To keep track of the top position, use a variable called stackTop C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Stacks as Arrays (cont'd.) Because stack is homogeneous You can use an array to implement a stack Can dynamically allocate array Enables user to specify size of the array The class stackType implements the functions of the abstract class stackADT C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Stacks as Arrays (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Stacks as Arrays (cont'd.) C++ arrays begin with the index 0 Must distinguish between: The value of stackTop The array position indicated by stackTop If stackTop is 0, the stack is empty If stackTop is nonzero, the stack is not empty The top element is given by stackTop - 1 C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Stacks as Arrays (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Initialize Stack C++ Programming: Program Design Including Data Structures, Fifth Edition

Empty Stack If stackTop is 0, the stack is empty C++ Programming: Program Design Including Data Structures, Fifth Edition

Full Stack The stack is full if stackTop is equal to maxStackSize C++ Programming: Program Design Including Data Structures, Fifth Edition

Push Store the newItem in the array component indicated by stackTop Increment stackTop Must avoid an overflow C++ Programming: Program Design Including Data Structures, Fifth Edition

Push (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Return the Top Element C++ Programming: Program Design Including Data Structures, Fifth Edition

Pop Simply decrement stackTop by 1 Must check for underflow condition C++ Programming: Program Design Including Data Structures, Fifth Edition

Pop (cont’d.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Pop (cont’d.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Copy Stack C++ Programming: Program Design Including Data Structures, Fifth Edition

Constructor and Destructor C++ Programming: Program Design Including Data Structures, Fifth Edition

Constructor and Destructor (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Copy Constructor C++ Programming: Program Design Including Data Structures, Fifth Edition

Overloading the Assignment Operator (=) C++ Programming: Program Design Including Data Structures, Fifth Edition

Stack Header File Place definitions of class and functions (stack operations) together in a file C++ Programming: Program Design Including Data Structures, Fifth Edition

Programming Example: Highest GPA Input: program reads an input file with each student’s GPA and name 3.5 Bill 3.6 John 2.7 Lisa 3.9 Kathy 3.4 Jason 3.9 David 3.4 Jack Output: the highest GPA and all the names associated with the highest GPA C++ Programming: Program Design Including Data Structures, Fifth Edition

Programming Example: Problem Analysis and Algorithm Design Read the first GPA and name of the student This is the highest GPA so far Read the second GPA and student name Compare this GPA with highest GPA so far New GPA is greater than highest GPA so far Update highest GPA, initialize stack, add to stack New GPA is equal to the highest GPA so far Add name to stack New GPA is smaller than the highest GPA Discard C++ Programming: Program Design Including Data Structures, Fifth Edition

Programming Example: Problem Analysis and Algorithm Design (cont’d.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Programming Example: Problem Analysis and Algorithm Design (cont’d.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Linked Implementation of Stacks Array only allows fixed number of elements If number of elements to be pushed exceeds array size Program may terminate Linked lists can dynamically organize data In a linked representation, stackTop is pointer to top element in stack C++ Programming: Program Design Including Data Structures, Fifth Edition

Linked Implementation of Stacks (cont’d.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Default Constructor Initializes the stack to an empty state when a stack object is declared Sets stackTop to NULL C++ Programming: Program Design Including Data Structures, Fifth Edition

Empty Stack and Full Stack In the linked implementation of stacks, the function isFullStack does not apply Logically, the stack is never full C++ Programming: Program Design Including Data Structures, Fifth Edition

Initialize Stack C++ Programming: Program Design Including Data Structures, Fifth Edition

Push The newElement is added at the beginning of the linked list pointed to by stackTop C++ Programming: Program Design Including Data Structures, Fifth Edition

Push (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Push (cont'd.) We do not need to check whether the stack is full before we push an element onto the stack C++ Programming: Program Design Including Data Structures, Fifth Edition

Return the Top Element C++ Programming: Program Design Including Data Structures, Fifth Edition

Pop Node pointed to by stackTop is removed C++ Programming: Program Design Including Data Structures, Fifth Edition

Pop (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Pop (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Copy Stack C++ Programming: Program Design Including Data Structures, Fifth Edition

Copy Stack (cont'd.) Notice that this function is similar to the definition of copyList for linked lists C++ Programming: Program Design Including Data Structures, Fifth Edition

Constructors and Destructors C++ Programming: Program Design Including Data Structures, Fifth Edition

Overloading the Assignment Operator (=) C++ Programming: Program Design Including Data Structures, Fifth Edition

Stack as Derived from the class unorderedLinkedList Our implementation of push is similar to insertFirst (discussed for general lists) Other functions are similar too: initializeStack and initializeList isEmptyList and isEmptyStack linkedStackType can be derived from linkedListType class linkedListType is abstract Must implement pop as described earlier C++ Programming: Program Design Including Data Structures, Fifth Edition

Derived Stack (cont’d.) unorderedLinkedListType is derived from linkedListType Provides the definitions of the abstract functions of the class linkedListType We can derive the linkedStackType from unorderedLinkedListType C++ Programming: Program Design Including Data Structures, Fifth Edition

Application of Stacks: Postfix Expressions Calculator Infix notation: usual notation for writing arithmetic expressions The operator is written between the operands Example: a + b The operators have precedence Parentheses can be used to override precedence C++ Programming: Program Design Including Data Structures, Fifth Edition

Application of Stacks: Postfix Expressions Calculator (cont'd.) Prefix (Polish) notation: the operators are written before the operands Introduced by the Polish mathematician Jan Lukasiewicz Early 1920s The parentheses can be omitted Example: + a b C++ Programming: Program Design Including Data Structures, Fifth Edition

Application of Stacks: Postfix Expressions Calculator (cont'd.) Reverse Polish notation: the operators follow the operands (postfix operators) Proposed by the Australian philosopher and early computer scientist Charles L. Hamblin Late 1950's Advantage: the operators appear in the order required for computation Example: a + b * c In a postfix expression: a b c * + C++ Programming: Program Design Including Data Structures, Fifth Edition

Application of Stacks: Postfix Expressions Calculator (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Application of Stacks: Postfix Expressions Calculator (cont'd.) Postfix notation has important applications in computer science Many compilers first translate arithmetic expressions into postfix notation and then translate this expression into machine code Evaluation algorithm: Scan expression from left to right When an operator is found, back up to get the operands, perform the operation, and continue C++ Programming: Program Design Including Data Structures, Fifth Edition

Application of Stacks: Postfix Expressions Calculator (cont'd.) Example: 6 3 + 2 * = C++ Programming: Program Design Including Data Structures, Fifth Edition

Application of Stacks: Postfix Expressions Calculator (cont'd.) Symbols can be numbers or anything else: +, -, *, and / are operators Pop stack twice and evaluate expression If stack has less than two elements  error If symbol is =, the expression ends Pop and print answer from stack If stack has more than one element  error If symbol is anything else Expression contains an illegal operator C++ Programming: Program Design Including Data Structures, Fifth Edition

Application of Stacks: Postfix Expressions Calculator (cont'd.) Examples: 7 6 + 3 ; 6 - = ; is an illegal operator 14 + 2 3 * = Does not have enough operands for + 14 2 3 + = Error: stack will have two elements when we encounter equal (=) sign C++ Programming: Program Design Including Data Structures, Fifth Edition

Application of Stacks: Postfix Expressions Calculator (cont'd.) We assume that the postfix expressions are in the following form: #6 #3 + #2 * = If symbol scanned is #, next input is a number If the symbol scanned is not #, then it is: An operator (may be illegal) or An equal sign (end of expression) We assume expressions contain only +, -, *, and / operators C++ Programming: Program Design Including Data Structures, Fifth Edition

Main Algorithm Pseudocode: We will write four functions: evaluateExpression, evaluateOpr, discardExp, and printResult C++ Programming: Program Design Including Data Structures, Fifth Edition

Function evaluateExpression C++ Programming: Program Design Including Data Structures, Fifth Edition

Function evaluateOpr C++ Programming: Program Design Including Data Structures, Fifth Edition

Function evaluateOpr (cont’d.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Function discardExp This function is called whenever an error is discovered in the expression C++ Programming: Program Design Including Data Structures, Fifth Edition

Function printResult If the postfix expression contains no errors, the function printResult prints the result Otherwise, it outputs an appropriate message The result of the expression is in the stack and the output is sent to a file C++ Programming: Program Design Including Data Structures, Fifth Edition

Function printResult (cont’d.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Removing Recursion: Nonrecursive Algorithm to Print a Linked List Backward To print the list backward, first we need to get to the last node of the list Problem: how do we get back to previous node? Links go in only one direction Solution: save a pointer to each of the nodes with info 5, 10, and 15 Use a stack (LIFO) C++ Programming: Program Design Including Data Structures, Fifth Edition

Removing Recursion: Nonrecursive Algorithm to Print a Linked List Backward (cont’d.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Removing Recursion: Nonrecursive Algorithm to Print a Linked List Backward (cont’d.) Let us now execute the following statements: Output: 20 15 10 5 C++ Programming: Program Design Including Data Structures, Fifth Edition

Queues Queue: list of homogeneous elements Elements are: Added at one end (the back or rear) Deleted from the other end (the front) First In First Out (FIFO) data structure Middle elements are inaccessible Example: Waiting line in a bank C++ Programming: Program Design Including Data Structures, Fifth Edition

Queue Operations Some of the queue operations are: initializeQueue isEmptyQueue isFullQueue front back addQueue deleteQueue Abstract class queueADT defines these operations C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays You need at least four (member) variables: An array to store the queue elements queueFront and queueRear To keep track of first and last elements maxQueueSize To specify the maximum size of the queue C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays (cont'd.) To add an element to the queue: Advance queueRear to next array position Add element to position pointed by queueRear Example: array size is 100; originally empty C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays (cont'd.) To delete an element from the queue: Retrieve element pointed to by queueFront Advance queueFront to next queue element C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays (cont'd.) Will this queue design work? Suppose A stands for adding an element to the queue And D stands for deleting an element from the queue Consider the following sequence of operations: AAADADADADADADADA... C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays (cont'd.) The sequence AAADADADADADADADA... would eventually set queueRear to point to the last array position Giving the impression that the queue is full C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays (cont'd.) Solution 1: When the queue overflows to the rear (i.e., queueRear points to the last array position): Check value of queueFront If value of queueFront indicates that there is room in the front of the array, slide all of the queue elements toward the first array position Problem: too slow for large queues Solution 2: assume that the array is circular C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays (cont'd.) To advance the index in a (logically) circular array: C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays (cont'd.) Case 1: C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays (cont'd.) Case 2: C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays (cont'd.) Problem: Figures 19-32b and 19-33b have identical values for queueFront and queueRear However, the former represents an empty queue, whereas the latter shows a full queue Solution? C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays (cont'd.) Solution 1: keep a count Incremented when a new element is added to the queue Decremented when an element is removed Initially, set to 0 Very useful if user (of queue) frequently needs to know the number of elements in the queue We will implement this solution C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays (cont'd.) Solution 2: let queueFront indicate index of the array position preceding the first element queueRear still indicates index of last one Queue empty if: queueFront == queueRear Slot indicated by queueFront is reserved Queue can hold 99 (not 100) elements Queue full if the next available space is the reserved slot indicated by queueFront C++ Programming: Program Design Including Data Structures, Fifth Edition

Implementation of Queues as Arrays (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Empty Queue and Full Queue C++ Programming: Program Design Including Data Structures, Fifth Edition

Initialize Queue C++ Programming: Program Design Including Data Structures, Fifth Edition

Front Returns the first element of the queue C++ Programming: Program Design Including Data Structures, Fifth Edition

Back Returns the last element of the queue C++ Programming: Program Design Including Data Structures, Fifth Edition

addQueue C++ Programming: Program Design Including Data Structures, Fifth Edition

deleteQueue C++ Programming: Program Design Including Data Structures, Fifth Edition

Constructors and Destructors C++ Programming: Program Design Including Data Structures, Fifth Edition

Constructors and Destructors (cont'd.) The array to store the queue elements is created dynamically When the queue object goes out of scope, the destructor simply deallocates the memory occupied by the array C++ Programming: Program Design Including Data Structures, Fifth Edition

Linked Implementation of Queues Array size is fixed: only a finite number of queue elements can be stored in it The array implementation of the queue requires array to be treated in a special way Together with queueFront and queueRear The linked implementation of a queue simplifies many of the special cases of the array implementation In addition, the queue is never full C++ Programming: Program Design Including Data Structures, Fifth Edition

Linked Implementation of Queues (cont'd.) Elements are added at one end and removed from the other We need to know the front of the queue and the rear of the queue Two pointers: queueFront and queueRear C++ Programming: Program Design Including Data Structures, Fifth Edition

Empty and Full Queue The queue is empty if queueFront is NULL The queue is never full C++ Programming: Program Design Including Data Structures, Fifth Edition

Initialize Queue Initializes queue to an empty state Must remove all the elements, if any C++ Programming: Program Design Including Data Structures, Fifth Edition

addQueue C++ Programming: Program Design Including Data Structures, Fifth Edition

front and back Operations C++ Programming: Program Design Including Data Structures, Fifth Edition

deleteQueue C++ Programming: Program Design Including Data Structures, Fifth Edition

Default Constructor C++ Programming: Program Design Including Data Structures, Fifth Edition

Queue Derived from the class unorderedLinkedListType The linked implementation of a queue is similar to the implementation of a linked list created in a forward manner addQueue is similar to insertFirst initializeQueue is like initializeList isEmptyQueue is similar to isEmptyList deleteQueue can be implemented as before queueFront is the same as first queueRear is the same as last C++ Programming: Program Design Including Data Structures, Fifth Edition

Queue Derived from the class unordered LinkedListType (cont'd.) We can derive the class to implement the queue from linkedListType Abstract class: does not implement all the operations However, unorderedLinkedListType is derived from linkedListType Provides the definitions of the abstract functions of the linkedListType Therefore, we can derive linkedQueueType from unorderedLinkedListType C++ Programming: Program Design Including Data Structures, Fifth Edition

Application of Queues: Simulation Simulation: a technique in which one system models the behavior of another system Computer simulations using queues as the data structure are called queuing systems C++ Programming: Program Design Including Data Structures, Fifth Edition

Designing a Queuing System Server: the object that provides the service Customer: the 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: Program Design Including Data Structures, Fifth Edition

Designing a Queuing System (cont'd.) We 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: Program Design Including Data Structures, Fifth Edition

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, say, one minute can be implemented by incrementing the counter by 1 Simulation is run for a fixed amount of time C++ Programming: Program Design Including Data Structures, Fifth Edition

Customer C++ Programming: Program Design Including Data Structures, Fifth Edition

Server C++ Programming: Program Design Including Data Structures, Fifth Edition

Server List A server list is a set of servers At a given time, a server is either free or busy C++ Programming: Program Design Including Data Structures, Fifth Edition

Waiting Customers Queue When a customer arrives, he/she goes to the end of the queue When a server becomes available, the 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 1 We can use queueType but must add the operation of incrementing the waiting time C++ Programming: Program Design Including Data Structures, Fifth Edition

Waiting Customers Queue (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Main Program Algorithm: Declare and initialize the variables Main loop (see next slide) Print results C++ Programming: Program Design Including Data Structures, Fifth Edition

Main Program (cont'd.) C++ Programming: Program Design Including Data Structures, Fifth Edition

Summary Stack: items are added/deleted from one end Last In First Out (LIFO) data structure Operations: push, pop, initialize, destroy, check for empty/full stack Can be implemented as array or linked list Middle elements should not be accessed Postfix notation: operators are written after the operands (no parentheses needed) C++ Programming: Program Design Including Data Structures, Fifth Edition

Summary (cont'd.) Queue: items are added at one end and removed from the other end First In First Out (FIFO) data structure 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 Restricted versions of arrays and linked lists C++ Programming: Program Design Including Data Structures, Fifth Edition