Ds - Lect4 - Linkedlist I
Ds - Lect4 - Linkedlist I
Linked Lists(Part I)
•Singly linked lists
•A reusable linked list Class
•Circular Lists
•Linked stacks & queues
•Polynomials
2 1
DAT
temp
1
First BAT CAT DAT EAT … WAT NULL
};
main( ) { S first→ data[1]
class ThreeLetterList {
public:
// List Manipulation operations
..
private:
ThreeLetterNode *first;
};
J.-J. Chen, EE NTUST April 13, 2023 Link List I-7
Relationship between
ThreeLetterList & ThreeLetterNode
first first 10 20 30 0
2 1
50 50
t t
Drawbacks
• In a template class (e.g., List<Type>)
– Operations should be independent of the type, while direct
traversal depends on the type of elements
– Example: computing the sum of a Rectangle container does not
make sense
• A new function requires traversal
– of a container class needs the support of a new class member
function x→ a class should be as compact as possible.
– This is difficult because the class provider & class users might be
different in a programming team
• If one class user is allowed to add a new member function
– He would need to know how the container is implemented
ListA
first 10 20 30 0
Macro
#define List_foreach(gen, node) definition
for(node=gen.first();gen.NotNULL(); node=gen.Next();)
sum=0; Usage of
ListIterator gen(list); List_foreach
List_foreach(gen, node) { sum+=*node;}
first 0
r=0 q p
Reverse the link of q:
q→link = r = 0;
first 0 0
r=0 q p
First 0 0
r q p
Reverse the link of q
q→link = r;
First 0 0
r q p
First 0 0
r q p
First 0 0
r q p=0
first=q
first
First 0 0
r q p=0
The final reversed
linked list
first rear
first -
first - d a c f
http://cis.stvincent.edu/html/tutorials/swd/lists/lists.html
J.-J. Chen, EE NTUST April 13, 2023 Link List I-36
http://cis.stvincent.edu/html/tutorials/swd/lists/lists.html
y top
first rear
…
0 0
{
if (top==0) { StackEmpty( ); return 0; }
// return null pointer constant
0 StackNode *delnode = top;
retvalue = top→data; // get data field of top node
top = top→link; //update the top pointer
delete delnode; // free the node
return &retvalue; // return pointer to data
}
0
J.-J. Chen, EE NTUST April 13, 2023 Link List I-42
OUTLINE
a = 3x14 + 2 x8 + 1 a.first 3 14 2 8 1 0 0
b = 8 x14 − 3x10 + 10 x 6
b.first 8 14 -3 10 10 6 0
p
a.first c.first
3 14 2 8 1 0 0 11 14
b.first
8 14 -3 10 10 6 0 -3 10 0
q
p.exp<q.exp
p
a.first c.first
3 14 2 8 1 0 0 11 14
b.first
8 14 -3 10 10 6 0 -3 10
q
p.exp>q.exp 2 8 0
Problem:
when the function terminates, the memory occupied by the
polynomials a, b, c, d and t may not be freed automatically
→ because ListNode <Term> objects are
not physically contained in List <Term> objects (ref. page LinkList I-15)
after
… 0
List<Type> poly
first … 0
template<class Type>
void CircularList <Type> ::RetNode(ListNode <Type> *x)
// free the node pointed by x
{ stack push
x→link = av;
av = x;
}
2
First … … 0
1
available space
second new av 3 old av
J.-J. Chen, EE NTUST April 13, 2023 Link List I-54
Circular List Representation of
Polynomials
Zero Polynomial
• Prob. 4.3.3
Let x = (x1,x2,…,xn) be a linked list. Write an algorithm to
compute the expression
n −5
(x * x
i =1
i i +5 )
[ Hint: use two iterators to be defined on the same list if
the iterator operations are implemented as member
function of List<Type>.]