Chapter 17: Stacks and Queues

Slides:



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

Chapter 22 Implementing lists: linked implementations.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
DATA STRUCTURE & ALGORITHMS
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Data Structures & Algorithms
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
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
Chapter 18: Stacks and Queues
Data Structures Using C++ 2E
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
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.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
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.
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.
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 8 Queue I CS Data Structures I COSC2006 April 24, 2017
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.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
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.
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Chapter 17: Stacks and Queues Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
STACKS & QUEUES for CLASS XII ( C++).
Data Structures Using C++ 2E
Data Structures Using C++ 2E
Objectives In this lesson, you will learn to: Define stacks
Data Structures Array Based Stacks.
Data Structures Array Based Stacks.
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.
CSC 248 – fundamentals of Data structure
Data Structures Using C++ 2E
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Queues.
CHAPTER 3: Collections—Stacks
Presentation transcript:

Chapter 17: Stacks and Queues Java Programming: Program Design Including Data Structures

Chapter Objectives 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 Java Programming: Program Design Including Data Structures

Chapter Objectives (continued) 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 Discover queue applications Java Programming: Program Design Including Data Structures

Stacks Lists of homogeneous elements Addition and deletion of elements occur only at one end, called the top of the stack Computers use stacks to implement method calls Stacks are also used to convert recursive algorithms into nonrecursive algorithms Especially recursive algorithms that are not tail recursive Java Programming: Program Design Including Data Structures

Stacks (continued) Figure 17-1 Various types of stacks Java Programming: Program Design Including Data Structures

Stacks (continued) Stacks are also called Last Input First Output (LIFO) data structures Operations performed on stacks Push: adds an element to the stack Pop: removes an element from the stack Peek: looks at the top element of the stack Java Programming: Program Design Including Data Structures

Stacks (continued) Figure 17-3 Stack operations Java Programming: Program Design Including Data Structures

Stacks (continued) Figure 17-4 UML diagram of the interface StackADT public class StackADT<T> { … } Java Programming: Program Design Including Data Structures

StackException Class Adding an element to a full stack and removing an element from an empty stack would generate errors or exceptions Stack overflow exception Stack underflow exception Classes that handle these exceptions StackException extends RunTimeException StackOverflowException extends StackException StackUnderflowException extends StackException Java Programming: Program Design Including Data Structures

Implementation of Stacks as Arrays The array implementing a stack is an array of reference variables Each element of the stack can be assigned to an array slot The top of the stack is the index of the last element added to the stack To keep track of the top position, declare a variable called stackTop Java Programming: Program Design Including Data Structures

Implementation of Stacks as Arrays (continued) Figure 17-5 UML class diagram of the class StackClass public class StackClass<T> implements StackADT<T> { … } Java Programming: Program Design Including Data Structures

Implementation of Stacks as Arrays (continued) public class StackClass<T> implements StackADT<T> { private int maxStackSize; private int stackTop; private T[] list; … } Java Programming: Program Design Including Data Structures

Implementation of Stacks as Arrays (continued) Figure 17-6 Example of a stack Java Programming: Program Design Including Data Structures

Constructors Default constructor public StackClass() { maxStackSize = 100; stackTop = 0; //set stackTop to 0 list = (T[]) new Object[maxStackSize]; //create the array }//end default constructor Java Programming: Program Design Including Data Structures

Initialize Stack Method initializeStack public void initializeStack() { for (int i = 0; i < stackTop; i++) list[i] = null; stackTop = 0; }//end initializeStack Java Programming: Program Design Including Data Structures

Empty Stack Method isEmptyStack public boolean isEmptyStack() { return (stackTop == 0); }//end isEmptyStack Java Programming: Program Design Including Data Structures

Full Stack Method isFullStack public boolean isFullStack() { return (stackTop == maxStackSize); }//end isFullStack Java Programming: Program Design Including Data Structures

Push Method push public void push(T newItem) throws StackOverflowException { if (isFullStack()) throw new StackOverflowException(); list[stackTop] = newItem; //add newItem at the //top of the stack stackTop++; //increment stackTop }//end push Java Programming: Program Design Including Data Structures

Peek Method peek public T peek() throws StackUnderflowException { if (isEmptyStack()) throw new StackUnderflowException(); return (T) list[stackTop - 1]; }//end peek Java Programming: Program Design Including Data Structures

Pop Method pop public void pop() throws StackUnderflowException { if (isEmptyStack()) throw new StackUnderflowException(); stackTop--; //decrement stackTop list[stackTop] = null; }//end pop Java Programming: Program Design Including Data Structures

Programming Example: Highest GPA Program reads a data file consisting of Each student’s GPA followed by the student’s name Then it prints Highest GPA (stored in the variable highestGPA) The names of all the students with the highest GPA The program uses a stack to keep track of the name of students with the highest GPA Java Programming: Program Design Including Data Structures

Linked Implementation of Stacks Arrays have fixed sizes Only a fixed number of elements can be pushed onto the stack Dynamically allocate memory using reference variables Implement a stack dynamically Similar to the array representation, stackTop is used to locate the top element stackTop is now a reference variable Java Programming: Program Design Including Data Structures

Linked Implementation of Stacks (continued) Figure 17-13 Nonempty linked stack public class LinkedStackClass<T> implements StackADT<T> { private StackNode<T> stackTop; … } Java Programming: Program Design Including Data Structures

LinkedStackClass & StackNode public class LinkedStackClass<T> implements StackADT<T> { private StackNode<T> stackTop; private class StackNode<T> { public T info; public StackNode<T> link; … } // end of StackNode<T> class private LinkedStackClass() { stackTop = null; } } // end of LinkedStackClass<T> class Java Programming: Program Design Including Data Structures

Default Constructor Default constructor public LinkedStackClass() { stackTop = null; }//end constructor Java Programming: Program Design Including Data Structures

Initialize Stack Method initializeStack public void initializeStack() { stackTop = null; }//end initializeStack Java Programming: Program Design Including Data Structures

Empty Stack and Full Stack Methods isEmptyStack and isFullStack public boolean isEmptyStack() { return (stackTop == null); }//end isEmptyStack public boolean isFullStack() return false; }//end isFullStack Java Programming: Program Design Including Data Structures

Push Method push public void push(T newElement) { StackNode<T> newNode; //reference variable to create //the new node newNode = new StackNode<T>(newElement, stackTop); //create //newNode and insert //before stackTop stackTop = newNode; //set stackTop to point to //the top element } //end push Java Programming: Program Design Including Data Structures

Peek Method peek public T peek() throws StackUnderflowException { if (stackTop == null) throw new StackUnderflowException(); return stackTop.info; }//end top Java Programming: Program Design Including Data Structures

Pop Method pop public void pop() throws StackUnderflowException { if (stackTop == null) throw new StackUnderflowException(); stackTop = stackTop.link; //advance stackTop to the //next node }//end pop Java Programming: Program Design Including Data Structures

Stack as Derived from the class UnorderedLinkedList The class LinkedStackClass can be derived from the class LinkedListClass The class LinkedListClass is an abstract class You can derive the class LinkedStackClass from the class UnorderedLinkedList public class LinkedStackClass<T> extends UnorderedLinkedList<T> { … } Java Programming: Program Design Including Data Structures

Linked Lists Collection Class… Object Interface… Iterable Interface… Cloneable Interface… Serializable Class… AbstractCollection Interface… Collection Class… AbstractList Interface… List Interface… Queue Class… AbstractSequentialList Interface… Deque Class… LinkedList COSC 237 © R.G. Eyer , 2012

Applications of Stacks: Postfix Expression Calculator Infix notation The operator is written between the operands Prefix or Polish notation Operators are written before the operands Does not require parentheses Reverse Polish or postfix notation Operators follow the operands Has the advantage that the operators appear in the order required for computation Java Programming: Program Design Including Data Structures

Applications of Stacks: Postfix Expression Calculator (continued) Table 17-1 Infix expressions and their equivalent postfix expressions Java Programming: Program Design Including Data Structures

Applications of Stacks: Postfix Expression Calculator (continued) Algorithm to evaluate postfix expressions using a stack Scan the expression from left to right Operands are pushed onto the stack When an operator is found, back up to get the required number of operands Perform the operation Continue processing the expression Java Programming: Program Design Including Data Structures

Main Algorithm Main algorithm in pseudocode for processing a postfix expression Get the next expression while more data to process { a. initialize the stack b. process the expression c. output result d. get the next expression } Java Programming: Program Design Including Data Structures

Method evaluateExpression General algorithm for evaluateExpression get the next token while (token != '=') { if (token is a number) output number push number onto stack } else token is an operation call method evaluateOpr to evaluate the operation if (no error in the expression) get next token discard the expression Java Programming: Program Design Including Data Structures

Method evaluateOpr This method evaluates an arithmetic operation Two operands are needed to evaluate an operation Operands are stored in the stack The stack must contain at least two operands Otherwise, the operation cannot be evaluated Java Programming: Program Design Including Data Structures

Method printResult public static void printResult(StackClass<Double> pStack, PrintWriter outp) { Double result; Double temp; if (expressionOk) //if no error, print the result if (!pStack.isEmptyStack()) result = (Double) pStack.peek(); pStack.pop(); if (pStack.isEmptyStack()) outp.printf(“%s %.2f %n”, strToken, result); else outp.println(strToken + “ (Error: Too many operands)”); }//end if outp.println(“ (Error in the expression)”); } outp.println(“_________________________________”); }//end printResult Java Programming: Program Design Including Data Structures

Postfix Expression Calculator: Graphical User Interface (GUI) Figure 17-28 GUI for the postfix calculator Java Programming: Program Design Including Data Structures

Postfix Expression Calculator: Graphical User Interface (GUI) (continued) Methods evaluateExpression, evaluateOpr, and printResult need minor modifications The data are no longer coming from a file The output is no longer stored in a file To simplify the accessing of the stack Declare it as an instance variable of the class containing the GUI program Java Programming: Program Design Including Data Structures

Postfix Expression Calculator: Graphical User Interface (GUI) (continued) To create the GUI, the class containing the GUI must extend the class JFrame The GUI interface contains several labels, three buttons, and a textfield When the user clicks a button, it should generate an action event You need to handle this event The class constructor creates the GUI components and initialize them Java Programming: Program Design Including Data Structures

Postfix Expression Calculator: Graphical User Interface (GUI) (continued) Figure 17-29 Sample run of PostfixCalculator program Java Programming: Program Design Including Data Structures

Removing Recursion: Nonrecursive Algorithm to Print a Linked List Backward Naïve approach Get the last node of the list and print it Traverse the link starting at the first node Repeat this process for every node Traverse the link starting at the first node until you reach the desired node Very inefficient Java Programming: Program Design Including Data Structures

Removing Recursion: Nonrecursive Algorithm to Print a Linked List Backward (continued) current = first; //Line 1 while (current != null) //Line 2 { stack.push(current); //Line 3 current = current.link; //Line 4 } Java Programming: Program Design Including Data Structures

Removing Recursion: Nonrecursive Algorithm to Print a Linked List Backward (continued) Figure 17-36 List and stack after the staments stack.push(current); and current = current.link; executes Java Programming: Program Design Including Data Structures

The class Stack Table 17-2 Members of the class Stack Java Programming: Program Design Including Data Structures

Queues Data structure in which the elements are added at one end, called the rear, and deleted from the other end, called the front A queue is a First In First Out data structure As in a stack, the middle elements of the queue are inaccessible public class QueueADT<T> { … } Java Programming: Program Design Including Data Structures

Queue Operations Queue operations initializeQueue isEmptyQueue isFullQueue front back addQueue deleteQueue Java Programming: Program Design Including Data Structures

QueueException Class Adding an element to a full queue and removing an element from an empty queue would generate errors or exceptions Queue overflow exception Queue underflow exception Classes that handle these exceptions QueueException extends RunTimeException QueueOverflowException extends QueueException QueueUnderflowException extends QueueException Java Programming: Program Design Including Data Structures

Implementation of Queues as Arrays Instance variables An array to store the queue elements queueFront: keeps track of the first element queueRear: keeps track of the last element maxQueueSize: specifies the maximum size of the queues Java Programming: Program Design Including Data Structures

Implementation of Queues as Arrays (continued) Figure 17-42 Queue after two more addQueue operations public class QueueClass<T> implements QueueADT<T> { … } Java Programming: Program Design Including Data Structures

Implementation of Queues as Arrays (continued) public class QueueClass<T> implements QueueADT<T> { private int maxQueueSize; private int count; private int queueFront; private int queueRear; private T[] list; … } Java Programming: Program Design Including Data Structures

Implementation of Queues as Arrays (continued) Problems with this implementation Arrays have fixed sizes After various insertion and deletion operations, queueRear will point to the last array position Giving the impression that the queue is full Solutions Slide all of the queue elements toward the first array position Use a circular array Java Programming: Program Design Including Data Structures

Implementation of Queues as Arrays (continued) Figure 17-45 Circular queue Java Programming: Program Design Including Data Structures

Implementation of Queues as Arrays (continued) Figure 17-46 Queue with two elements at positions 98 and 99 Java Programming: Program Design Including Data Structures

Implementation of Queues as Arrays (continued) Figure 17-47 Queue after one more addQueue operation Java Programming: Program Design Including Data Structures

Constructors Default constructor //Default constructor public QueueClass() { maxQueueSize = 100; queueFront = 0; //initialize queueFront queueRear = maxQueueSize - 1; //initialize queueRear count = 0; list = (T[]) new Object[maxQueueSize]; //create the //array to implement the queue } Java Programming: Program Design Including Data Structures

initializeQueue Method initilializeQueue public void initializeQueue() { for (int i = queueFront; i < queueRear; i = (i + 1) % maxQueueSize) list[i] = null; queueFront = 0; queueRear = maxQueueSize - 1; count = 0; } Java Programming: Program Design Including Data Structures

Empty Queue and Full Queue Methods isEmptyQueue and isFullQueue public boolean isEmptyQueue() { return (count == 0); } public boolean isFullQueue() return (count == maxQueueSize); Java Programming: Program Design Including Data Structures

Front Method front public T front() throws QueueUnderflowException { if (isEmptyQueue()) throw new QueueUnderflowException(); return (T) list[queueFront]; } Java Programming: Program Design Including Data Structures

Back Method back public T back() throws QueueUnderflowException { if (isEmptyQueue()) throw new QueueUnderflowException(); return (T) list[queueRear]; } Java Programming: Program Design Including Data Structures

addQueue Method addQueue public void addQueue(T queueElement) throws QueueOverflowException { if (isFullQueue()) throw new QueueOverflowException(); queueRear = (queueRear + 1) % maxQueueSize; //use the //mod operator to advance queueRear //because the array is circular count++; list[queueRear] = queueElement; } Java Programming: Program Design Including Data Structures

deleteQueue Method deleteQueue public void deleteQueue() throws QueueUnderflowException { if (isEmptyQueue()) throw new QueueUnderflowException(); count--; list[queueFront] = null; queueFront = (queueFront + 1) % maxQueueSize; //use the //mod operator to advance queueFront //because the array is circular } Java Programming: Program Design Including Data Structures

Linked Implementation of Queues Simplifies many of the special cases of the array implementation Because the memory to store a queue element is allocated dynamically, the queue is never full Class LinkedQueueClass implements a queue as a linked data structure It uses nodes of type QueueNode Java Programming: Program Design Including Data Structures

LinkedQueueClass & QueueNode public class LinkedQueueClass<T> implements QueueADT<T> { private QueueNode<T> queueFront; private QueueNode<T> queueRear; private class QueueNode<T> { public T info; public QueueNode<T> link; … } // end of QueueNode<T> class private LinkedQueueClass() { queueFront = null; queueRear = null; } } // end of LinkedStackClass<T> class Java Programming: Program Design Including Data Structures

Linked Implementation of Queues (continued) Method initializeQueue public void initializeQueue() { queueFront = null; queueRear = null; } Java Programming: Program Design Including Data Structures

Linked Implementation of Queues (continued) Methods isEmptyQueue and isFullQueue public boolean isEmptyQueue() { return (queueFront == null); } public boolean isFullQueue() return false; Java Programming: Program Design Including Data Structures

front and back Methods front and back public T front() throws QueueUnderflowException { if (isEmptyQueue()) throw new QueueUnderflowException(); return queueFront.info; } public T back() throws QueueUnderflowException return queueRear.info; Java Programming: Program Design Including Data Structures

addQueue Method addQueue public void addQueue(T newElement) { QueueNode<T> newNode; newNode = new QueueNode<T>(newElement, null); //create //newNode and assign newElement to newNode if (queueFront == null) //if initially the queue is empty queueFront = newNode; queueRear = newNode; } else //add newNode at the end queueRear.link = newNode; queueRear = queueRear.link; }//end addQueue Java Programming: Program Design Including Data Structures

deleteQueue Method deleteQueue public void deleteQueue() throws QueueUnderflowException { if (isEmptyQueue()) throw new QueueUnderflowException(); queueFront = queueFront.link; //advance queueFront if (queueFront == null) //if after deletion the queue queueRear = null; //is empty, set queueRear to null } //end deleteQueue Java Programming: Program Design Including Data Structures

Queue Derived from the class DoublyLinkedList The class LinkedQueueClass can be derived from the class UnorderedLinkedListClass addQueue and deleteQueue Likewise, the operations initializeQueue, initializeList, isEmptyQueue, and isEmptyList are similar queueFront is the same as front queueRear is the same as back public class LinkedQueueClass<T> extends DoublyLinkedList<T> { … }

Application of Queues: Simulation A technique in which one system models the behavior of another system Used when it is too expensive or dangerous to experiment with real systems You can also design computer models to study the behavior of real systems Queuing systems Queues of objects are waiting to be served Java Programming: Program Design Including Data Structures

Designing a Queuing System Terms Server Object that provides a service Customer Object receiving a service Transaction time The time it takes to serve a customer Java Programming: Program Design Including Data Structures

Designing a Queuing System (continued) Components List of servers Queue of waiting objects Process The customer at the front of the queue waits for the next available server When a server becomes free, the customer at the front of the queue moves to be served At the beginning, all servers are free Java Programming: Program Design Including Data Structures

Designing a Queuing System (continued) What you need to know The number of servers The customer expected arrival time The time between the arrivals of customers The number of events affecting the system Time-driven simulation Uses a clock that can be implemented as a counter The simulation is run for a fixed amount of time Java Programming: Program Design Including Data Structures

Customer Figure 17-54 UML class diagram of the class Customer Java Programming: Program Design Including Data Structures

Customer (continued) Methods getWaitingTime and incrementWaitingTime public int getWaitingTime() { return waitingTime; } public void incrementWaitingTime() waitingTime++; Java Programming: Program Design Including Data Structures

Server Figure 17-55 UML class diagram of the class Server Java Programming: Program Design Including Data Structures

Server (continued) Methods setTransactionTime and decreaseTransactionTime public void setTransactionTime(int t) { transactionTime = t; } public void setTransactionTime() transactionTime = currentCustomer.getTransactionTime(); public void decreaseTransactionTime() transactionTime--; Java Programming: Program Design Including Data Structures

Server List Figure 17-56 UML class diagram of the class serverList Java Programming: Program Design Including Data Structures

Server List (continued) Method setServerBusy public void setServerBusy(int serverID, Customer cCustomer, int tTime) { servers[serverID].setBusy(); servers[serverID].setTransactionTime(tTime); servers[serverID].setCurrentCustomer(cCustomer); } public void setServerBusy(int serverID, Customer cCustomer) int time; time = cCustomer.getTransactionTime(); servers[serverID].setTransactionTime(time); Java Programming: Program Design Including Data Structures

Waiting Customer Queue Class WaitingCustomerQueue class WaitingCustomerQueue extends QueueClass { public WaitingCustomerQueue() super(); } public WaitingCustomerQueue(int size) super(size); public void updateWaitingQueue() //... Java Programming: Program Design Including Data Structures

Main Program To run the simulation, you need the following simulation parameters Number of time units the simulation should run Number of servers Amount of time it takes to serve a customer Transaction time Approximate time between customer arrivals Java Programming: Program Design Including Data Structures

Main Program (continued) General algorithm Declare and initialize the variables, including the simulation parameters Run the main loop that moves customers from waiting queue to the next available server and updates simulation times Customer waiting time Server busy time Print the appropriate results Java Programming: Program Design Including Data Structures

Chapter Summary Stacks Stack implementation Addition and deletion of elements occur only at one end, called the top of the stack Follow a LIFO policy Stack implementation Array-based stacks Linked stacks Stacks are used to convert recursive algorithms into nonrecursive algorithms Java Programming: Program Design Including Data Structures

Chapter Summary (continued) Queues Elements are added at one end, called the rear, and deleted from the other end, called the front Follow a FIFO policy Queue implementation Array-based queues Linked queues Queues are used to construct computer-aided simulation programs Java Programming: Program Design Including Data Structures