Linked Lists Dr. Youssef Harrath

Slides:



Advertisements
Similar presentations
TK1924 Program Design & Problem Solving Session 2011/2012
Advertisements

Linked Lists Geletaw S..
COMP171 Fall 2005 Lists.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
Data Structures Using C++
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Data Structures & Algorithms
Chapter 4 ADT Sorted List.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Doubly Linked Lists CS 308 – Data Structures. Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Data Structures Using C++ 2E
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Chapter 7 Stacks Dr. Youssef Harrath
C++ Classes and Data Structures Jeffrey S. Childs
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
Data Structures Using Java1 Chapter 4 Linked Lists.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Graphs CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
Data Structures Using C++1 Chapter 5 Linked Lists.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
– Graphs 1 Graph Categories Strong Components Example of Digraph
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
Chapter 3 Pointers and Array-Based Lists Dr. Youssef Harrath
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Chapter 11 Binary Trees Dr. Youssef Harrath
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Chapter 16: Linked Lists.
Data Structures Using C++ 2E
C++ Programming:. Program Design Including
Review Deleting an Element from a Linked List Deletion involves:
UNIT-3 LINKED LIST.
C++ Plus Data Structures
Linked lists.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 20: Binary Trees.
Chapter 16-2 Linked Structures
Chapter 21: Binary Trees.
Data structures in C++.
Doubly Linked List Implementation
Chapter 16 Linked Structures
CSI 1340 Introduction to Computer Science II
Linked lists.
Presentation transcript:

Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

5. Linked List As an ADT: deleteNode() Program Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

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

5. Linked List As an ADT: copyList() Program Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

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

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

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

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

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

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

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

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

6. Ordered Linked Lists: insertNode() Program Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

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

6. Ordered Linked Lists: deleteNode() Program Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

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

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

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

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

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

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

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

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

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

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

7. Doubly Linked Lists: insertNode Program Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

7. Doubly Linked Lists: deleteNode Program Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

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

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

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

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

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

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

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