Professional Documents
Culture Documents
Ds - Lect4 - Linked List II
Ds - Lect4 - Linked List II
11 3 11 5 7 3 8 4 6 8 6 0
4 1 0 10 9 2
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
11 3 11 5 7 3 8 4 6 8 6 0
4 1 0 10 9 2
int i, j, n;
inFile >> n; // read number of objects
// initialize seq and out
ListNodePtr *seq = new ListNodePtr[n];
Boolean *out = new Boolean[n];
for(i=0; i<n; i++){
seq[ i ] = 0;
out[ i ] = FALSE;
}
J.-J. Chen, EE NTUST April 13, 2023 Linked List II-8
do{ inFile>> i >> j; { // check end of file //phase 1:input equivalence pairs
ListNode *x = new ListNode(j); x->link=seq[i]; seq[i]=x; // add j to seq[i]
ListNode *y = new ListNode(i); y->link=seq[j]; seq[j]=y; // add i to seq[j]
} while(inFile.good());
j i y
x 11 0 0
x 0 0
- - 4
y
0 0 0
j i y
x 11 0 0
x 0 0
- - 4
y
0 0 0
0 0 11 0 0 13 0
12 0 0 0 0 0 14
0 − 4 0 0 0 −8 0
Field: head
0 0 0 0 0 0 0 Head of head nodes
→To distinguish
0 0 0 0 0 0 0 i, j denotes the dimension
between head nodes
of the matrix
and nonzero terms 0 − 9 0 0 0 0 0
H0
0 0 2
H1
1 0 4 1 3 3
class Matrix{
friend istream& operator>>(istream&, Matrix&);
public:
~Matrix(); // destructor
private:
MatrixNode *headnode;
};
H1
First … … 0
available space
second
new av old av
J.-J. Chen, EE NTUST April 13, 2023 Linked List II-22
Chapter 4
Linked Lists(Part II)
•Equivalence Class
•Sparse Matrices
•Doubly Linked Lists
•Generalized Lists
•Virtual Functions & Dynamic Binding
•Heterogeneous Lists
Head node
Left Right
__
first An empty list is not
really empty,
headnode is out there
J.-J. Chen, EE NTUST April 13, 2023 Linked List II-24
Class of doubly linked list
class Dblist;
class DblListNode{
friend class DblList;
private:
int data;
DblListNode *llink, *rlink;
};
class DblList{
public:
// lis manipulation operations
private:
DblListNode*first; // points to head node
};
J.-J. Chen, EE NTUST April 13, 2023 Linked List II-25
Deletion from doubly linked list
void DblList:: Delete(DblListNode *x)
{
if(x == first) cerr<<“Deletion of head node not permitted” << endl;
else{ x→llink→rlink=x→rlink;
x→rlink→llink=x→llink;
delete x;
}
}
4 2
1 3
A polynomial term
Fig. 4.34
Level of Value of p Continuing p Continuing p
recursion level level
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 0
1 b 4 w 3 r
2 b
1
J.-J. Chen, EE NTUST April 13, 2023 Linked List II-37
List Equality
// Driver- assumed to be a friend of GenList
int operator==(const GenList&l, const GenList&m)
// l and m are non-recursive lists
// The function returns 1 if the two lists are identical and 0, otherwise
{ return equal(l.first, m.first) }
// workhorse – assumed to be a friend of GenListNode
int equal(GenListNode* s, GenListNode *t)
{
int x;
if( (!s) && (!t) ) return 1; 1
if( s && t && (s→tag==t→tag ) )
{
Tag Data/ Link
if(!s→tag) Dlink 3
if(s→data == t→data) x=1; else x=0;
else x = equal(s→dlink, t→dlink); 2
if (x) return equal(s→link, t→link);
}
return 0;
}
J.-J. Chen, EE NTUST April 13, 2023 Linked List II-38
Depth of A General List
first
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
//workhorse
int GenList::depth(GenListNode *s)
{ first
if(!s) return 0;
r
GenListNode *p=s; int m=0; b t t 0
while(p) {
t u V
if (p→tag) { // list node S f a f b 0 t f e 0
int n = depth(p→dlink);
W X
if(m<n) m=n; f c f d 0
}
p=p→link; // move forward Depth(s)=
} 0
return m+1
1+max{depth(x1),…,depth(xn)}
}
if s is the list(x1,..,xn),n1
J.-J. Chen, EE NTUST April 13, 2023 Linked List II-40
General List with Sharing
tag data/down next Data/link of head node
→means reference count
A f 1 0 useful in decide when a node
can be returned to the storage pool
f 1 f b f c 0
C f 1 t t t 0 C = (B, B, ( ))
D f 2 f a t 0 f 1 0
f 1
1. A call to t.~Genlist() should f bonly in fa c 0
result
decrementing by 1 of the reference counter of
c. first ft.first.
1 t t t 0 C = (B, B, ( ))
A B C D E F G H
I J K L M N O P Q R
S T U V
t=(3x4+5x3+7x)y3
u=(3x4+5x3+7x)y6 +(6x)y
J.-J. Chen, EE NTUST April 13, 2023 Linked List II-44
Erasing A List Recursively
//Driver
GenList::~GenList()
//Each head node has a reference count. We assume first 0.
{
Delete(first);
first =0;
}
//WorkHorse
void GenList::Delete(GenListNode*x)
{
x→ref - -; // decrement reference count of head node.
if(!x→ref)
{
GenListNode *y = x; // y traverses top-level of x.
while(y→link) { y = y→link; if (y→tag == 1) Delete(y→dlink);}
y→link = av; // attach top-level nodes to av list
av=x;
}
}
J.-J. Chen, EE NTUST April 13, 2023 Linked List II-45
Indirect Recursion Case
• For recursive list such as D=(a,D)
– The reference count will never be 1
– So, they cannot be recycled
• Indirect recursive lists cannot be recycled either
After calls to
A.~Genlist() and B.~Genlist()
Output:
Data DerivedNode<char>::GetData();
{ Data t; t.id=0; t.c=data; return t; }
// the implementations of GetData() for DerivedNode<int> and
// derivedNode<float> are similar to that of
DerivedNode<char>::GetData()
Data DerivedNode<float>::GetData();
{ Data t; t.id = 2; t.f = data; return t; }
derived
class
base class
J.-J. Chen, EE NTUST April 13, 2023 Linked List II-58