INTRODUCTION TO PROGRAM DESIGN
Lecture 14: Custom Templatized
Data Structures
Yun-Shan Hsieh (謝昀珊),
Department of Computer Science and Information Engineering, NCKU
Acknowledgement: Slides template by [Link]
Introduction
• Linked lists are collections of data items logically “lined up in a
row”—insertions and removals are made anywhere in a linked list.
• Stacks are important in compilers and operating systems: Insertions
and removals are made only at one end of a stack—its top.
• Queues represent waiting lines; insertions are made at the back
(also referred to as the tail) of a queue and removals are made from
the front (also referred to as the head) of a queue.
• Binary trees facilitate searching and sorting data, duplicate
elimination and compiling expressions into machine code.
2
Self-Referential Classes and Link Lists
class Node {
public:
// constructor
explicit Node(int);
// set data member
void setData(int);
// get data member
int getData() const;
// set pointer to next Node
void setNextPtr(Node*);
// get pointer to next Node
Node* getNextPtr() const;
private:
int data; // data stored in this Node
Node* nextPtr; // pointer to another object of same type
};
3
Linked Lists
• Insert a value at the beginning of the List
• Insert a value at the end of the List
• Delete a value from the beginning of the List
• Delete a value from the end of the List
• End the List processing
4
Linked Lists (Cont.)
5
Linked Lists (Cont.)
6
Linked Lists (Cont.)
7
Linked Lists (Cont.)
8
Linked Lists (Cont.)
9
Linked Lists (Cont.)
10
Linked Lists (Cont.)
11
Linked Lists (Cont.)
12
Linked Lists (Cont.)
13
Linked Lists (Cont.)
14
Linked Lists (Cont.)
15
Linked Lists (Cont.)
16
Linked Lists (Cont.)
17
Linked Lists (Cont.)
18
Linked Lists (Cont.)
19
Linked Lists (Cont.)
20
Linked Lists (Cont.)
21
Linked Lists (Cont.)
22
Member Function insertAtFront
23
Member Function insertAtBack
24
Member Function removeFromFront
25
Member Function removeFromBack
26
Circular Linked Lists and Double Linked Lists
27
Stacks
28
Stacks (Cont.)
29
Stacks (Cont.)
30
Stacks (Cont.)
31
Stacks (Cont.)
32
Stacks (Cont.)
33
Stacks (Cont.)
34
Stacks (Cont.)
35
Stacks (Cont.)
36
Stacks (Cont.)
37
Queues
38
Queues (Cont.)
39
Queues (Cont.)
40
Queues (Cont.)
41
Queues (Cont.)
42
Queues (Cont.)
43
Queues (Cont.)
44
Basic Terminology of Trees
45
Binary Search Trees
46
Binary Search Trees (Cont.)
47
Binary Search Trees (Cont.)
48
Binary Search Trees (Cont.)
49
Binary Search Trees (Cont.)
50
Binary Search Trees (Cont.)
51
Binary Search Trees (Cont.)
52
Binary Search Trees (Cont.)
53
Binary Search Trees (Cont.)
54
Binary Search Trees (Cont.)
55
Binary Search Trees (Cont.)
56
Binary Search Trees (Cont.)
57
Binary Search Trees (Cont.)
58
Inorder Traversal Algorithm
• Traverse the left subtree with an inorder traversal.
• Process the value in the node—i.e., print the node value.
• Traverse the right subtree with an inorder traversal.
• Output: 6 13 17 27 33 42 48
59
Preorder Traversal Algorithm
• Process the value in the node.
• Traverse the left subtree with a preorder traversal.
• Traverse the right subtree with a preorder traversal.
• Output: 27 13 6 17 42 33 48
60
Postorder Traversal Algorithm
• Traverse the left subtree with a postorder traversal.
• Traverse the right subtree with a postorder traversal.
• Process the value in the node.
• Output: 6 17 13 33 48 42 27
61
References
• [Link]
• [Link]
62
Thank You Very Much!
Questions & Answers?
63