Stacks and Queues - PowerPoint PPT Presentation

About This Presentation
Title:

Stacks and Queues

Description:

2. Output a message with current time and event. 3. AtHead = True is Line is empty. ... Insert in Event List a Departure Event for Current Time PersonInfo's ... – PowerPoint PPT presentation

Number of Views:173
Avg rating:3.0/5.0
Slides: 20
Provided by: kumarm8
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Stacks and Queues


1
Stacks and Queues
  • 3/5/99
  • B. 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.

3
ADT Stack
  • Methods
  • Constructor
  • Push, Pop
  • Peek
  • Empty
  • Java 1.2 Collection Classs stack

4
Application
  • Evaluation of postfix expression
  • Evaluate ABCD-/ using a stack. A6, B3,C10,D2

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)

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.

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.

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.

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.

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)

11
Simulate Statistics
  • class Stats
  • int TotalNum 0
  • int TotalWait 0
  • Stats() // construtor

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.

13
Simulate main method
  • main()
  • Stats Statistics
  • Simulate(bank.dat, Statistics)
  • //Print out Statistics

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

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 CurrentTimeTransTime of current event
  • 7. Get Next Arrival from Input File Update Event
    List.

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 persons info
  • Insert in Event List a Departure Event for
    Current Time PersonInfos TranTime
  • Update Totalwait Statistics by (Current Time
    - PersonInfo.Time)
  • 6. Exit.

17
Overall Picture
Line (queue)
A
D
Simulator
Event List
Trace Stats
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

19
Sample data
  • Arrival time Service Time
  • 20 5
  • 22 8
  • 26 3
  • 30 2
  • Then change first service time to 10 and check
    what happens.
Write a Comment
User Comments (0)
About PowerShow.com