stacks and queues

26
1 of 32 FOA Stacks and Queues

Upload: muhammad-huzaifa-chaudhary

Post on 15-Apr-2017

59 views

Category:

Education


0 download

TRANSCRIPT

Page 1: STACKS AND QUEUES

1 of 32

FOA

Stacks and Queues

Page 2: STACKS AND QUEUES

2 of 32

Stacks

“A Stack is a special kind of list in which all insertions and deletions take place at one end, called the Top”

Other Names Pushdown List Last In First Out (LIFO)

Page 3: STACKS AND QUEUES

3 of 32

Stacks

Examples:

Books on a floor

Dishes on a shelf

Page 4: STACKS AND QUEUES

4 of 32

Common Operations on Stacks

1. MAKENULL(S): Make Stack S be an empty stack.

2. TOP(S): Return the element at the top of stack S.

3. POP(S): Remove the top element of the stack.4. PUSH(S): Insert the element x at the top of the

stack.5. EMPTY(S): Return true if S is an empty stack;

return false otherwise.

Page 5: STACKS AND QUEUES

5 of 32

An Array Implementation of StacksFirst Implementation Elements are stored in contiguous cells of an array. New elements can be inserted to the tail of the list.

Last Element

Second ElementFirst Element

List

Emptymaxlength

top

Page 6: STACKS AND QUEUES

6 of 32

An Array Implementation of Stacks

Problem with this implementation Every PUSH and POP requires moving the entire

array up and down.

1

1

2

1

2

3

1

2

Page 7: STACKS AND QUEUES

7 of 32

Since, in a stack the insertion and deletion take place only at the top, so…

A better Implementation: Anchor the bottom of the stack at the

bottom of the array Let the stack grow towards the top of the

array Top indicates the current position of the

first stack element.

An Array Implementation of Stacks

Page 8: STACKS AND QUEUES

8 of 32

A better Implementation:

First Element

Last Elementmaxlength

top

Second Element1

2 ..

An Array Implementation of Stacks

Page 9: STACKS AND QUEUES

9 of 32

A Linked-List Implementation of Stacks

PUSH and POP operate only on the header cell and the first cell on the list.

x y .zTop

Page 10: STACKS AND QUEUES

10 of 32

Queues

“A Queue is a special kind of list, where items are inserted at one end (the rear) And deleted at the other end (the front)”

Other Name: First In First Out (FIFO)

Difference from Stack:Insertion go at the end of the list, rather than the beginning of the list.

Page 11: STACKS AND QUEUES

11 of 32

Common Operations on Queues

1. MAKENULL(Q): Makes Queue Q be an empty list.

2. FRONT(Q): Returns the first element on Queue Q.

3. ENQUEUE(x,Q): Inserts element x at the end of Queue Q.

4. DEQUEUE(Q): Deletes the first element of Q.5. EMPTY(Q): Returns true if and only if Q is an

empty queue.

Page 12: STACKS AND QUEUES

12 of 32

A pointer Implementation of Queues

Keep two pointers: FRONT: A pointer to the first element of the

queue. REAR: A pointer to the last element of the

queue.

x y .zFront

Rear

Page 13: STACKS AND QUEUES

13 of 32

A pointer Implementation of Queues

Q.front

Q.Rear

MAKENULL(Q)

.

Q.front

Q.Rear

ENQUEUE(x,Q)

.x

Page 14: STACKS AND QUEUES

14 of 32

Q.frontQ.Rear

ENQUEUE(y,Q)

x .y

Q.front

Q.Rear

DEQUEUE(Q)

x .y

A pointer Implementation of Queues

Page 15: STACKS AND QUEUES

15 of 32

An Array Implementation of QueuesSame as Stack:

First ElementSecond Element

Last Element

List

Emptymaxlength

top

Page 16: STACKS AND QUEUES

16 of 32

An Array Implementation of Queues

No Problem in ENQUEUE

Problem with DEQUEUE:Removing the first element requires the entire queue be moved up one position in the array.

Thus, DEQUEUE takes O(n) times if the queue has length n.

Page 17: STACKS AND QUEUES

17 of 32

A better Implementation:

First Element

Last Elementmaxlength

top

Second Element1

2 ..

An Array Implementation of Stacks

bottom

Page 18: STACKS AND QUEUES

18 of 32

A better Implementation:

T

An Array Implementation of Queue

B

T

B

Page 19: STACKS AND QUEUES

19 of 32

A Circular Array Implementation of QueuesAn even better implementation

1maxlength2

queue

Q.rear Q.front

...

..

Page 20: STACKS AND QUEUES

20 of 32

The First position follows the last

The queue is found somewhere around the circle in consecutive positions

That is, a queue of length four could occupy the last two and first two positions of the array.

A Circular Array Implementation of QueuesAn even better implementation

Page 21: STACKS AND QUEUES

21 of 32

ENQUEUE: Move the Q.rear pointer one position clockwise. 1

maxlength2

Q.rear Q.front

. .

..X

A Circular Array Implementation of QueuesAn even better implementation

Page 22: STACKS AND QUEUES

22 of 32

DEQUEUE: Move the Q.front pointer one position clockwise. 1

maxlength2

Q.rear

. .

..

Q.frontX

A Circular Array Implementation of QueuesAn even better implementation

Page 23: STACKS AND QUEUES

23 of 32

Problem with above implementation:

No way to distinguish an Empty Queue from a Completely Filled Queue.

A Circular Array Implementation of QueuesAn even better implementation

Page 24: STACKS AND QUEUES

24 of 32

A Circular Array Implementation of Queues

Q.rear Q.front Q.rear Q.frontQ.rear

ab

dc

i

e

hgf

i

A Completely

Filled Queue

A Queue with

Only 1 Element

Page 25: STACKS AND QUEUES

25 of 32

Q.rear Q.front Q.rear Q.frontQ.rear

ab

dc

i

e

hgf

i

A Completely

Filled Queue

An Empty Queue

DEQUEUE

A Circular Array Implementation of Queues

Page 26: STACKS AND QUEUES

26 of 32

=> Although the array has maxlength elements but Queue should not grow more than maxlength - 1

Alternatively, introduce a separate bit to indicate the Queue Empty or Queue Filled status.

A Circular Array Implementation of Queues