lecture 3: stacks & queues - prakash gautamapplications: stacks & queues 38 to implement...
TRANSCRIPT
Lecture 3: Stacks & Queues
Prakash Gautamhttps://prakashgautam.com.np/dipit02/[email protected] 22 March, 2018
Objectives● Definition: Stacks & Queues● Operations of Stack & Queues● Implementation of Stack & Queue● Applications
2
Stacks
3
Last In, First Out: LIFO
Queues
5
First In, First Out: FIFO
Agenda● Introduction: Stacks & Queues● Basic Operations of Stack● Implementation of Stack● Basic Operations of Queue● Implementation of Queue
7
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
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.
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
● Structural Definition:✔ A stack is either empty or✔ It consists of a top and the rest which is a stack
11
TOPTOP
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
TOP
EmptyStack
ATOP AB
TOPTOP
A
PUSH(A) PUSH(B) POP( )
13
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
● 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
● 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
● 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
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)
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)
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)
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
● Overflow won’t happen. Why?● Underflow can happen. How can we solve?● If node is popped from a list: data will be removed.
22
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
Queue Operations● Enqueue: To insert an item at back of the queue● Dequeue: To delete an item from front of the queue
24
Queue Implementations using Array
25
● 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
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
● ‘Notice how the array contents “crawl” to the right as elements are inserted and deleted…!’
28
23 97 44 333
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)
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)
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)
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
● 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
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
Dequeue
36
● First = First.next
44 97 23 17
lastfirst
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
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
...?
Thank You39