Chapter 5 Linked Lists

advertisement
Chapter 5
Linked Lists
Dr. Youssef Harrath
[email protected]
Outline
1.
Introduction
2.
Properties
3.
Insertion and Deletion
4.
Building a Linked List
5.
Linked List As an ADT
6.
Ordered Linked Lists
7.
Doubly Linked Lists
8.
Linked Lists with Header and Trailer Nodes
9.
Circular Linked Lists
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
2
1. Introduction

A linked list is a collection of components, called nodes.

Every node (except the last node) contains the address of the
next node.

Every node has two components: data to store the relevant
information, and link to store the address of the next node.
struct nodeType
info
link
{
int info;
Structure of a node
nodeType *link;
};
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
3
1. Introduction
head
node1
node2
17
92
node3
node4
93
45
NULL
Memory locations
2000
head
2000
17
2800
2800
node1
92
1500
1500
node2
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
63
3600
3600
node3
45
0
node4
4
2. Properties
head
2000
2800
2000
17
2800
92
1500
63
3600
45
0
info
link
info
link
info
link
info
link
Variable
head
1500
3600
Value
2000
head->info
17
head->link
2800
head->link->info
92
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
5
2. Properties
nodeType *current;
current = head;
head
2000
2800
1500
3600
2000
17
2800
92
1500
63
3600
45
0
info
link
info
link
info
link
info
link
current
Variable
current
Value
2000
current->info
17
current->link
2800
current->link->info
92
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
6
2. Properties
head
2000
2800
1500
3600
2000
17
2800
92
1500
63
3600
45
0
info
link
info
link
info
link
info
link
current
current = current->link;
head
2000
2800
2000
17
2800
92
1500
63
3600
45
0
info
link
info
link
info
link
info
link
current
1500
3600
current
2800
current->info
92
current->link
1500
current->link->info
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
63
7
2. Properties
head
2000
2800
1500
3600
2000
17
2800
92
1500
63
3600
45
0
info
link
info
link
info
link
info
link
current
head->link->link
1500
head->link->link->info
63
head->link->link->link
3600
head->link->link->link->info
current->link->link
45
3600
current->link->link->info
45
current->link->link->link
0 (that is, NULL)
current->link->link->link->info
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
Does not exist
8
2. Properties: Traversing a Linked List


The basic operations of a linked list are:

Search the list to determine whether a particular item is in the list.

Insert an item in the list.

Delete an item from the list.
The operations require the list to be traversed.
current = head; // To keep head pointing always the first node.
while(current != NULL)
{
// Process current
current = current->link;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
9
2. Properties: Traversing a Linked List
head
2000
2800
1500
3600
2000
17
2800
92
1500
63
3600
45
0
info
link
info
link
info
link
info
link
current
current = head;
while(current != NULL) // to output the elements of the linked list
{
cout<<current->info<<" ";
current = current->link;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
10
3. Insertion And Deletion: Insertion
struct nodeType
{
int info;
nodeType *link;
};
…
nodeType *head, *p, *q, *newNode;
head
45
65
34
76
65
34
76
p
newNode = new nodeType;
newNode->info = 50;
head
45
p
newNode
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
50
11
3. Insertion And Deletion: Insertion
newNode = new nodeType;
newNode->info = 50;
head
45
?
head
45
65
34
76
p
newNode
65
50
34
76
p
50
newNode->link = p->link;
newNode
p->link = newNode;
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
12
3. Insertion And Deletion: Insertion
What happen if we inverse the order of the two statements?
p->link = newNode;
newNode->link = p->link;
p->link = newNode;
newNode->link = p->link;
head
45
65
34
76
p
50
newNode
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
13
3. Insertion And Deletion: Insertion
Using two pointers, we can simplify the insertion code.
head
45
65
34
76
q
p
50
newNode
The order of the two statements is not important.
newNodep->link = q ;
p->link = newNode;
p->link = newNode;
newNodep->link = q ;
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
14
3. Insertion And Deletion: Deletion
Consider the following linked list. Suppose that the node with
info 34 is to be deleted from the list.
head
45
65
34
76
p
p->link = p->link->link; // the link of the node with info 65 will point the node with info 76
head
45
65
34
76
p
The node still in memory
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
15
3. Insertion And Deletion: Deletion
To delete the node with info 34 from the list and deallocate the
memory occupied by this node:
head
45
65
34
76
q
p
q = p->link;
p->link = q->link;
delete q;
head
45
65
76
p
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
16
4. Building a Linked List: Forward
nodeType *first, *last, *newNode;
int num;
first = NULL; // to point the first node
last = NULL; // to point the last node
cin>>num;
newNode = new nodeType; // Fill the newNode fields
newNode->info = num;
newNode->link = NULL;
if(first == NULL) // empty list
{
first = newNode;
last = newNode;
}
else // to insert a node at the end of the list
{
last->link = newNode;
last = newNode;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
17
4. Building a Linked List: Forward
Exercise:
Write
a
function
named
buildListForward() with no parameter to
build a linked list (with many integers) and
returns a pointer to the first element. Each
new element has to be inserted at the end
of the list.
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
18
4. Building a Linked List: Forward
// Solution
nodeType * buildListForward()
{
nodeType *first, *last, *newNode;
int num;
cout<<"Enter a list of integers ending by -1: ";
cin>>num;
first = NULL; // to point the first node
// here a loop to read many elements terminated by -1
return first;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
19
4. Building a Linked List: Forward
while(num != -1)
// the loop
{
newNode = new nodeType;
assert(newNode != NULL);
newNode->info = num;
newNode->link = NULL;
if(first == NULL) // empty list
{
first = newNode;
last = newNode;
}
else // to insert a node at the end of the list
{
last->link = newNode;
last = newNode;
}
cin>>num;
} // end while
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
20
4. Building a Linked List: Backwards
 Algorithm to insert a new node at the beginning of linked list:
1. Initialize first to NULL.
2. For each item in the list:
A. Create the new node, newNode.
B. Store the item in newNode ().
C. Inset newNode before first.
D. Update the value of the pointer first
 Question: convert the above algorithm to C++ code.
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
21
4. Building a Linked List: Backwards
nodeType* buildListBackward()
{
nodeType *first, *newNode;
int num;
cout<<"Enter a list of integers ending by -1: ";
cin>>num;
first = NULL; // to point the first node
while(num != -1)
{
newNode = new nodeType;
assert(newNode != NULL);
newNode->info = num;
newNode->link = first;
first = newNode;
cin>>num;
} // end while
return first;
// Solution
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
22
5. Linked List As an ADT

Starting from this section, the linked lists will be treated as an ADT by the
use of templates (general type will be used for the info field).

The basic operations on linked lists are:
1. Initialize the list
7. Retrieve the info contained in the
last node
2. Check whether the list is empty
8. Search the list for a given item
3.Output the list
9. Insert an item in the list
4. Find the length of the list
10. Delete an item from the list
5. Destroy the list
11. Make a copy of the linked list
6. Retrieve the info contained in the
first node
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
23
5. Linked List As an ADT
template<class Type>
// Part I
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template<class Type>
class linkedListType
{
friend ostream& operator<<(ostream&, const linkedListType<Type>&);
public:
const linkedListType<Type>& operator=(const linkedListType<Type>&);
void initializeList();
bool isEmtyList();
int length();
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
24
5. Linked List As an ADT
// Part II
void destroyList();
Type front();
Type back();
bool serach(const Type& searchItem);
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode(const Type& deleteItem);
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
~linkedListType();
protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const linkedListType<Type> otherList);
};
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
25
5. Linked List As an ADT
linkedListType<Type> list;
list
nodeType<Type> objects
node1
node2
nodecount
…
first
info link
info link
info link
last
count
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
26
5. Linked List As an ADT: Remarks

The data members of the class linkedListType are protected , not private,
because other classes will be derived from this class.

The function copyList is declared private because we use this function
only to implement the copy constructor and the function to overload the
assignment operator.

The definition of the class linkedListType includes a member function to
overload the assignment operator. This must be done for every class
including pointer data members.

For the same reason, the class linkedListType includes a copy
constructor.
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
27
5. Linked List As an ADT: isEmptyList() and default Constructor
template<class Type>
bool linkedListType<Type>::isEmtyList()
{
return(first == NULL);
}
template<class Type>
linkedListType<Type>::linkedListType()
{
first = NULL;
last = NULL;
count = 0;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
28
5. Linked List As an ADT: destroyList()
first
45
65
34
76
last
first
45
65
34
temp
first
76
last
45
65
34
temp
76
last
first
65
34
76
last
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
29
5. Linked List As an ADT: destroyList()
template<class Type>
void linkedListType<Type>::destroyList()
{
nodeType<Type> *temp;
while(first != NULL)
{
temp = first;
first = first->link;
delete temp;
}
last = NULL;
count = 0;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
30
5. Linked List As an ADT: initializeList()
template<class Type>
void linkedListType<Type>::initializeList()
{
destroyList();
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
31
5. Linked List As an ADT: << operator
template<class Type>
ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)
{
nodeType<Type> *current; // pointer to traverse the list
current = list.first;
while(current != NULL)
{
osObject<<current->info<<" ";
current = current->link;
}
return osObject;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
32
5. Linked List As an ADT: length() and front()
template<class Type>
int linkedListType<Type>::length()
{
return count;
}
template<class Type>
Type linkedListType<Type>::front()
{
assert(last != NULL);
return first->info;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
33
5. Linked List As an ADT: back()
template<class Type>
Type linkedListType<Type>::back()
{
assert(last != NULL);
return last->info;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
34
5. Linked List As an ADT: searchItem()
Algorithm
The member function search searches for a given item. If the item is
found, it returns true. The search must start from the first node.
1.
Compare the search item with the current node in the list. If
the info of the current node is the same as the search item,
stop the search; otherwise, make the next node the current
node.
2.
Repeat step 1 until either the item is found or no more data is
left in the list to compare with the search item.
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
35
5. Linked List As an ADT: searchItem()
template<class Type>
bool linkedListType<Type>::search(const Type& searchItem)
{
nodeType<Type> *current;
bool found;
current = first;
found = false;
while(current != NULL && !found)
if(current->info == searchItem)
found = true;
else
current = current->link;
return found;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
36
5. Linked List As an ADT: insertFirst()
Algorithm
The member function insertFirst inserts the new item at the
beginning of the list. The Algorithm is:
1.
Create a new node.
2.
If unable to create the node, terminate the program.
3.
Store the new item in the new node.
4.
Insert the node before first.
5.
Increment count by 1.
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
37
5. Linked List As an ADT: insertFirst()
template<class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
assert(newNode != NULL);
newNode->info = newItem;
newNode->link = first;
first = newNode;
count++;
if(last == NULL)
last = newNode;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
38
5. Linked List As an ADT: insertLast()
template<class Type>
void linkedListType<Type>::insertLast(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
assert(newNode != NULL);
newNode->info = newItem;
newNode->link = NULL;
if(first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
count++;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
39
5. Linked List As an ADT: deleteNode()
first
Case1:deleteNode(10)
last
count
0
first
Case2: deleteNode(10)
76
10
34
76
25
34
76
3
first
last
count
3
first
Case4: deleteNode(10)
34
last
count
Case3: deleteNode(34)
or deleteNode(76)
10
last
count
3
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
40
5. Linked List As an ADT: deleteNode(): ALGORITHM
if the list is empty
Output(cannot delete from an empty list);
else
{
if the first node is the node with the given info,
adjust first, last (if necessary), count, and deallocate the memory;
else
{
search the list for the node with the given info
if such a node is found, delete it and adjust the values of last (if
necessary), and count.
}
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
41
5. Linked List As an ADT: deleteNode()
Program
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
42
5. Linked List As an ADT: copyList()
Algorithm
•
•
•
copyList makes an identical copy of a linked list.
We traverse the list to be copied starting from the first node.
For each node in the original list, we:
1.
Create a new node.
2.
Copy the info of the node (in the original list) into the new
node.
3.
Insert the new node at the end of the list being created.
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
43
5. Linked List As an ADT: copyList()
Program
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
44
5. Linked List As an ADT: Destructor and Copy Constructor
template<class Type>
linkedListType<Type>::~linkedListType() //destructor
{
destroyList();
}
template<class Type>
linkedListType<Type>::linkedListType(const linkedList<Type>& otherList)
{
first = NULL;
copyList(otherList);
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
45
5. Linked List As an ADT: Overloading the Assignment Operator
template<class Type>
const linkedListType<Type>& linkedListType<Type> ::operator=
(const linkedListType<Type>& otherList)
{
if(this != &otherList) //avoid self-copy
copyList(otherList);
return *this;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
46
6. Ordered Linked Lists

This section discusses how to build ordered linked list

The basic operations on linked lists are:
1. Initialize the list
6. Search the list for a given item
2. Check whether the list is empty
7. Insert an item in the list
3.Output the list
8. Delete an item from the list
4. Output the list
9. Find the length of the list
5. Destroy the list
10. Make a copy of the list
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
47
6. Ordered Linked Lists
template<class Type>
class orderedLinkedListType: public linkedListType<Type>
{
public:
bool serach(const Type& searchItem);
void insertNode(const Type& newItem);
void deleteNode(const Type& newItem);
};
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
48
6. Ordered Linked Lists: Remarks

The
class
orderedLinkedListType
is
derived
from
the
class
linkedListType.

The elements of the ordered list are to be ordered according to a criteria.

Some functions can be used from the class linkedListType (like
insertFirst and insertLast.

No need for the pointer last (it is set to NULL) because the elements are
ordered.

The function back is not to be used to return the last element because
the pointer last is set to NULL.
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
49
6. Ordered Linked Lists: search()
Algorithm
The function search is similar to the search function for general lists.
Here because the list is sorted, we can improve the search algorithm.
1.
Compare the search item with the current node in the list. If
the info of the current node is greater than or equal to the
search item, stop the search; otherwise, make the next node
the current node.
2.
Repeat step 1 until either an item in the list is greater than or
equal to the search item is found, or no more data is left in the
list to compare with the search item.
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
50
6. Ordered Linked Lists: search()
template<class Type>
bool orderedLinkedListType<Type>::search(const Type& searchItem)
{
bool found;
nodeType<Type> *current;
found = false;
current = first;
while(current != NULL && !found)
if(current->info >=searchItem)
found = true;
else
current = current->link;
if(found)
found = (current->info == searchItem);
return found;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
51
6. Ordered Linked Lists: insertNode()

Case 1: The list is initially empty: insert the new item at the
beginning and increment count by 1.

Case 2: The list is not empty and the new item is smaller than the
smallest in the list: insert the new item at the beginning and
increment count by 1.

Case 3: The list is not empty and the new item is larger than the
first item.

Case 3a: The new item is larger than the last item. Insert the new
item at the end of the list and increment count by 1.

Case 3b: The new item is to be inserted somewhere in the middle.
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
52
6. Ordered Linked Lists: insertNode()
Program
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
53
6. Ordered Linked Lists: deleteNode()

Case 1: The list is initially empty. We have an error.

Case 2: The item to be deleted is contained in the first node of the
list. We must adjust the head pointer of the list – first .

Case 3: The item to be deleted is somewhere in the list. In this
case, current points to the node containing the item to be deleted,
and trailCurrent points to the node just before the node pointed to
by current.

Case 4: The list is not empty, but the item to be deleted is not in
the list.

After deleting the node, count is decremented by 1.
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
54
6. Ordered Linked Lists: deleteNode()
Program
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
55
7. Doubly Linked Lists
A doubly linked list is a linked list in which every node has a next
pointer and a back pointer.
first
15
20
39
last
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
56
7. Doubly Linked Lists: Class Part I
template<class Type>
struct nodeType
{
Type info;
nodeType<Type> *next;
nodeType<Type> *back;
};
template<class Type>
class doublyLinkedList
{
friend ostream& operator<< (ostream&, const doublyLinkedList<Type>&);
public:
const doublyLinkedList<Type>& operator=(const doublyLinkedList<Type>&);
void initializeList();
bool isEmptyList();
void destroy();
void reversePrint();
int length();
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
57
7. Doubly Linked Lists: Class Part II
Type front();
Type back();
bool search(const Type& searchItem);
void insertNode(const Type& insertItem);
void deleteNode(const Type& deleteItem);
doublyLinkedList();
doublyLinkedList(const doublyLinkedList<Type>& otherList);
protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const doublyLinkedList<Type>& otherList);
};
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
58
7. Doubly Linked Lists: Default constructor and isEmptyList
template<class Type>
doublyLinkedList<Type>::doublyLinkedList()
{
first = NULL;
last = NULL;
count = 0;
}
template<class Type>
bool
doublyLlinkedList<Type>::isEmtyList()
{
return(first == NULL);
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
59
7. Doubly Linked Lists: destroy
template<class Type>
void doublyLinkedList<Type>::destroy()
{
nodeType<Type> *temp;
while(first != NULL)
{
temp = first;
first = first->next;
delete temp;
}
last = NULL;
count = 0;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
60
7. Doubly Linked Lists: initializeList and length
template<class Type>
void doublyLinkedList<Type>::initializeList()
{
destroyList();
}
template<class Type>
int doublyLinkedList<Type>::length()
{
return count;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
61
7. Doubly Linked Lists: operator<<
template<class Type>
ostream& operator<<(ostream& osObject, const doublyLinkedList<Type>& list)
{
nodeType<Type> *current; // pointer to traverse the list
current = list.first;
while(current != NULL)
{
cout<<current->info<<" ";
current = current->next;
}
return osObject;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
62
7. Doubly Linked Lists: reversePrint
template<class Type>
void doublyLinkedList<Type>::reversePrint()
{
nodeType<Type> *current;
current = last;
while(current != NULL)
{
cout<<current->info<<" ";
current = current->back;
}
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
63
7. Doubly Linked Lists: search
template<class Type>
void doublyLinkedList<Type>::search(const Type& searchItem)
{
bool found = false;
nodeType<Type> *current;
current = first;
while(current != NULL && !found)
if(current->info >= searchItem)
found true;
else
current = current->next;
if (found)
found = (current->info == searchItem);
return found;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
64
7. Doubly Linked Lists: front and back
template<class Type>
void doublyLinkedList<Type>::front()
{
assert(first != NULL);
return first->info;
}
template<class Type>
void doublyLinkedList<Type>::back()
{
assert(first != NULL);
return last->info;
}
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
65
7. Doubly Linked Lists: insertNode
Program
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
66
7. Doubly Linked Lists: deleteNode
Program
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
67
8. Linked Lists with Header and Trailer Nodes

Insertion and deletion of items from a linked list includes special
cases: insertion/deletion at the beginning or in an empty list.

Hint: never insert before the first item or after the last item and
never delete the first node.

In the case of an ordered list, set up two virtual nodes header at
the beginning and trailer at the end.
first
first
A
Ahmad
A
zzzzzzzz
Ali
Marwa
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
zzzzzzzz
68
8. Linked Lists with Header and Trailer Nodes
The usual operations on a linked list with header and trailer
nodes are:
1. Initialize the list
7. Retrieve the info contained in the
last node
2. Check whether the list is empty
8. Search the list for a given item
3.Output the list
9. Insert an item in the list
4. Find the length of the list
10. Delete an item from the list
5. Destroy the list
11. Make a copy of the linked list
6. Retrieve the info contained in the
first node
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
69
8. Linked Lists with Header and Trailer Nodes
Exercise
1. Write the definition of the class that defines a linked
list with header and trailer nodes as an ADT.
2. Write the definitions of the member functions of the
class defined in 1 (you may assume that the
elements of the linked list with header and trailer
nodes are sorted in an ascending order).
3. Write a program to test various operations of the
class defined in 1.
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
70
8. Linked Lists with Header and Trailer Nodes
Exercise
1. Write the definition of the class that defines a linked
list with header and trailer nodes as an ADT.
2. Write the definitions of the member functions of the
class defined in 1 (you may assume that the
elements of the linked list with header and trailer
nodes are sorted in an ascending order).
3. Write a program to test various operations of the
class defined in 1.
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
71
9. Circular Linked Lists
A circular linked list is a linked list in which the last node points to the first
node.
first
34
first
34
20
15
-12
first
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
72
9. Circular Linked Lists
The usual operations on a circular linked list are:
1. Initialize the list
7. Retrieve the info contained in the
last node
2. Check whether the list is empty
8. Search the list for a given item
3.Output the list
9. Insert an item in the list
4. Find the length of the list
10. Delete an item from the list
5. Destroy the list
11. Make a copy of the linked list
6. Retrieve the info contained in the
first node
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
73
9. Circular Linked Lists
Exercise
1. Write the definition of the class that defines a sorted
circular linked list as an ADT.
2. Write the definitions of the member functions of the
class defined in 1 (you may assume that the
elements are sorted in an ascending order).
3. Write a program to test various operations of the
class defined in 1.
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
74
Download