lecture 3: stacks & queues - prakash gautamapplications: stacks & queues 38 to implement...

38
Lecture 3: Stacks & Queues Prakash Gautam https://prakashgautam.com.np/dipit02/ [email protected] 22 March, 2018

Upload: others

Post on 13-Jun-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Lecture 3: Stacks & Queues

Prakash Gautamhttps://prakashgautam.com.np/dipit02/[email protected] 22 March, 2018

Page 2: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Objectives● Definition: Stacks & Queues● Operations of Stack & Queues● Implementation of Stack & Queue● Applications

2

Page 3: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Stacks

3

Page 4: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Last In, First Out: LIFO

Page 5: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Queues

5

Page 6: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

First In, First Out: FIFO

Page 7: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Agenda● Introduction: Stacks & Queues● Basic Operations of Stack● Implementation of Stack● Basic Operations of Queue● Implementation of Queue

7

Page 8: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

An array is a random access data structure, where each element can be accessed

directly and in constant time.

A typical illustration of random access is a book - each page of the book can be open independently of others. Random access

is critical to many algorithms,like binary search

An array is a random access data structure, where each element can be accessed

directly and in constant time.

A typical illustration of random access is a book - each page of the book can be open independently of others. Random access

is critical to many algorithms,like binary search

Page 9: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

A linked list is a sequential access data Structure, where each element can be

accessed only in particular order.

A typical illustration of sequential access is a roll of paper or tape - all prior material

must be unrolled in order to get to data you want.

A linked list is a sequential access data Structure, where each element can be

accessed only in particular order.

A typical illustration of sequential access is a roll of paper or tape - all prior material

must be unrolled in order to get to data you want.

Page 10: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Stacks● Linear data structure● Last In, First Out (LIFO) data structure● A Stack is a container of objects: inserted and removed

according to LIFO principle✔ Items are removed in the reverse order from the way they were

inserted

● Stacks are less flexible (Limited access DS)✔ Data can be added & removed from the Stack only at the top✔ But are more efficient and easy to implement

10

Page 11: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

● Structural Definition:✔ A stack is either empty or✔ It consists of a top and the rest which is a stack

11

TOPTOP

Page 12: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Stack Operations● PUSH: To insert an item from top of stack● POP: To remove an item from top of the stack● IsEmpty: Stack considered empty when there is no

item on top● IsFull: Stack considered full if no other element can be

inserted on top of the stack

12

Page 13: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

TOP

EmptyStack

ATOP AB

TOPTOP

A

PUSH(A) PUSH(B) POP( )

13

Page 14: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Implementation of Stacks using Array● An array S: must be, ≥ 1

● TOP: Refers to the top element S[S.top]

● Capacity: Refers to the size 14

10 20 30 40 50 60 10 20 30 40 50 60

S.TOP=5 S.Capacity=10

0 21 3 4 5 6 7 8 9

S

Page 15: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

● The variable TOP changes from -1 to capacity–1

● The stack S is empty when top = -1

● The stack S is full when top = capacity-1● In fixed-size stack abstraction the capacity stays

unchanged, so when top=capacity, the stack object throws an exception

● In a dynamic stack abstraction when top=capacity, we double up the stack size

15

Page 16: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

● Stack Errors✔ Stack Underflow:trying to pop empty stack

✔ Stack Overflow: trying to push a full stack

● For underflow, you should throw an exception✔ If don’t caught, Java will throw an ArrayIndexOutOfBounds

exception✔ You could create your own, more informative exception

● For Overflow: Consider larger array

16

Page 17: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

● The bottom of the stack could be at the other end

● Two stacks to share the same storage area

17

S.top=6

17239744

0 1 2 3 4 5 6 7 8 9

S

S2.top=6

1723974449 57 3

0 1 2 3 4 5 6 7 8 9

S1.top=2

Page 18: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

STACK-EMPTY(S)1. if S.top == -12. return TRUE3. else return FALSE

STACK-EMPTY(S)1. if S.top == -12. return TRUE3. else return FALSE

O(1)

Page 19: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

PUSH(S, x)1. S.top = S.top+12. S[S.top]=x

PUSH(S, x)1. S.top = S.top+12. S[S.top]=x

O(1)

Page 20: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

POP(S)1. if STACK-EMPTY(S)2. error “underflow”3. else S.top = S.top-14. return S[S.top+1]

POP(S)1. if STACK-EMPTY(S)2. error “underflow”3. else S.top = S.top-14. return S[S.top+1]

O(1)

Page 21: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Implementation of Stacks using Linked-List● All the action happens at the top of a stack, so SLL is a

fine way to implement it● The header of the list points to the top of the stack

● Push: inserting an element at the front of the list● Pop: deleting an element from the front of the list

21

44 97 23 17

myStackNull

Page 22: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

● Overflow won’t happen. Why?● Underflow can happen. How can we solve?● If node is popped from a list: data will be removed.

22

Page 23: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Queues● Linear data structure● First In, First Out (FIFO) data structure● A Stack is a container of objects: inserted and removed

according to FIFO principle✔ Insertion is done at one end, while deletion is performed

at the other end● Example: Customers waiting to pay a cashier

✔ Can only add to the end of the queue, and can only remove from the front of the queue

23

Page 24: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Queue Operations● Enqueue: To insert an item at back of the queue● Dequeue: To delete an item from front of the queue

24

Page 25: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Queue Implementations using Array

25

Page 26: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

● To Insert(enqueue): Insert element on 5th position & set MyQueue.back=4

● To delete(dequeue): Take element from 1st position(0) & set MyQueue.front=1

26

17 23 97 44

0 1 2 3 4 5 6 7myQueue

MyQueue.front=0 MyQueue.back=4

Page 27: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

17 23 97 44 333After insertion:

23 97 44 333After deletion:

17 23 97 44Initial queue

…?

Q1.back=5

Q1.back=4

Q1.front=1

Q1.front=0

Page 28: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

● ‘Notice how the array contents “crawl” to the right as elements are inserted and deleted…!’

28

23 97 44 333

Page 29: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

QUEUE-EMPTY(Q)1. if Q.front==Q.back 2. return TRUE3. else return FALSE

QUEUE-EMPTY(Q)1. if Q.front==Q.back 2. return TRUE3. else return FALSE

O(1)

Page 30: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

ENQUEUE(Q,x)1. Q[Q.back]=x 2. if Q.back==Q.length3. Q.back=04. else Q.back=Q.back+1

ENQUEUE(Q,x)1. Q[Q.back]=x 2. if Q.back==Q.length3. Q.back=04. else Q.back=Q.back+1

O(1)

Page 31: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

DEQUEUE(Q)1. x=Q[Q.front] 2. if Q.front==Q.length3. Q.front=04. else Q.front=Q.front+15. return x

DEQUEUE(Q)1. x=Q[Q.front] 2. if Q.front==Q.length3. Q.front=04. else Q.front=Q.front+15. return x

O(1)

Page 32: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Implementations of Queues using Linked-List● In a queue, insertions occur at one end, deletions at the

other end● Operations at the front of a SLL are O(1), but at the

other end they are O(n),✔ Because you have to find the last element each time

● BUT✔ There is a simple way to use a SLL to implement both

insertions and deletions in O(1) time

32

Page 33: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

● HOW?✔ Use the first element in an SLL as the front of the

queue✔ Use the last element in an SLL as the back of the

queue✔ Keep pointers to both the front and the rear of the

SLL

34

Page 34: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Enqueue

35

17

Node to be enqueued

2344

lastfirst

97

● Find the current last node● Change it to point to the new last node● Change the last pointer in the list header

Page 35: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Dequeue

36

● First = First.next

44 97 23 17

lastfirst

Page 36: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Queue Implementation Details

37

● Array Implementation✔ You can have both overflow and underflow

✔ You should set deleted elements to null● SLL Implementation

✔ You can have underflow✔ Overflow is a global out-of-memory condition

✔ There is no reason to set deleted elements to null

Page 37: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

Applications: Stacks & Queues

38

● To Implement recursive functions● Queue of processes to be scheduled on the CPU ● Page-visited history in a Web browser● Undo sequence in a text editor● HTML Tag Matching & Management● Access to shared resources (e.g., printer)● Multiprogramming● Waiting lists● Bracket Balance(Mathematics)● Round-Robin scheduling

Page 38: Lecture 3: Stacks & Queues - Prakash GautamApplications: Stacks & Queues 38 To Implement recursive functions Queue of processes to be scheduled on the CPU Page-visited history in a

...?

Thank You39