Chapter 5 - PowerPoint PPT Presentation

chapter 5 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 5 PowerPoint Presentation
play fullscreen
1 / 74
Chapter 5
245 Views
Download Presentation
gay-sears
Download Presentation

Chapter 5

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Chapter 5 Linked Lists Dr. YoussefHarrath yharrath@uob.edu.bh

  2. 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. 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. structnodeType { int info; nodeType *link; }; info link Structure of a node Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  4. 1. Introduction head node1 node2 node3 node4 17 92 93 45 NULL Memory locations 2000 2800 1500 3600 17 2800 92 1500 63 3600 45 0 head 2000 node1 node2 node3 node4 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  5. 2. Properties 2000 2800 1500 3600 head 17 2800 92 1500 63 3600 45 0 2000 info link info link info link info link Value 2000 17 2800 92 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  6. 2. Properties nodeType *current; current = head; 2000 2800 1500 3600 head 17 2800 92 1500 63 3600 45 0 2000 info link info link info link info link current Value 2000 17 2800 92 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  7. 2. Properties 2000 2800 1500 3600 head current = current->link; 17 2800 92 1500 63 3600 45 0 2000 info link info link info link info link current 2000 2800 1500 3600 head 17 2800 92 1500 63 3600 45 0 2000 info link info link info link info link current Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  8. 2. Properties 2000 2800 1500 3600 head 17 2800 92 1500 63 3600 45 0 2000 info link info link info link info link current 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 = head; while(current != NULL) // to output the elements of the linked list { cout<<current->info<<" "; current = current->link; } 2000 2800 1500 3600 head 17 2800 92 1500 63 3600 45 0 2000 info link info link info link info link current Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  11. 3. Insertion And Deletion: Insertion structnodeType { int info; nodeType *link; }; … nodeType *head, *p, *q, *newNode; newNode = new nodeType; newNode->info = 50; head 45 65 34 76 p head 45 65 34 76 p newNode 50 Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  12. 3. Insertion And Deletion: Insertion newNode = new nodeType; newNode->info = 50; ? head 45 65 34 76 p head 45 65 34 76 50 newNode->link = p->link; p->link = newNode; p newNode newNode 50 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; head 45 65 34 76 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 q p 50 newNode The order of the two statements is not important. newNodep->link = q ; p->link = newNode; newNodep->link = q ; p->link = newNode; 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. 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 head 45 65 34 76 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: 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

  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 = newnodeType; // 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

  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 = newnodeType; 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

  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 = newnodeType; 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

  23. 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: Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  24. 5. Linked List As an ADT template<class Type> // Part I structnodeType { Type info; nodeType<Type> *link; }; template<class Type> classlinkedListType { friendostream& operator<<(ostream&, constlinkedListType<Type>&); public: constlinkedListType<Type>& operator=(constlinkedListType<Type>&); voidinitializeList(); boolisEmtyList(); int length(); Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  25. 5. Linked List As an ADT voiddestroyList(); // Part II Type front(); Type back(); boolserach(const Type& searchItem); voidinsertFirst(const Type& newItem); voidinsertLast(const Type& newItem); voiddeleteNode(const Type& deleteItem); linkedListType(); linkedListType(constlinkedListType<Type>& otherList); ~linkedListType(); protected: int count; nodeType<Type> *first; nodeType<Type> *last; private: voidcopyList(constlinkedListType<Type> otherList); }; Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  26. 5. Linked List As an ADT linkedListType<Type> list; nodeType<Type> objects list node1 node2 nodecount … first info link info link info link last count 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> boollinkedListType<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() first 45 65 34 76 last first 45 65 34 76 temp last first 45 65 34 76 temp last first 65 34 76 last Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  30. 5. Linked List As an ADT: destroyList() template<class Type> voidlinkedListType<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> voidlinkedListType<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, constlinkedListType<Type>& list) { nodeType<Type> *current; // pointer to traverse the list current = list.first; while(current != NULL) { osObject<<current->info<<" "; current = current->link; } returnosObject; } Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  33. 5. Linked List As an ADT: length() and front() template<class Type> intlinkedListType<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> boollinkedListType<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> voidlinkedListType<Type>::insertFirst(const Type& newItem) { nodeType<Type> *newNode; newNode = newnodeType<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> voidlinkedListType<Type>::insertLast(const Type& newItem) { nodeType<Type> *newNode; newNode = newnodeType<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

  40. 5. Linked List As an ADT: deleteNode() first first first 10 25 10 34 34 34 76 76 76 Case1:deleteNode(10) last last last count count count 3 3 3 Case2: deleteNode(10) first last Case3: deleteNode(34) ordeleteNode(76) count 0 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; 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

  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(); } template<class Type> linkedListType<Type>::linkedListType(constlinkedList<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> constlinkedListType<Type>& linkedListType<Type> ::operator= (constlinkedListType<Type>& otherList) { if(this != &otherList) //avoid self-copy copyList(otherList); return *this; } Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  47. 6. Ordered Linked Lists • This section discusses how to build ordered linked list • The basic operations on linked lists are: Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

  48. 6. Ordered Linked Lists template<class Type> classorderedLinkedListType: publiclinkedListType<Type> { public: boolserach(const Type& searchItem); voidinsertNode(const Type& newItem); voiddeleteNode(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 equalto 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