Lect04 Ds Elementary Data Structures
Lect04 Ds Elementary Data Structures
2024
Contents
1. Array
2. Linked Lists
5. Workshop
Array
Array
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Concept 1
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
An array is a fixed collection of same-type data that are stored contiguously and
Stack, Queue that are accessible by an index.
and Deque
Stack
Queue
Deque
Concept 2
Workshop A dynamic array is an array whose size can be changed during the execution of
the program.
4
Example of The sieve of Eratosthenes
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List • A simple program prints out all prime numbers less than N.
Circular Linked Lists
Doubly Linked Lists
Generalized Lists void sieve(int N) {
Stack, Queue int i;
and Deque
Stack
int *a = new int[N];
Queue for (i = 2; i < N; i++) a[i] = 1;
Deque
for (i = 2; i < N; i++)
Workshop
if (a[i])
for (int j = i; i*j < N; j++) a[i*j] = 0;
for (i = 2; i < N; i++)
if (a[i]) cout << " " << i;
delete [] a;
}
5
Linked Lists
• Singly Linked Lists
• Ordered Linked List
Data Abstraction
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Concept 3
Circular Linked Lists
Doubly Linked Lists Data abstraction is a process of hiding the implementation details of data
structures and operations, while exposing only the essential features and
Generalized Lists
Stack, Queue
and Deque
Stack
functionalities to the user.
Queue
Deque
insert data
delete data
update data
search data
...
7
Data Abstraction (cont.)
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Data
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
Stack, Queue
and Deque
Stack
Queue
Deque
Workshop
Data structures
8
Linked Lists
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Concept 4
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
A linked list is a set of items where each item is part of a node that also contains
Stack, Queue a link to a node. It allows the items be arranged in a linear order.
and Deque
Stack
Queue
Deque
Workshop
9
Linked Lists (cont.)
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Data
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
Stack, Queue
and Deque
Stack
Queue
Deque
Workshop
Data structures
10
Example of Josephus Election
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List
• Imagine that N people have decided to elect a leader by arranging themselves
Circular Linked Lists
Doubly Linked Lists
in a circle and eliminating every Mth person around the circle, closing ranks
Generalized Lists
as each person drops out. The problem is to find out which person will be
Stack, Queue
and Deque the last one remaining
Stack • If N = 9 and M = 5
Queue
Deque
1
Workshop
9 2
8 3
7 4
6 5
11
Data Structure for List Node
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List • We use pointers for links and structures for nodes
Circular Linked Lists
Doubly Linked Lists
Generalized Lists struct ListNode {
Stack, Queue DataType data;
and Deque
Stack
ListNode *next;
Queue ListNode ( DataType data , ListNode *next= nullptr ) {
Deque
this ->data = data;
Workshop
this ->next = next;
}
};
typedef ListNode *Link;
12
Create a List Node
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List • Creating a new node
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
ListNode *p = new ListNode (...);
Stack, Queue
and Deque
Stack
• We so often need to use the phrase “the node referenced by link p” that we
Queue
Deque
simply say “node p”
Workshop • It is a null link that points to no node.
• It refers to a dummy node that contains no data.
13
Delete a List Node
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List • Deleting a node
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
ListNode *p;
Stack, Queue
...
and Deque delete p;
Stack
Queue
Deque
• Writing a function to delete a node
Workshop
14
Deep Deletion
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List • Deleting a node and its link
Circular Linked Lists
Doubly Linked Lists • Writing a function to delete a node deeply
Generalized Lists
15
Organize a Linked List
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
first
Linked List
Circular Linked Lists
Doubly Linked Lists
to null
Generalized Lists
Stack, Queue
first second
and Deque
Stack
Queue
to be null
Deque
Workshop
first second third
to be or null
16
Data Structure for Linked List
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List struct LinkedList {
Circular Linked Lists
Doubly Linked Lists ListNode *first; // or ListNode *head;
Generalized Lists
LinkedList () {
Stack, Queue
and Deque
this ->first = nullptr ;
Stack }
Queue
Deque };
Workshop
17
Insert at The Beginning
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
first oldfirst
Linked List
Circular Linked Lists
Doubly Linked Lists null
to be or
Generalized Lists
Stack, Queue
first
and Deque oldfirst
Stack
Queue
not null to be or null
Deque
Workshop
first
not to be or null
18
Traversing a Linked List
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Assign List head to node pointer.
Circular Linked Lists
Doubly Linked Lists
while node pointer is not null
Generalized Lists Display the value member of the node pointed to by node pointer.
Stack, Queue
and Deque
Assign node pointer to its own next member.
Stack
Queue
end while.
Deque
Workshop
19
Another Data Structure for Linked List
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List struct LinkedList {
Circular Linked Lists
Doubly Linked Lists ListNode *first;
Generalized Lists
ListNode *last;
Stack, Queue
and Deque
LinkedList () {
Stack this ->first = nullptr ;
Queue
Deque this ->last = nullptr ;
Workshop }
};
20
Insert at The End
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
first oldlast last
Linked List
Circular Linked Lists
Doubly Linked Lists null
to be or
Generalized Lists
Stack, Queue
last
and Deque first oldlast
Stack
Queue
to be or null not null
Deque
Workshop
first oldlast last
to be or not null
21
Remove from The Beginning
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
first
Linked List
Circular Linked Lists
Doubly Linked Lists
not to be or null
Generalized Lists
Deque
Workshop
22
Search
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List ListNode * search ( ListNode *first , DataType k) {
Circular Linked Lists
Doubly Linked Lists ListNode * current = first;
Generalized Lists
while ( current ) {
Stack, Queue
and Deque
if (current ->data == k) then return current ;
Stack current = current ->next;
Queue
Deque }
Workshop return nullptr ;
}
23
Ordered Linked List
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Concept 5
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
An ordered linked list is a data structure that maintains a collection of elements in
Stack, Queue a linear sequence. The elements in an ordered linked list are arranged in a specific
and Deque
Stack
order, such as ascending or descending, based on the values of the elements.
Queue
Deque
Workshop
24
Insert
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Create a new node.
Circular Linked Lists
Doubly Linked Lists
Store data in the new node.
Generalized Lists if there are no nodes in the list
Stack, Queue
and Deque
Make the new node the first node.
Stack
Queue
else
Deque Find the first node whose value is greater than or equal to the new
Workshop value, or the end of the list (whichever is first).
Insert the new node before the found node, or at the end of the list
if no such node was found.
end if.
25
Dummy Head Node
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Concept 6
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
A dummy head node is a head node that does not store any actual data related to
Stack, Queue the problem.
and Deque
Stack
Queue head
Deque
Workshop
26
Sort
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Concept 7
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
Sorting an unordered linked list is arranging the elements of the list in a specific
Stack, Queue order, typically ascending or descending
and Deque
Stack
Queue
Deque
Workshop
27
Variations on Linked List
• Circular Linked Lists
• Doubly Linked Lists
• Generalized Lists
Circular Linked Lists danh sách liên kết vòng
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Concept 8
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
Circular linked list is a variation of linked list in which the last element points to
Stack, Queue the first element (or the first element points to the last element and).
and Deque
Stack
Queue
Deque
Workshop
29
Doubly Linked Lists
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Concept 9
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
Doubly linked is a variation of linked list in that it has two pointers. One points to
Stack, Queue the next node as before, while the other points to the previous node.
and Deque
Stack
Queue
Deque
Workshop
30
How to Extend Linked List
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List We can extend a data structure of linked list by abstracting
Circular Linked Lists
Doubly Linked Lists
• Data field
• Link field
Generalized Lists
Stack, Queue
and Deque
Stack
Queue
Deque
Workshop
31
Multi-Linked List
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Concept 10
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
A multi-linked list is a variation of the traditional linked list data structure where
Stack, Queue each node can have multiple pointers, or references, to other nodes, creating a
and Deque
Stack
hierarchical structure.
Queue
Deque
• Skip List
Workshop
NIL
NIL
NIL
NIL
head 1 2 3 4 5 6 7 8 9 10
32
Multi-Linked List
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List
Circular Linked Lists
Doubly Linked Lists
head
Generalized Lists of salary
Stack, Queue
and Deque
Stack
Queue
Deque
Workshop
head
of age
33
Generalized Lists
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Concept 11
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
A generalized list l is a finite sequence of n ≥ 0 elements, {e0 , e1 , . . . , en−1 },
Stack, Queue where ei is either an element or a generalized list.
and Deque
Stack
Queue
Deque
struct GenListNode {
Workshop bool tag;
GenListNode * next;
union {
DataType data;
GenListNode * down;
};
};
34
Generalized Lists (cont.)
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List • Consider the generalized list L = ((a, b, c), ((d, e), f ), g)
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
Stack, Queue
and Deque
Stack
Queue
Deque
Workshop
35
Stack, Queue and Deque
• Stack
• Queue
• Deque
Stack
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Concept 12
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
A stack is a data structure that stores and retrieves items in a last-in-first- out
Stack, Queue (LIFO) manner.
and Deque
Stack
Queue
Deque
Workshop
37
Stack API
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List
Circular Linked Lists
method description
Doubly Linked Lists
Generalized Lists
boolean isEmpty() is the stack empty?
Stack, Queue
int size() number of items in the stack
and Deque
Stack
void push(Item item) add item to the stack
Queue Item top() most recently added item
remove the most recently added item
Deque
Workshop
void pop()
38
Stack applications
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List • Parsing in a compiler. quá trình viết trình biên dịch
Circular Linked Lists
Doubly Linked Lists • Java virtual machine. dùng trong máy ảo
• Undo in a word processor. phần mềm soạn thảo
Generalized Lists
Stack, Queue
and Deque
Stack • Back button in a Web browser. trong web
Queue
Deque • PostScript language for printers.
Workshop
• Implementing function calls in a compiler. thực thi câu lệnh gọi
• ... hàm
39
Function calls
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List • How a compiler implements a function.
• Function call: push local environment and return address.
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
• Return: pop return address and local environment.
Stack, Queue
and Deque
Stack
Queue
Deque
Workshop
40
Remove recursion
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List • Recursive function: Function that calls itself.
Circular Linked Lists
Doubly Linked Lists • Can always use an explicit stack to remove recursion.
Generalized Lists
41
Arithmetic expression
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Arithmetic expression can be represented in
Circular Linked Lists
Doubly Linked Lists
• infix trung tố, phổ biến
Generalized Lists
Stack, Queue
<operand 1><operator><operand 2>
and Deque
Stack
• prefix (Polish Notation) tiền tố
Queue
Deque <operator><operand 1><operand 2>
Workshop
• postfix (Reverse-Polish Notation) hậu tố
<operand 1><operand 2><operator>
42
Conversion of an infix expression to postfix
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List
• Convert infixExp to postfixExp
Circular Linked Lists
Doubly Linked Lists
stackOps.push('(')
Generalized Lists
infixExp.append(')')
Stack, Queue
and Deque while not infixExp.end()?
Stack
Queue
tok ← infixExp.nextToken()
Deque
if tok is operand then postfixExp.append(tok)
Workshop
if tok is “(” then stackOps.push(tok)
if tok is operator then
while precedence of stackOps.top() is higher than or equal tok?
postfixExp.append(stackOps.pop())
stackOps.push(tok)
if tok is “)” then
while stackOps.top() is not “(”?
postfixExp.append(stackOps.pop())
stackOps.pop()
43
Example
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List
• Convert the infix expression (A+B)*(C-(D+A)) into a postfix expression
Circular Linked Lists
Doubly Linked Lists
stackOps infixExp postfixExp
Generalized Lists ( (A+B)*(C-(D+A)))
Stack, Queue ( A+B)*(C-(D+A)))
and Deque
Stack
( +B)*(C-(D+A)))
Queue ( B)*(C-(D+A)))
Deque
( )*(C-(D+A)))
Workshop
( *(C-(D+A)))
( (C-(D+A)))
( C-(D+A)))
( -(D+A)))
( (D+A)))
( D+A)))
( +A)))
( A)))
( )))
( ))
( ) 44
Arithmetic expression evaluation
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List • A simple version of two-stack algorithm proposed by E. W. Dijkstra
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
Scan tokens from the expression (fully parenthesized)
Stack, Queue If token
and Deque
Stack
• Value: push onto the value stack.
Queue
Deque • Operator: push onto the operator stack.
Workshop
• Left parenthesis: ignore.
• Right parenthesis:
• pop operator and two values.
• push the result of applying that operator to those values onto the
operand stack.
45
Arithmetic expression evaluation (cont.)
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List
• Evaluate the expression ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
Stack, Queue
and Deque
Stack
Queue
Deque
Workshop
46
Implementation (simple)
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List • Input in is a arithmetic expression that is fully parenthesized and contains
delimiters (space characters)
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
double evaluate(istream& in) {
Stack, Queue stack <string > ops;
and Deque stack <double > vals;
Stack string tok;
Queue while (!in.eof()) {
Deque in >> tok;
if (tok == "(");
Workshop else if (tok == "+" || tok == "*") ops.push(tok);
else if (tok == ")") {
string op = ops.top(); ops.pop();
double val2 = vals.top(); vals.pop();
double val1 = vals.top(); vals.pop();
if (op == "+") vals.push(val1 + val2);
else if (op == "*") vals.push(val1 * val2);
}
else vals.push(stod(tok));
}
return vals.top();
}
47
Queue
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Concept 13
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
A queue is a data structure that stores and retrieves items in a first-in- first-out
Stack, Queue (FIFO) manner.
and Deque
Stack
Queue
Deque
Workshop
48
Queue API
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List
Circular Linked Lists
method description
Doubly Linked Lists
Generalized Lists
boolean isEmpty() is the queue empty?
Stack, Queue
int size() number of items in the queue
and Deque
Stack
void enqueue(Item item) add item to the queue
Queue void dequeue() remove the least recently added item
the least recently added item
Deque
Workshop
Item front()
49
Queue applications
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List • Operating systems (queuing messages, IO requests, mouse movements, etc),
Circular Linked Lists
Doubly Linked Lists • Web servers (queuing incoming requests, file operations, etc)
Generalized Lists
Stack, Queue • Ticket counter line where people who come first will get his ticket first
and Deque
Stack • Bank line where people who come first will done his transaction first
Queue
Deque • ...
Workshop
50
Deque
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Concept 14
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
The deque stands for Double Ended Queue. Deque is a linear data structure
Stack, Queue where the insertion and deletion operations are performed from both ends. We
and Deque
Stack
can say that deque is a generalized version of the queue.
Queue
Deque
insertion insertion
removal removal
51
Deque (cont.)
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Some restricted deques
Circular Linked Lists
Doubly Linked Lists
• If we insert at the end and remove at the end, we get a stack
• if we insert at the end and remove at the beginning, we get a FIFO queue
Generalized Lists
Stack, Queue
and Deque
Stack
Queue
Deque
Workshop
52
Deque (cont.)
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List Array-based deque
Circular Linked Lists
0
Doubly Linked Lists
Generalized Lists
1
0
Stack, Queue
and Deque 2 11 1
Stack
Queue 3 first
Deque 10 2
4
Workshop
last
5
9 first 3
6
7
8 4
8
9 7 5
6
10 last
11
53
Deque API
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List
Circular Linked Lists
method description
Doubly Linked Lists
Generalized Lists
void push_front(Item item) Insert item at the front
Stack, Queue
void push_back(Item item) Insert item at the back
and Deque
Stack
void pop_front() Remove at the front
Queue void pop_back() Remove at the back
...
Deque
Workshop
54
Workshop
� Quiz
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List 1. What is a linked list?
.........................................................................
Circular Linked Lists
Doubly Linked Lists
Generalized Lists
.........................................................................
Stack, Queue
and Deque .........................................................................
Stack
Queue 2. What is a stack?
.........................................................................
Deque
Workshop
.........................................................................
.........................................................................
3. What is a queue?
.........................................................................
.........................................................................
.........................................................................
56
� Quiz (cont.)
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List 4. A letter means push and an asterisk means pop in the sequence
EAS*Y*QUE***ST***IO*N***
Circular Linked Lists
Doubly Linked Lists
Stack, Queue
5. An uppercase letter means put at the beginning, a lowercase letter means put
and Deque
Stack
Queue
Deque
at the end, a plus sign means get from the beginning, and an asterisk means
Workshop get from the end in the sequence
EAs+Y+QUE**+st+*+IO*n++*
Give the sequence of values returned by the get operations when this
sequence of operations is performed on an initially empty deque.
57
Ï Projects
Array
Linked Lists
Singly Linked Lists
Ordered Linked List
Variations on
Linked List 1. Design and implement class Polynomial
Circular Linked Lists
Doubly Linked Lists 2. Design and implement class Tensor
Generalized Lists
Workshop
58
References
Deitel, P. (2016).
C++: How to program.
Pearson.
Gaddis, T. (2014).
Starting Out with C++ from Control Structures to Objects.
Addison-Wesley Professional, 8th edition.
Jones, B. (2014).
Sams teach yourself C++ in one hour a day.
Sams.