200 likes | 239 Views
Stacks and Queues. 3/5/99 B. Ramamurthy. Introduction. Stacks and queues are very commonly used ADTs in a computer system. For example every process in computer system has a user stack to maintain its context. The subroutine call structure is maintained in a LIFO basis.
E N D
Stacks and Queues 3/5/99 B. Ramamurthy Ramamurthy
Introduction • Stacks and queues are very commonly used ADTs in a computer system. • For example every process in computer system has a user stack to maintain its context. • The subroutine call structure is maintained in a LIFO basis. • The requests for the processes for various operation/ resources are maintained in a queue and served in a FIFO basis. • We will study the stack ADT and queue ADT in this discussion. Ramamurthy
ADT Stack • Methods • Constructor • Push, Pop • Peek • Empty • Java 1.2 Collection Class’s stack Ramamurthy
Application • Evaluation of postfix expression • Evaluate AB*CD-/ using a stack. A=6, B=3,C=10,D=2 Ramamurthy
ADT Queue • Queue is collection of items where items are added to one and removed from the other end in a FIFO fashion. Constructor() Empty() Add(NewItem) (to the end) Remove(Item) (from the front) Front(FrontItem) (similar to peek of stack) Ramamurthy
Implementation • Using Arrays, Vectors, or Linked List • First write the interface • Then implement it using any of already existing ADTs. • Lets define an interface for Queue class and implement it using LinkedList of Java Collection class. Ramamurthy
Application : Simulation • Simulation is a major area for computer applications. • It is a technique for modeling the behavior of a system. • Goal of simulation is to generate statistics that summarize the performance of an existing system and/or to predict the performance of a proposed system. • Central to simulation is the concept of simulated time. • Time-driven simulation, Event-driven simulation, and process-driven simulation. Ramamurthy
Simulate Bank Line • Objective: To determine the average wait time given a arrival time and processing time for a set of customers. • Input: • File containing many pairs of numbers representing {arrival time, processing time} Ordered by time of arrival. • Output requires: (1) Trace of events executed; (2) Statistics : total number of arrivals, average time spent in line. Ramamurthy
Simulate Bank Line : ADTs • Line : A queue of arrival events • Event list : • A list of arrival events and departure events that are sorted by the time of the event. • Possible states: empty, Arrival (A) event, Departure (D) event, A D and DA. • Insertion into event list is according to time. Ramamurthy
Simulate … Event class class BankEvent { EventKind whichevent; int Time; // arrival time int TransTime; // processing time Event(); // constructors Event (EventKind E, int Atime, int Ttime); } Ramamurthy
Simulate… Statistics class Stats { int TotalNum = 0; int TotalWait = 0; Stats(); // construtor }; Ramamurthy
Simulate… methods • GetArrival ( Instream Afile, Elist EL); • Reads arrival event from input file and inserts it into event list. • ProcessArrival (BankEvent AE, Instream Afile, Elist EL, Queue Line, Stats S); • Executes arrival event. • ProcessDeparture (BankEvent DE, Elist EL, Queue Line, Stats S); • Executes a departure event. • Simulate(String Filename, Stats S); • Performs the simulation by calling the above methods. Ramamurthy
Simulate… main method main() { Stats Statistics; Simulate(“bank.dat”, Statistics); //Print out Statistics; } Ramamurthy
Simulate method 1.Get Arrival from input file; Update EventList; 2. While (EventList is not empty) Get Next Event; If (Event is Arrival) Process Arrival; Else Process Departure; Ramamurthy
ProcessArrival method 1. Update number of arrival statistics. 2. Output a message with current time and event. 3. AtHead = True is Line is empty. 4. Add an Arrival event to Line. 5. Delete an event from Event list since it is being processed now. 6. If (AtHead) // start the processing first person Insert in the Event List a Departure Event for CurrentTime+TransTime of current event; 7. Get Next Arrival from Input File; Update Event List. Ramamurthy
ProcessDeparture method 1. Current Time = Arrival time of Departure Event; 2. Output a message with time and event; 3. Remove Item from Line since a person is departing; 4. Delete an event from the list; 5. If Line is not empty PersonInfo = Line front person’s info; Insert in Event List a Departure Event for Current Time + PersonInfo’s TranTime; Update Totalwait Statistics by (Current Time - PersonInfo.Time); 6. Exit. Ramamurthy
Overall Picture Line (queue) D A Simulator Event List Trace & Stats Ramamurthy
Miscellaneous • Insert into EventList is according to time of occurrence. • Delete the first element of the Event list. • Event List contains at most one arrival and one departure event. • Lets try hand simulate the bank line for the data: Ramamurthy
Arrival time Service Time 20 5 22 8 26 3 30 2 Then change first service time to 10 and check what happens. Sample data Ramamurthy