1 / 19

Stacks and Queues

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.

ehoman
Download Presentation

Stacks and Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks and Queues 3/5/99 B. Ramamurthy Ramamurthy

  2. 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

  3. ADT Stack • Methods • Constructor • Push, Pop • Peek • Empty • Java 1.2 Collection Class’s stack Ramamurthy

  4. Application • Evaluation of postfix expression • Evaluate AB*CD-/ using a stack. A=6, B=3,C=10,D=2 Ramamurthy

  5. 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

  6. 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

  7. 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

  8. 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

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

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

  11. Simulate… Statistics class Stats { int TotalNum = 0; int TotalWait = 0; Stats(); // construtor }; Ramamurthy

  12. 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

  13. Simulate… main method main() { Stats Statistics; Simulate(“bank.dat”, Statistics); //Print out Statistics; } Ramamurthy

  14. 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

  15. 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

  16. 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

  17. Overall Picture Line (queue) D A Simulator Event List Trace & Stats Ramamurthy

  18. 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

  19. 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

More Related