Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan...
Transcript of Stacks and Queuesanhtt/Slidesss/DataAlgo/DataAlgo-StackQueue… · Stacks and Queues Truong Tuan...
Stacks and QueuesTruong Tuan Anh
CSE-HCMUT
2
Outline
Basic conceptsStacksQueues
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
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
5
Push an Element
6
Push an Element: Overflow
Nothing changed in the stack
7
Pop an Element
8
Pop an Element: Underflow
Nothing changed in the stack
9
Implementation of Stacks
10
Using Linked List
11
Using Linked List
12
Using Linked List: C++
13
Create an empty Linked Stack
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
15
Push Data into a Linked Stack
16
Push Data into a Linked Stack: C++
17
Pop
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
19
Pop Linked Stack: C++
20
Other Functions
21
Destroy a Linked Stack
Release all nodes in the stack→ save memory
How?
22
Destroy a Linked Stack
23
Print out a Linked Stack
Print all nodes’ data
24
Using a Linked Stack
25
Read More
Array-based stack implementation
26
Queues
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
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
29
Enqueue
30
Dequeue
31
Implementation of Queues
32
Using Linked List
33
Using Linked List
34
Using Linked List: C++
35
Creating Queue
36
Creating Queue
37
Enqueue: Insert into an Empty Queue
38
Enqueue: Insert into a Queue
39
Enqueue: C++
40
Dequeue: Delete an Item in a Queue Having only one Item
41
Dequeue: Delete an Item in a Queue
42
Dequeue: C++
43
Queue: Other Functions
44
Destroy a Linked Queue
Release all nodes in the Queue→ save memory
How?
45
Destroy a Linked Queue
46
Print out a Linked Queue
Print all nodes’ data
47
Using a Linked Queue
48
Read More
Array-based queue implementation
49
Takeaways
Basic conceptsStacksQueues