Download presentation
Presentation is loading. Please wait.
1
Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh
Chapter 5 Linked Lists Dr. Youssef Harrath
2
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
Outline Introduction Properties Insertion and Deletion Building a Linked List Linked List As an ADT Ordered Linked Lists Doubly Linked Lists Linked Lists with Header and Trailer Nodes Circular Linked Lists Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
3
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
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 { int info; nodeType *link; }; info link Structure of a node Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
4
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
1. Introduction 92 93 17 45 head node1 node4 node3 node2 NULL 92 1500 63 3600 17 2800 45 head 2000 node1 node4 node3 node2 Memory locations Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
5
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
2. Properties 92 1500 63 3600 17 2800 45 head 2000 info link Variable head head->info head->link head->link->info Value 2000 17 2800 92 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
6
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
2. Properties nodeType *current; current = head; current 92 1500 63 3600 17 2800 45 head 2000 info link Variable current current->info current->link current->link->info Value 2000 17 2800 92 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
7
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
2. Properties current 92 1500 63 3600 17 2800 45 head 2000 info link current = current->link; current 92 1500 63 3600 17 2800 45 head 2000 info link current 2800 current->info 92 current->link 1500 current->link->info 63 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
8
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
2. Properties current 92 1500 63 3600 17 2800 45 head 2000 info link head->link->link 1500 head->link->link->info 63 head->link->link->link 3600 head->link->link->link->info 45 current->link->link current->link->link->info current->link->link->link 0 (that is, NULL) current->link->link->link->info Does not exist Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
9
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
10
2. Properties: Traversing a Linked List
current 92 1500 63 3600 17 2800 45 head 2000 info link 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
11
3. Insertion And Deletion: Insertion
struct nodeType { int info; nodeType *link; }; … nodeType *head, *p, *q, *newNode; 65 34 45 76 head p newNode = new nodeType; newNode->info = 50; 65 34 45 76 head p 50 newNode Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
12
3. Insertion And Deletion: Insertion
newNode = new nodeType; newNode->info = 50; 65 34 45 76 head p 50 newNode 65 34 45 76 head p 50 newNode ? newNode->link = p->link; p->link = newNode; Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
13
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; 65 34 45 76 head p 50 newNode Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
14
3. Insertion And Deletion: Insertion
Using two pointers, we can simplify the insertion code. head 45 65 34 76 p->link = newNode; newNodep->link = q ; p q 50 newNode The order of the two statements is not important. p->link = newNode; newNodep->link = q ; Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
15
3. Insertion And Deletion: Deletion
Consider the following linked list. Suppose that the node with info 34 is to be deleted from the list. 65 34 45 76 head p p->link = p->link->link; // the link of the node with info 65 will point the node with info 76 65 34 45 76 head p The node still in memory Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
16
3. Insertion And Deletion: Deletion
To delete the node with info 34 from the list and deallocate the memory occupied by this node: 65 34 45 76 head p q q = p->link; p->link = q->link; delete q; 65 45 76 head p Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
17
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; Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
18
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
19
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
20
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; cin>>num; } // end while Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
21
4. Building a Linked List: Backwards
Algorithm to insert a new node at the beginning of linked list: Initialize first to NULL. For each item in the list: Create the new node, newNode. Store the item in newNode (). Inset newNode before first. 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
22
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; } // end while return first; } // Solution Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
23
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
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
24
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
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
25
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
5. Linked List As an ADT void destroyList(); // Part II 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
26
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
5. Linked List As an ADT first last count info link … list linkedListType<Type> list; node1 node2 nodecount nodeType<Type> objects Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
27
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
28
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
29
5. Linked List As an ADT: destroyList()
65 34 45 76 first last 65 34 45 76 first temp last 65 34 45 76 first temp last 65 34 76 first last Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
30
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
31
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
32
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
33
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
34
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
35
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. 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. 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
36
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
37
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: Create a new node. If unable to create the node, terminate the program. Store the new item in the new node. Insert the node before first. Increment count by 1. Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
38
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
39
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; count++; Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
40
5. Linked List As an ADT: deleteNode()
first last count Case1:deleteNode(10) first last count 3 10 34 76 Case2: deleteNode(10) first last count 3 10 34 76 Case3: deleteNode(34) or deleteNode(76) first last count 3 25 34 76 Case4: deleteNode(10) Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
41
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; 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
42
5. Linked List As an ADT: deleteNode()
Program Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
43
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: Create a new node. Copy the info of the node (in the original list) into the new node. Insert the new node at the end of the list being created. Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
44
5. Linked List As an ADT: copyList()
Program Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
45
5. Linked List As an ADT: Destructor and Copy Constructor
template<class Type> linkedListType<Type>::~linkedListType() //destructor { destroyList(); } linkedListType<Type>::linkedListType(const linkedList<Type>& otherList) first = NULL; copyList(otherList); Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
46
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
47
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
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
48
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
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
49
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
50
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. 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. 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
51
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
52
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
53
6. Ordered Linked Lists: insertNode()
Program Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
54
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
55
6. Ordered Linked Lists: deleteNode()
Program Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
56
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
7. Doubly Linked Lists A doubly linked list is a linked list in which every node has a next pointer and a back pointer. 15 20 39 last first Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
57
7. Doubly Linked Lists: Class Part I
template<class Type> struct nodeType { Type info; nodeType<Type> *next; nodeType<Type> *back; }; 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
58
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
59
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
60
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
61
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
62
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
63
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
64
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
65
7. Doubly Linked Lists: front and back
template<class Type> void doublyLinkedList<Type>::front() { assert(first != NULL); return first->info; } void doublyLinkedList<Type>::back() return last->info; Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
66
7. Doubly Linked Lists: insertNode
Program Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
67
7. Doubly Linked Lists: deleteNode
Program Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
68
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. A first zzzzzzzz A Ahmad Ali Marwa first zzzzzzzz Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
69
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
70
8. Linked Lists with Header and Trailer Nodes
Exercise Write the definition of the class that defines a linked list with header and trailer nodes as an ADT. 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). Write a program to test various operations of the class defined in 1. Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
71
8. Linked Lists with Header and Trailer Nodes
Exercise Write the definition of the class that defines a linked list with header and trailer nodes as an ADT. 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). Write a program to test various operations of the class defined in 1. Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
72
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
9. Circular Linked Lists A circular linked list is a linked list in which the last node points to the first node. first first 34 34 20 15 -12 first Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
73
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
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
74
Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
9. Circular Linked Lists Exercise Write the definition of the class that defines a sorted circular linked list as an ADT. 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). Write a program to test various operations of the class defined in 1. Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.