stacks and queuesanhtt/slidesss/dataalgo/dataalgo-stackqueue… · stacks and queues truong tuan...

49
Stacks and Queues Truong Tuan Anh CSE-HCMUT

Upload: others

Post on 13-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

Stacks and QueuesTruong Tuan Anh

CSE-HCMUT

Page 2: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

2

Outline

Basic conceptsStacksQueues

Page 3: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

3

Stacks

A stack of elements of type T is a finite sequenceof elements of T, in which all insertions and deletions are restricted to one end, called the topStack is a Last In - First Out (LIFO) data structure.LIFO: The last item put on the stack is the first item that can be taken off

Page 4: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

4

Basic Operations of Stacks

Construct a stack, leaving it emptyPush an element: put a new element on to the top of the stackPop an element: remove the top element from the top of the stackTop an element: retrieve the top element

Page 5: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

5

Push an Element

Page 6: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

6

Push an Element: Overflow

Nothing changed in the stack

Page 7: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

7

Pop an Element

Page 8: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

8

Pop an Element: Underflow

Nothing changed in the stack

Page 9: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

9

Implementation of Stacks

Page 10: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

10

Using Linked List

Page 11: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

11

Using Linked List

Page 12: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

12

Using Linked List: C++

Page 13: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

13

Create an empty Linked Stack

Page 14: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

14

Push Data into a Linked Stack

1. Allocate memory for the new node and set up data

2. Update pointers:1. Point the new node to the top node (before adding the

new node)2. Point top to the new node

3. Update count

Page 15: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

15

Push Data into a Linked Stack

Page 16: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

16

Push Data into a Linked Stack: C++

Page 17: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

17

Pop

Page 18: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

18

Pop Linked Stack

1. dltPtr holds the element on the top of the stack2. top points to the next element3. Recycle dltPtr. Decrease count by 1

Page 19: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

19

Pop Linked Stack: C++

Page 20: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

20

Other Functions

Page 21: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

21

Destroy a Linked Stack

Release all nodes in the stack→ save memory

How?

Page 22: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

22

Destroy a Linked Stack

Page 23: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

23

Print out a Linked Stack

Print all nodes’ data

Page 24: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

24

Using a Linked Stack

Page 25: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

25

Read More

Array-based stack implementation

Page 26: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

26

Queues

Page 27: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

27

Queues

A queue of elements of type T is a finite sequence of elements of T, in which data can only be inserted at one end called the rear, and deleted from the other end called the frontQueue is a First In - First Out (FIFO) data structureFIFO: The first item stored in the queue is the first item that can be taken out

Page 28: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

28

Basic Operations of Queues

Construct a queue, leaving it emptyEnqueue: put a new element into the rear of the queueDequeue: remove the first element from the front of the queueQueue Front: retrieve the front elementQueue Rear: retrieve the rear element

Page 29: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

29

Enqueue

Page 30: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

30

Dequeue

Page 31: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

31

Implementation of Queues

Page 32: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

32

Using Linked List

Page 33: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

33

Using Linked List

Page 34: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

34

Using Linked List: C++

Page 35: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

35

Creating Queue

Page 36: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

36

Creating Queue

Page 37: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

37

Enqueue: Insert into an Empty Queue

Page 38: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

38

Enqueue: Insert into a Queue

Page 39: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

39

Enqueue: C++

Page 40: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

40

Dequeue: Delete an Item in a Queue Having only one Item

Page 41: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

41

Dequeue: Delete an Item in a Queue

Page 42: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

42

Dequeue: C++

Page 43: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

43

Queue: Other Functions

Page 44: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

44

Destroy a Linked Queue

Release all nodes in the Queue→ save memory

How?

Page 45: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

45

Destroy a Linked Queue

Page 46: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

46

Print out a Linked Queue

Print all nodes’ data

Page 47: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

47

Using a Linked Queue

Page 48: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

48

Read More

Array-based queue implementation

Page 49: Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan Anh CSE-HCMUT. 2 Outline zBasic concepts zStacks zQueues. 3 Stacks zA stack of elements

49

Takeaways

Basic conceptsStacksQueues