0% found this document useful (0 votes)
9 views

Ds - Lect4 - Linkedlist I

The document discusses different aspects of linked lists including singly linked lists, a reusable linked list class, circular lists, linked stacks and queues, and polynomials. It also covers creating and traversing linked lists as well as the use of templates to create a generic linked list that can be used for different data types. The key benefit of linked lists over arrays discussed is that they do not have limitations on data movement or wasted space that arrays can experience.

Uploaded by

Y.H. Guo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Ds - Lect4 - Linkedlist I

The document discusses different aspects of linked lists including singly linked lists, a reusable linked list class, circular lists, linked stacks and queues, and polynomials. It also covers creating and traversing linked lists as well as the use of templates to create a generic linked list that can be used for different data types. The key benefit of linked lists over arrays discussed is that they do not have limitations on data movement or wasted space that arrays can experience.

Uploaded by

Y.H. Guo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Chapter 4

Linked Lists(Part I)
•Singly linked lists
•A reusable linked list Class
•Circular Lists
•Linked stacks & queues
•Polynomials

J.-J. Chen, EE NTUST April 13, 2023 Link List I-1


Introduction
• Why Linked List ?
• Array
– successive items locate a fixed distance
– Disadvantage
• Data movement (e.g, insertion & deletion)
• Waste space (storing n ordered lists of varying size)
• Solution → Linked Lists

J.-J. Chen, EE NTUST April 13, 2023 Link List I-2


Linked List
• Inserting a Node

First BAT CAT EAT … WAT NULL

First BAT CAT EAT


… WAT NULL

2 1
DAT

J.-J. Chen, EE NTUST April 13, 2023 Link List I-3


Linked List
• Deleting a Node

First BAT CAT DAT EAT … WAT NULL

temp
1
First BAT CAT DAT EAT … WAT NULL

J.-J. Chen, EE NTUST April 13, 2023 Link List I-4


A List Node in C++
class ThreeLetterNode{ first 0x 551b
private:

char data[3];
ThreeLetterNode *link; 0x 551b D first→ data[0]

};
main( ) { S first→ data[1]

ThreeLetterNode *First; C first→ data[2]


}
first→link

“First” is just a pointer, not a member function.


→Illegal access because data & link are private data

J.-J. Chen, EE NTUST April 13, 2023 Link List I-5


Dilemma of Node Data
• Public node data
– Will allow one to access the data through pointer
– The data encapsulation principle is violated
• Private node data
– Need another member functions to access the data
e.g., get_link(), get_data(), set_link(), set_data();
– Ideal solution:
• grant those functions that perform list operations
access to data members
• No other functions should have access to data
members
– Less efficient access

J.-J. Chen, EE NTUST April 13, 2023 Link List I-6


Composite Class
• Has-A relationship
– A data object of Type A Has-A data object of Type B if A
conceptually contains B or B is part of A
class ThreeLetterList; // forward declaration
class ThreeLetterNode{
friend class ThreeLetterList;
private:
char data[3];
ThreeLetterNode *link;
};

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

ThreeLetterList Conceptual relationship


ThreeLetterNode

first CAT DAT EAT … WAT NULL

ThreeLetterList Actual relationship


ThreeLetterNode
first CAT DAT EAT … WAT NULL

J.-J. Chen, EE NTUST April 13, 2023 Link List I-8


Nested Class
•ThreeLetterNode is defined as a
class ThreeLetterList{ //***********
inner class of ThreeLetterList
public:
// List Manipulation operations
•The data of ThreeLetterNode is
.
declared as public
.
Private:
class ThreeLetterNode { //nested class
public:
char data[3];
ThreeLetterNode *llink;
}
ThreeLetterNode *first;
};//*************

J.-J. Chen, EE NTUST April 13, 2023 Link List I-9


Nested class
• Ensures that ThreeLetterNode objects cannot be
accessed outside class ThreeLetterList
• Data members are declared as public
– Ensure that they can be accessed by member
functions of ThreeLetterList
• Using nested class
– Achieve the same effects as in composite class
approach

J.-J. Chen, EE NTUST April 13, 2023 Link List I-10


Create a Linked List
class ListNode {
private:
int data; List
ListNode *link
} first 10 20 0
void List::Create2()
{
first = new ListNode(10); // create & initialize first node
// create & initialize 2nd node & place its address in first→link
first→link = new ListNode(20);
}
ListNode::ListNode(int element=0) // 0 is the default argument in
constructor
{
data = element;
link = 0; // null pointer constant
}

J.-J. Chen, EE NTUST April 13, 2023 Link List I-11


Linked List Insertion
void List::Insert50(ListNode *x)
{
ListNode *t = new ListNode(50); // create and initialize new node
if(!first) // insert into empty list
{
first = t;
return; // exit List::Insert50
}
// insert after x
t→link = x→link;
x→link = t;
} x

first first 10 20 30 0
2 1
50 50
t t

J.-J. Chen, EE NTUST April 13, 2023 Link List I-12


OUTLINE

•Singly linked lists


•A reusable linked list Class
•Circular Lists
•Linked stacks & queues
•Polynomials

J.-J. Chen, EE NTUST April 13, 2023 Link List I-13


A reusable linked List with Template
• Software development strategy
– Reduce the person-hours spent without sacrificing
the quality of the software
→Reuse
• Reuse
– When designing software, try to do in a manner that
makes it possible to reuse the software in the future

J.-J. Chen, EE NTUST April 13, 2023 Link List I-14


Linked List Template
template <class Type> class List; // forward declaration

Template <class Type>


class ListNode {
friend class List <Type>;
private:
Type data;
ListNode *link;
}

template <class Type>


class List {
public:
List( ) { first=0; }; // constructor initializing first to 0
// list manipulation operations
private:
ListNode <Type> *first;
};
J.-J. Chen, EE NTUST April 13, 2023 Link List I-15
Direct Traversal of a List
// initialize a container C C++ implementation of
1. int x=-MAXINT; lines 2 and 4 depends on
2. for each item in Container C the container class being
3. { used
4. current = current item of C;
5. x = max(current, x); // body
6. }
7. return (x); // post-procoessing step

Direct traversal  a member of List<Type>


1 (revision of statements 3 to 7 for integer container)
2 for (ListNode<int> *ptr=first; ptr!=0; ptr=ptr→link)
3 {
4 current = ptr→ data; For an array a of size n,
5 x = max(current, x); 2 for (int i=0;i<n ;i++)
6} 4 currentItem=a[i]

J.-J. Chen, EE NTUST April 13, 2023 Link List I-16


Direct Traversal of a List
// initialize a container C C++ implementation of
1. int x=-MAXINT; lines 2 and 4 depends on
2. for each item in Container C the container class being
3. { used
4. current = current item of C;
5. x = max(current, x); // body
6. }
7. return (x); // post-procoessing step

Direct traversal  a member of List<Type>


(revision of statements 3 to 7 for integer container)
2 for (ListNode<int> *ptr=first; ptr!=0; ptr=ptr→link)
{
4 current = ptr→ data; For Direct Traversal:
x = max(current, x); It has to provide a member
} function to access the private
data of the class ListNode
J.-J. Chen, EE NTUST April 13, 2023 Link List I-17
Direct Traversal of Container Class
(ref. page 188 of textbook)

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

J.-J. Chen, EE NTUST April 13, 2023 Link List I-18


Iterator
• Iterative operations should be moved outside
the definition of List <Type>
– None of the operations modify or update the contents
of linked list
• But,
– each operation needs to access private data
members of List <Type> & ListNode <Type>
• Rather than making all these operations
friends of List <Type>,
– we define a third class ListIterator <Type>
– Handles the details of the linked list traversal &
retrieves the elements stored in the list

J.-J. Chen, EE NTUST April 13, 2023 Link List I-19


Array Iterator
void main()
{
int x[3] = {0, 1, 2}
//use a pointer y to iterate through the array x
for (int *y=x; y!=x+3; y++)
cout << *y << “ ”;
cout << endl;
} It’s more transparent
but prog. 4.8 can be
for (int i=0; i!=3; i++) generalized easily to
cout << x[i]<< “ ”; process the elements
of any object.

for (iterator i=start; i!=end; i++)


cout << *i << “ ”;

J.-J. Chen, EE NTUST April 13, 2023 Link List I-20


Linked List Iterator
Iterator
• An object used to traversal all the elements of a container
class C
• Useful in operations:
– Print all integers in C
– Obtain the maximum, minimum, mean or median of all integers
in C
– Obtain the sum, product, or sum of squares of all integers in C
– Obtain all integers in C that satisfy some property P(e.g.,
integers that are positive, or are the square of an integer, etc.)
– Obtain the integer x from C such that, for some function f, f(x) is
maximum

J.-J. Chen, EE NTUST April 13, 2023 Link List I-21


Linked List with Iterator (1)
enum Boolean {FALSE, TRUE}
template<class Type> class List;

template<class Type> class ListIterator;

template <class Type>


ListNode {
friend class List <Type>;
friend class ListIterator <Type>;
private:
Type data; template <class Type>
ListNode *link; class List {
}; friend class ListIterator <Type>;
public:
List() {first=0;} // constructor initializing first to 0
// list manipulation operations ..
private:
ListNode <Type> *first;
};
J.-J. Chen, EE NTUST April 13, 2023 Link List I-22
Linked List with Iterator(2)
template <class Type>
class ListIterator {
public:
ListIterator(const List <Type> &lg):list(lg),current(lg.first) { };
Boolean NotNull();
Boolean NextNotNull();
Type *First(); int a = b is setting a's VALUE to b's VALUE
Type *Next(); int* a = &b is setting a's VALUE to the ADDRESS of b
int& a = b is setting a's ADDRESS to b's ADDRESS (a is a reference to b)
private:
const List <Type> & list; // refers to an existing list
ListNode <Type> *current; // points to a node in list
}
ListIterator I
List A
first 10 20 0 listA current

J.-J. Chen, EE NTUST April 13, 2023 Link List I-23


List Iterator Functions
template <class Type> // check that the current element in List is non-null
Boolean ListIterator <Type> ::NotNull(){
if(current) return TRUE; else return FALSE;
}
template <class Type> //check that the next element in List is non-null
Boolean ListIterator <Type> ::NextNotNull() {
if(current && current→link) return TRUE; else return FALSE;
}
template <class Type> // return a pointer to the first element of List
Type *ListIterator <Type> ::First() {
if(list.first) return(&list.first→data); else return 0;
}
template <class Type> // return a pointer to the next element of List
Type * ListIterator <Type> ::Next() {
if(current) {
current = current→link;
if(current) return (&current→data);
else return 0;
}
}
J.-J. Chen, EE NTUST April 13, 2023 Link List I-24
Using the iterator
int sum(const List <int> &input_list)
{
ListIterator <int> lt(input_list); // the lt is associated with list input_list lg
if( ! lt.NotNull()) return 0; // return 0 if the list is empty

int ret_value = *lt.First(); // get the first element’s pointer


while(lt.NextNotNull()) { // iteratively sum up every element’s value
ret_value += *lt.Next(); // get it, add it to the current total
}
return (ret_value);
}

ListA
first 10 20 30 0

J.-J. Chen, EE NTUST April 13, 2023 Link List I-25


A Stylish Foreach Macro
• Assume List, ListNode, and ListIterator
– have been defined as above
• Variables
– list: an object of list
– data_ptr: an object of ListNode’s data pointer
– gen: an object of ListIterator for list

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;}

J.-J. Chen, EE NTUST April 13, 2023 Link List I-26


Inverting a Linked List (or Chain)
template <class T>
void List<T>::Invert()
// A chain x is inverted so that if x=(a1, a2, …, an)
// then after execution, x = (an, an-1, …, a1)
{
ListNode <T> *p=first; *q=0; // q trails p
while(p) {
ListNode <T> *r=q; q=p; // r trails q
p=p→link; // remember the next node of the node being inverted q
q→link = r; // link q to the preceding node r
}
first =q;
}
• Inverting
– Can be done “in place” in linear time O(n)
1 2 3
first
Current r q p
next r q p

J.-J. Chen, EE NTUST April 13, 2023 Link List I-27


Inverting a List(1)
first 0
q=0 p
Move p, q, r forward

first 0
r=0 q p
Reverse the link of q:
q→link = r = 0;

first 0 0
r=0 q p

J.-J. Chen, EE NTUST April 13, 2023 Link List I-28


Inverting a List(2)
first 0 0
r=0 q p
Move p, q, r forward

First 0 0
r q p
Reverse the link of q
q→link = r;

First 0 0
r q p

J.-J. Chen, EE NTUST April 13, 2023 Link List I-29


Inverting a List(3)

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

J.-J. Chen, EE NTUST April 13, 2023 Link List I-30


Concatenating two Chains
Template <class T>
void List <T> ::Concatenate(List <T> b)
// this = (a1,a2,…,an) and b= (b1,b2,…,bn), m,n >=0
// produces the new chain z= (a1,a2,…,an,b1,b2,…,bn) in this
{
if(!first) { first = b.first; return; }
if(b.first) { // finding the last node of *this
for(ListNode <T> *p=first; p→link; p=p→link); // no body
p→link = b.first;
}
}

List *this List b


a b first c d
first

J.-J. Chen, EE NTUST April 13, 2023 Link List I-31


OUTLINE

•Singly linked lists


•A reusable linked list Class
•Circular Lists
•Linked stacks & queues
•Polynomials

J.-J. Chen, EE NTUST April 13, 2023 Link List I-32


Circular List
• Major features
– The link field of the last element points to the first
element
– Check the last element (rear is a hint):
(current→link == first) instead of (current→link=0)

first rear

J.-J. Chen, EE NTUST April 13, 2023 Link List I-33


Insertion-to-rear Function
template <class Type>
void CircularList::Insert_to_rear(ListNode <Type> *x)
// insert the node pointed at by x at the rear of the circular
// list *this, where last points to the last node in the list
{
if(! last) { // empty list 3
last =x; x→link = x;
} last
else {
 x→link = last→link; x 2
 last→link = x;
 last = x;
}
} 1
first last

J.-J. Chen, EE NTUST April 13, 2023 Link List I-34


Header Nodes
◼ One problem with the basic description:
➢ it assumes that whenever an item x is removed
(or inserted) some previous item is always present.
◼ Remove the 1st item & insert a new item
➢ become special cases to consider.
◼ To avoid dealing with special cases:
➢ introduce a header node (dummy node).
◼ A header node
➢ is an extra node in the list that holds no data but
serves to satisfy the requirement that every node has a
previous node.

J.-J. Chen, EE NTUST April 13, 2023 35


Dummy Head Node
• In some applications
– Using simple circular list structure cause problems as
the empty list has to be handled as a special case
• Solution
– A dummy head node is introduced

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

J.-J. Chen, EE NTUST April 13, 2023 Link List I-37


OUTLINE

•Singly linked lists


•A reusable linked list Class
•Circular Lists
•Linked stacks & queues
•Polynomials

J.-J. Chen, EE NTUST April 13, 2023 Link List I-38


Linked Stack & Queue

y top

first rear

0 0

J.-J. Chen, EE NTUST April 13, 2023 Link List I-39


Stack Class Definition
class Stack; // forward declaration
class StackNode {
friend class Stack;
private:
int data;
StackNode *link;
StackNode (int d=0;StackNode *l=0):data(d),link(l){ }; // constructor
};
class Stack {
public:
Stack() {top=0;}; // constructor
void Add(const int);
int *Delete(int&);
private:
StackNode *top;
void StackEmpty();
};

J.-J. Chen, EE NTUST April 13, 2023 Link List I-40


Stack Add & Delete
2 top
void Stack::Add(const int y){
y
1 top = new StackNode( y, top);
top }

int *Stack::Delete(int & retvalue)


// Delete top node from stack & return a pointer to its data

{
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
}

J.-J. Chen, EE NTUST April 13, 2023 Link List I-41


Queue
Add & Delete
void Queue::Add(const int y)
{
if (front==0) front = rear = new QueueNode(y,0); // empty queue
else rear=rear→link = new QueueNode(y,0);
// attach node and update rear
}
int *Queue::Delete(int &retvalue)
// Delete the first node in queue and return a pointer to its data
{
if(front==0) { QueueEmpty(); return 0; } // return null pointer constant
QueueNode *x = front;
retvalue = front → data; //get data
front = x→link; // delete front node
delete x; // free the node
return &retvalue; // return pointer to data
}
old front front old rear rear

0
J.-J. Chen, EE NTUST April 13, 2023 Link List I-42
OUTLINE

•Singly linked lists


•A reusable linked list Class
•Circular Lists
•Linked stacks & queues
•Polynomials

J.-J. Chen, EE NTUST April 13, 2023 Link List I-43


Polynomial
• IS-IMPLEMENTED-BY Relationship
– A data object of Type A IS-IMPLMENTED-INTERMS-
OF a data object of Type B if the Type B object is
central to the implementation of Type A object
– Declaring the Type B object as a data member of the
Type A object
– Example:
• Polynomial implemented by linked list

J.-J. Chen, EE NTUST April 13, 2023 Link List I-44


Polynomial Class
struct Term //all members of Term are public by default
{
int coef; // coefficient 係數
int exp; // order 指數
Term Set(int c,int e) {coef = c; exp = e; return *this;};
};
class Polynomial
{
friend Polynomial operator+(const Polynomial &, const Polynomial&);
private:
List <Term> poly;
};

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

J.-J. Chen, EE NTUST April 13, 2023 Link List I-45


Generating the first three terms
p
a.first
3 14 2 8 1 0 0 c.first
11 14 0
b.first
8 14 -3 10 10 6 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 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

J.-J. Chen, EE NTUST April 13, 2023 Link List I-46


Adding two
polynomials
a&b

J.-J. Chen, EE NTUST April 13, 2023 Link List I-47


Analysis of operator+
• Computing time (assume each op. takes one unit of time)
– Coefficient additions → [0,min{m, n}]
– Exponent comparisons → bounded by m + n
– Addition/deletions to available space →
– Creation of new nodes → bounded by m + n
• Assumptions (Polynomial a & b have m & n terms)
• Coefficient additions:[0, min{m, n}] times
– Lower-bound: when none of the exponents are equal
– Upper-bound: when the exponents of one polynomial are a subset of the
exponents of the other polynomial
• Comparisons
– Since each comparison would make p or q or both move to the
next term → the # of exponent comparison is bounded by m+n
• Overall complexity: O(m + n)
– No more than (m + n) nodes are created → Maximum number of
executions of any operations is bounded by m+n

J.-J. Chen, EE NTUST April 13, 2023 Link List I-48


Polynomial computation
• D(x)=a(x)*b(x)+c(x) void func()
{
Polynomial a, b, c, d, t;
cin >> a; // read and create polynomial
cin >> b;
cin >> c;
t = a * b;
d = t + c;
cout << d
}

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)

J.-J. Chen, EE NTUST April 13, 2023 Link List I-49


Polynomial Object
before & after it goes out of scope
before
List<Type> poly
first … 0

after

… 0

Only removing pointer

J.-J. Chen, EE NTUST April 13, 2023 Link List I-50


Erase a Polynomial
template<class Type>
List <Type> ::~List() {
// Free all nodes in the chain
ListNode <Type> *next;
for ( ; first ; first=next) {
next = first→link;
delete first;
}
}

List<Type> poly
first … 0

J.-J. Chen, EE NTUST April 13, 2023 Link List I-51


Space Management of ListNodes
• By incorporating Circular List
– Freeing all the nodes in a list is more efficient
• Reuse strategy
– Deletion: nodes that have been “deleted” are actually
maintained in a pool→ available(av) space
– Request for a new node:
• If available space is not empty, recycle one of them
• If available space is empty→ by “new” command

J.-J. Chen, EE NTUST April 13, 2023 Link List I-52


Getting & Returning a Node
template<class Type> Consider av is a
ListNode <Type> * CircularList::GetNode()
// Provide a node for use
linked stack and
{ perform pop
ListNode <Type> *x;
if(!av) x = new ListNode<Type>; // request for a new one
else {x = av; av = av→link;} // recycle one from AV-pool
return x;
}

template<class Type>
void CircularList <Type> ::RetNode(ListNode <Type> *x)
// free the node pointed by x
{ stack push
x→link = av;
av = x;
}

J.-J. Chen, EE NTUST April 13, 2023 Link List I-53


Erasing a Circular List
template<class Type>
void CircularList <Type> ::~CircularList()
//Erase the entire circular list pointed by first
{
if(first){
ListNode* second = first→link; // second node
first→link =av; // first node linked to av
av=second; // second node of list vecomes front of av list
first=0;
} A circular list can be deleted in a fixed amount of time
} → independent of the number of nodes in the list

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

J.-J. Chen, EE NTUST April 13, 2023 55


Both circular lists reach the last
node, return the resultant
polynomial C

adding two polynomials


represented by a circular list

J.-J. Chen, EE NTUST April 13, 2023 Link List I-56


Adding circularly represented polynomials

J.-J. Chen, EE NTUST April 13, 2023 Link List I-57


Exercises
• Prob. 4.3.1 (page 183)
Write an algorithm to print all elements of all elements of
a linked list

• 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>.]

J.-J. Chen, EE NTUST April 13, 2023 Link List I-58


Exercise 4.3.1

template <class Type>


ostream &operator<<(ostream& os, List<Type> &l)
ListIterator<Type> li(l);
if(!li.NotNull()) return os;
os<<*li.First() << endl;
while(li.NextNotNull())
os<<*li.Next()<<endl;
return os;

J.-J. Chen, EE NTUST April 13, 2023 Link List I-59


Exercise 4.3.3
int Shift5Add(const List<int> &l) {
ListIterator<int> li1(l),li2(l); //li1 & li2 are both associated with list l
int retvalue;
if (! li1.NotNull() ) return 0; // empty list, return 0
for(int i=1,i<5 && li1.NextNotNull();i++)
li1.Next(); // li1 now points to the fifth element in the list
if(i<5) return 0; // list has < 5 elements
if(li1. NextNotNull( ) )
retvalue=(*li2.First( ) ) // li2 returns first element
*(*li1.Next( ) ); // li1 returns sixth element
else return 0; // list has exactly five elements

// step through the remaining elements, adding them to retvalue


while(li1.NextNotNull() ) // make sure that next element exists
retvalue += (*li1.Next() ) * (*li2.Next() );
return retvalue;
}
J.-J. Chen, EE NTUST April 13, 2023 Link List I-60

You might also like