Stacks Queues and Deques B Ramamurthy 1212022 Ramamurthy
Stacks, Queues and Deques B. Ramamurthy 1/21/2022 Ramamurthy 1
Introduction u u 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. 1/21/2022 Ramamurthy 2
ADT Stack u Methods – Constructor – Push, Pop – Peek – Empty u. Java 1/21/2022 1. 2 Collection Class’s stack Ramamurthy 3
Stack Class Hierarchy Abstract. List extends java. util. Vector implemen ts Java. util. Stack 1/21/2022 List, Cloneable, Serializable Stack Ramamurthy 4
Stack Class Diagram Stack //constructor Stack() //methods Object push(Object item) Object pop() Object peek() int search(Object o) //predicate method boolean empty() 1/21/2022 Ramamurthy 5
Application : Using a Stack u Evaluation of postfix expression u Evaluate AB*CD-/ using a stack. A=6, B=3, C=10, D=2 1/21/2022 Ramamurthy 6
ADT Queue u Queue is collection of items where items are added to one and removed from the other end in a FIFO fashion. Constructor() Empty() Add(New. Item) (to the end) Remove(Item) (from the front) Front(Front. Item) (similar to peek of stack) 1/21/2022 Ramamurthy 7
Implementation u Using Arrays, Vectors, or Linked List u First write the interface u Then implement it using any of already existing classes. u Lets define an interface for Queue ADT and implement it using Linked. List of Java Collection class. 1/21/2022 Ramamurthy 8
Application : Simulation u u u 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. 1/21/2022 Ramamurthy 9
Simulate Bank Line u Objective: To determine the average wait time given a arrival time and processing time for a set of customers. u Input: – File containing many pairs of numbers representing {arrival time, processing time} Ordered by time of arrival. u Output requires: (1) Trace of events executed; (2) Statistics : total number of arrivals, average 1/21/2022 Ramamurthy 10 time spent in line (queue).
Deque u In a queue you insert at one end and remove from the other end. u In a deque you may insert and remove from both ends. u Deque comes from double-ended queue. u Hwk 3 : Implement a Deque from the Deque. Inter given. Use Vector for underlying data structure. Write a driver that creates an Deque object and exercises the methods by adding and removing 6 Integers from head and tail of deque. Due: 1/21/202210/22/99 Ramamurthy 11
Deque Interface Deque //constructor public Deque() //methods Object get. Head() Object get. Tail() void enque. Head(Object item) void enque. Tail(Object item) Object deque. Head() Object deque. Tail() 1/21/2022 Ramamurthy 12
- Slides: 12