Title: Advance Data Structure Review of Chapter 4
1Advance Data StructureReview of Chapter 4
2Overview of Chapter 4
- Type of List
- 4.1 Singly Linked Lists
- 4.2 Representing Lists in C
- 4.3 Reusable Linked List Class
- 4.4 Circular Lists
- 4.9 Doubly Linked Lists
- 4.10 Generalized Lists
- 4.12 Heterogeneous Lists
- Example
- 4.5 Linked Stacks and Queues
- 4.6 Polynomials
- 4.7 Equivalence Classes
- 4.8 Sparse Matrices
3Array-Based Ordered List
- ??
- ????????? ? O(1)
- ??
- ??????,?????????
- ??????????? ? O(n)
?????
4Array-Based Ordered List Insert
Insert (2, 44)
5Array-Based Ordered List Delete
Delete(2)
6Linked List
data
link
1
2
3
4
5
6
7
8
9
10
11
7Linked List ????
- ???????????????,????????
- ????????,???????
- ???????,????????
- ?????????? O(1)
- ?????????????,?????????????????,???????
- ????? O(n)
8Singly Linked Lists
Circular Lists
9Doubly Linked Lists
Head Node
data
LeftLink
RightLink
-
BAT
CAT
EAT
A
0
0
F
T
T
B
F
C
0
F
Generalized Lists
P
10Heterogeneous Lists
11Singly Linked Lists Insert
first
BAT
CAT
EAT
HAT
0
FAT
(3)
(2)
(1)
12Singly Linked Lists Delete
(2)
(3)
first
BAT
CAT
EAT
HAT
0
FAT
(1)
13Implementing Node of Linked Lists
- The Node of Linked list include
- Data field ????
- Link field ????? node
- ????,??? List Node ? class
- Example
- class ThreeLetterNode
-
- private
- char data3
- ThreeLetterNode link
-
14Implementing Linked Lists
- ?? List Node ???,?? Lists,????????????
- Design Attempt 1
- Use a global variable which is a pointer of
ListNode - Example ThreeLetterNode first
- Unable to access to private data members data
and link. - Design Attempt 2
- Make public member functions in class ListNode.
- Defeat the purpose of data encapsulation.
- Design Attempt 3
- Composition Class (HAS-A)
- Friend Classes.
- Nested Classes.
- Inheritance with private
15Design Attempt 3 Composition Classes
- Composition by Friend Classed
- //forward delcarion
- class ThreeLetterList
- class ThreeLetterNode
-
- friend class ThreeLetterList
- private
- char data3
- ThreeLetterNode link
-
- class ThreeLetterList
-
- public
- //List Manipulation operations
- .
- private
- ThreeLetterNode first
- Composition by Nested Classed
- class ThreeLetterList
-
- public
- //List Manipulation operations
- .
- .
- private
- //nested class
- class ThreeLetterNode
-
- public
- char data3
- ThreeLetterNode link
-
- ThreeLetterNode first
-
16Design Attempt 3 Composition Classes
List Manipulation operations interface
???
ThreeLetterNode
ThreeLetterList
???
ThreeLetterNode
17Design Attempt 3 Composition Classes
- Linked Lists ???????????,???????????????,????(Enca
psulation)???? - Linked Lists ?????(member functions)?????? Linked
Lists Node ???? - ???????? Liked List ????????????????
- Linked Lists Node ???????????,?? Linked Lists
????????!?????,?????
18Design Attempt 3 Composition Classes ????
- Linked Lists Node ????????????,?????
- ? C ? template ??!
- Linked Lists ??????????????????? Linked List Node
- ?????? iterators class,?????????????,Linked List
??????? iterators????????
19Design Linked Lists with Template
- template ltclass Typegt class List //forward
declaration - template ltclass Typegt
- class ListNode
- friend class ListltTypegt
- private
- Type data
- ListNode link
-
- template ltclass Typegt
- class List
- public
- List() first 0 //constructor initializing
first to 0 - // List manipulation operations
-
- private
- ListNodeltTypegt first
20Container and Iterators
- Containers (??)
- A container class is a class that represents a
data structure that stores a number of data
objects. (see book p121) - Iterators (???)
- An iterator is an object that is used to traverse
all the elements of a container class.
21Linked Lists Iterators
- template ltclass Typegt class ListIterator
- public
- ListIterator(const ListltTypegt l)list(l),current
(l.first) - bool NotNull()
- bool NextNotNull()
- Type First()
- Type Next()
- private
- const ListltTypegt list // refers to an
existing list - ListNodeltTypegt current // points to a node
in list -
Member function see book p180. Program 4.9
22Linked Lists Iterators Member Function
- template ltclass Typegt
- //check that the current element in list is
non-null - Boolean ListIterator ltTypegtNotNull()
- if (current) return TRUE
- else return FALSE
-
-
- template ltclass Typegt
- //check that the next element in list is non-null
- Boolean ListIterator ltTypegtNextNotNull()
- if (current current-gtlink) return TRUE
- else return FALSE
23Linked Lists Iterators Member Function
- template ltclass Typegt
- //return a pointer to the first element of list
- Type ListIterator ltTypegtFirst()
- if (list.first) return list.first-gtdata
- else return 0
-
- template ltclass Typegt
- //return a pointer to the next element of list
- Type ListIterator ltTypegtNext()
- if (current)
- current current-gtlink
- if (current) return current-gtdata
-
- else return 0
24Design Linked Lists with Template
List Node
- template ltclass Typegt class List //forward
declaration - template ltclass Typegt
- class ListNode
- friend class ListltTypegt
- friend class ListIterator ltTypegt
- private
- Type data
- ListNode link
Lists
template ltclass Typegt class List friend class
ListIterator ltTypegt public List() first
0 //constructor initializing first to 0
//List manipulation operations private
ListNodeltTypegt first
25Reusing the Linked Lists Class
- ????????,??????????????? Linked Lists Class
?????? - ?????,??????????? Linked Lists class,??
- ???????
- stacks ? queues ???
- ??????????????
- ?? equivalence class ?
26Circular Lists
Check Last Node current-gtlink first
Use the last node instead of the first node
27Circular Lists with dummy head node
Empty List
28Deconstruct a Linked Lists
- ?? Linked Lists ? Node ??? new ???,??????????
delete ??,??? memory leak ???? - ????
- ????
- ????????????,?????? delete first.
- Free Pool
- ?????,?????????????? available-space list or av
list (???? static ListNodeltTypegt av) - ???????,??? Free Pool,? Free Pool???,?? Free Pool
??????????,??,? Free Pool ??,? new
??????????????? - Free Pool ? Liked Lists?????????????
29Deconstruct a Linked Lists
- ??????
- template ltclass Typegt
- ListltTypegtList()
- //Free all nodes in the chain
-
- ListNodeltTypegt next
- for ( first first next)
- next first-gtlink
- delete first
-
-
Time Complexity O(n)
30Deconstruct a Linked Lists
(1)
(3)
(4)
first
0
(2)
av
0
31Doubly Linked Lists
- To efficiently delete a node, we need to know its
preceding node. Therefore, doubly linked list is
useful. - A node in a doubly linked list has at least three
fields left link field (llink), a data field
(item), and a right link field (rlink).
32Doubly Linked Lists
- class DblList
- class DblListNode
-
- friend class DblList
- private
- int data
- DblListNode llink, rlink
-
- class DblList
-
- public
- //List manipulation operations
- private
- DblListNode first //points to head node
-
33Doubly Linked Lists
first
data
LeftLink
RightLink
-
BAT
CAT
EAT
p-gtllink-gtrlink p p-gtrlink-gtllink
-
first
Empty doubly linked circular list with head node
34Doubly Linked Lists Delete
first
-
x
CAT
- void DblListDelete(DblListNode x)
-
- if (xfirst)
- cerrltltDeletion of head node not
permittedltltendl - else
-
- x-gtllink-gtrlink x-gtrlink
- x-gtrlink-gtllink x-gtllink
-
35Doubly Linked Lists Insert
first
-
x
p
void DblListInsert(DblListNode p,DblListNode
x) //insert node p to the right of node x
p-gtllink x p-gtrlink x-gtrlink
x-gtrlink-gtllink p x-gtrlink p
36Generalized List
- Generalized List
- A generalized list, A, is a finite sequence of
ngt 0 elements, a0,,an-1, where ai is either an
atom or a list. - The elements ai, 0?i?n-1, that are not atoms are
said to be sublists of A. - Examples
- D()
- A (a, (b, c))
- B (A, A, ())
- C (a, C)
- Consequences
- Lists may be empty (Example D)
- lists may be shared by other lists (Example B)
- lists may be recursive (Example C)
37Generalized List
D() A (a, (b, c)) B (A, A, ()) C (a, C)
A
a
0
F
T
b
F
c
0
F
B
T
T
0
0
T
C
a
0
F
T
38Important Generalized List Functions
- List Copy
- See textbook pp225-227
- Program 4.36
- List Equality
- See textbook pp228
- Program 4.37
- List Depth
- See textbook pp229
- Program 4.38
39Lists Copy
- ??????????
- ???(shallow copy)???????
- ???(deep copy)??????
L
???
20
45
51
76
L
L
20
45
51
76
???
L
20
45
51
76
40List Copy with Recursive Algorithms
- Indirect Recursive
- A recursive algorithm consists of two components
- The recursive function (the workhorse)
- Declared as a private function
- A second function that invokes the recursive
function at the top level (the driver) - declared as a public function.
41List Copy
- void GenListCopy(const GenList l)
-
- first Copy(l.first)
-
- GenListNode GenListCopy(GenListNode p)
- //Copy the nonrecursive list with no shared
sublists pointed at by p -
- GenListNode q 0
- if (p)
- q new GenListNode
- q-gttag p-gttag
- if (!p-gttag)
- q-gtdata p-gtdata
- else
- q-gtdlink Copy(p-gtdlink)
- q-gtlink Copy(p-gtlink)
-
- return q
Driver
Workhorse
O(M)
42Example List Copy A (Generalized Lists)
A
r
b
t
t
0
t
u
v
s
f
a
f
b
0
t
f
e
0
w
x
f
c
f
d
0
A((a,b),((c,d),e))
43Example List Copy A (Generalized Lists)
GenListCopy(A)
Level of recursion Value of p Continuing level p Continuing level p
1 b 2 r 3 u
2 s 3 u 4 v
3 t 4 w 5 0
4 0 5 x 4 v
3 t 6 0 3 u
2 s 5 x 2 r
1 b 4 w 3 0
2 r
1 b
44List Depth
- The Depth of list is defined as follows.
- An empty list has depth 0.
45Heterogeneous list
- A heterogeneous list is one that contains nodes
of different types. - If merging nodes by using union, then each node
is allocated for the largest node type. This
would waste space. - Use of public inheritance and virtual functions
can resolve this issue. - Let S(x) denote the space occupied by an object
of type x, then - S(CombinedNode)S(Data)S(CombineNode )
- S(int) max(S(char), S(int), S(float))
S(CombineNode )
46Heterogeneous list with union
- struct Data
-
- // id 0, 1, or 2 if the node contains a char,
an int, or a float - int id
- union
- int i
- char c
- float f
-
-
- class CombinedNode
- //Use union to merge different node types into
one class definition -
- friend class List
- friend class ListIterator
- private
- Data data
- CombinedNode link
Space allocation is based on the largest data
type, which is float.
47Heterogeneous list with union
- class list
-
- friend class ListIterator
- public
- // List manipulation operations follow
- .
- private
- CombinedNode first
-
- //the return type of class ListIterator is Data
48Heterogeneous list with Public Inheritance
- class Node
- friend class List friend class ListIterator
- protected
- Node link
- virtual Data GetData() 0
-
- templateltclass Typegt
- class DerivedNode public Node
-
- friend class List friend class ListIterator
- public
- DerivedNode(Type item) data(item) link 0
- private
- Type data
- Data GetData()
-
- Data DerivedNodeltchargtGetData()
49Heterogeneous list with Public Inheritance
class List friend class ListIterator public N
ode first class ListIterator
public ListIterator(const List l) list(l),
current(l.first) Data First() // minor
change in homogeneous list implementation Data
Next() // minor change in homogeneous list
implementation Boolean NotNull() //
implemented as for homogeneous lists Boolean
NextNotNull() // implemented as for homogeneous
lists private const List list Node
current Data temp
50Heterogeneous list with Public Inheritance
- Key point
- using the dynamic typing through public
inheritance, a pointer to a Node may be used to
point to nodes of type - DerivedNodeltchargt
- DerivedNodeltintgt
- DerivedNodeltfloatgt.
- This eliminates the problem that necessitated the
artificial union of the different node types
51Example
- Stacks and Queues
- Polynomial
- Equivalence Classes
- Sparse Matrix
52Example Stacks and Queues
data
link
top
first
rear
data
link
Linked Queue
Linked Stack
53Example Stacks and Queues
- class stack //forward declaration
- class StackNode
-
- friend class stack
- private
- int data
- StackNode link
- //???,??????????
- StackNode(int d0, StackNode l0)data(d),
link(l) -
- class Stack
-
- public
- Stack() top0 //constructor
- void Add(const int)
- int Delete(int )
- private
- StackNode top
????? textbook pp188-189
54Example Polynomial
Polynomial representation of 3x142x81
Polynomial representation of 8x14-3x1010x6
Polynomial representation of 3x142x81 with
Circular linked lists
55Example Polynomial
Zero polynomial with dummy head to avoid special
case
3x142x81 with dummy head to avoid special case
56Example Polynomial
- struct Term
- // all members of Terms are public by default
-
- int coef // coefficient
- int exp // exponent
- void Init(int c, int e) coef c exp e
-
- class Polynomial
-
- friend Polynomial operator(const
Polynomial,const Polynomial) - private
- ListltTermgt poly
-
????,class Polynomial ?????????? class List !
57Example PolynomialRepresentation of 3x2y with
Generalized Lists
trio
vble
exp
link
trio
vble
exp
link
var
y
0
ptr
1
0
var
x
0
no
3
2
0
P
About class definition of polynomial with
Generalized Lists Please see book pp222-223
58Example Polynomial with Generalized Lists
- trio var the node is a head node.
- vble indicates the name of the variable. Or it is
an integer point to the variable in a variable
table. - exp is set to 0.
- trio ptr
- coefficient itself is a list and is pointed by
the field dlink. - exp is the exponent of the variable on which the
list is based on. - trio no
- coefficient is an integer and is stored in coef.
- exp is the exponent of the variable on which the
list is based on.
59Example PolynomialRepresentation of P(x,y,z)
with Generalized Lists
v
z
0
p
2
p
1
0
v
y
0
v
y
0
p
3
p
2
0
p
4
p
1
0
v
x
0
n
2
0
0
v
x
0
n
3
8
0
v
x
0
n
1
10
n
2
8
0
v
x
0
n
1
4
n
6
3
0
P(x,y,z) x10y3z2 2x8y3z2 3x8y2z2 x4y4z
6x3y4z 2yz ((x102x8)y3 3x8y2)z2
((x46x3)y42y)z
60Example Equivalence Classes
- Equivalence Relation
- Definition
- A relation over a set S, is said to be an
equivalence relation over S if and only if it is - Reflexive(???)
- xx
- Symmetric(???)
- xy ? yx
- Transitive (???)
- xy and yz ? xz
- over S.
- Example
- is an equivalence relation over Z (how prove?
)
61Example Equivalence Classes
- Give a set S0,1,2,3,4,5,6,7,8,9,10,11 and
define the relation as follows - 04, 31, 610, 89, 74, 68, 35, 211, 110
- If is a equivalence relation over S, then we
get partitioning of S into three equivalence
classes - 0,2,4,7,11 1,3,5 6,8,9,10
- How do we determine the equivalence classes over
S?
62Determine the Equivalence Classes
- Two phases to determine equivalence classes.
- Phase one
- the equivalence pairs (i, j) are read in and
stored. - ????????????
- Phase two
- Begin at 0 and find all pairs of the form (0, j).
- Continue until the entire equivalence class
containing 0 has been found, marked, and printed. - Next find another object not yet output, and
repeat the above process
63Example Equivalence Classes
0
1
2
3
4
5
6
7
8
9
10
11
data
11
3
11
5
7
3
8
4
6
8
6
0
link
0
0
0
0
0
0
data
4
1
0
10
9
2
link
0
0
0
0
0
0
64Example Sparse Matrix
- Circular linked list representation of a sparse
matrix has two types of nodes - head node head, down, right, and next
- entry node head, down, row, col, right, value
- Head node i is the head node for both row i and
column i. - Each head node is belonged to three lists a row
list, a column list, and a head node list. - For an nxm sparse matrix with r nonzero terms,
the number of nodes needed is maxn, m r 1.
65Example Sparse Matrix
down
head
right
down
head
row
col
right
f
i
j
next
value
Setup for aij
Head node
Typical node
H1
H0
H2
H3
Matrix head
H0
11
H1
12
2
1
H2
-4
H3
-15
??head data member of a node is not shown
66Example Sparse Matrix
A 4x4 sparse matrix