1 / 85

Data Structures Using C++ 2E

Data Structures Using C++ 2E. Chapter 5 Linked Lists. Edited by Malak Abdullah Jordan University of Science and Technology. Objectives. Learn about linked lists Become aware of the basic properties of linked lists Explore the insertion and deletion operations on linked lists

junkoj
Télécharger la présentation

Data Structures Using C++ 2E

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures Using C++ 2E Chapter 5 Linked Lists Edited by Malak Abdullah Jordan University of Science and Technology

  2. Objectives Learn about linked lists Become aware of the basic properties of linked lists Explore the insertion and deletion operations on linked lists Discover how to build and manipulate a linked list

  3. Objectives (cont’d.) Learn how to construct a doubly linked list Discover how to use the STL container list Learn about linked lists with header and trailer nodes Become aware of circular linked lists

  4. Lists 1 7 9 17 33 47 57 To delete or to add an item you need to shift the contents of the array to right or to left...

  5. Linked Lists Collection of components (nodes) Every node (except last) Contains address of the next node Node components Data: stores relevant information Link: stores address FIGURE 5-1 Structure of a node

  6. Linked Lists (cont’d.) Head (first) : a pointer of node type Address of the first node in the list Arrow points to node address Stored in node Down arrow in last node indicates NULL link field FIGURE 5-2 Linked list FIGURE 5-3 Linked list and values of the links

  7. Linked Lists (cont’d.) FIGURE 5-1 Structure of a node head is a pointer of nodeType Type Which means that it will point to an object of nodeType struct Each object (node) has two components info(data) link

  8. Linked Lists: Some Properties Head stores address of first node, Info stores information Link stores address of next node, Assume info type int TABLE 5-1 Values of head and some of the nodes of the linked list in Figure 5-4 FIGURE 5-4 Linked list with four nodes

  9. Example info Link pointer head Null 5 77 12 node nodeType *head; head= new nodeType; head info=5; headlink=new nodeType; headlinkinfo=77; headlinklink=new nodeType; headlinklinkinfo=12; headlinklinklink=NULL; (*head) . info 5 head info 5 (*(*head) . link). info 77 head link info 77

  10. Linked Lists: Some Properties (cont’d.) Pointer current: same type as pointer head current = head; Copies value of head into current current = current->link; Copies value of current->link (2800) into current FIGURE 5-5 List after the statement current = current->link; executes

  11. nodeType *current = head; current = current->link;

  12. Traversing a Linked List Basic linked list operations Search list to determine if particular item is in the list Insert item in list Delete item from list These operations require list traversal Given pointer to list first node, we must step through list nodes If we move head to the next node we will loose the real first node...so don’t move head pointer...

  13. Traversing a Linked List (cont’d.) Suppose head points to a linked list of numbers Code outputting data stored in each node head Null 5 77 12 current

  14. Item Insertion and Deletion Generic definition of a node on page 270 TABLE 5-3 Inserting a node in a linked list Data Structures Using C++ 2E

  15. p head 3 4 5 8 nodeType *N; N= new nodeType; Ninfo = 7; 7 N Nlink = plink; Plink= N;

  16. Item Insertion and Deletion (cont’d.) Sequence of statements to insert node Very important Use only one pointer (p) to adjust links of the nodes Don’t do the following p link = newNode newNode link= plink Using two pointers Can simplify insertion code somewhat

  17. Using two pointers p and q to add head Null 5 77 12 p q New node N 20 head Null 5 77 12 p q New node N 20

  18. Item Insertion and Deletion (cont’d.) Memory still occupied by node after deletion Memory is inaccessible Deallocate memory using a pointer to this node FIGURE 5-10 List after the statement p->link = p->link->link; executes

  19. head Delete 77 Null 5 77 12 p q head Null 5 77 12 p q Delete q; head Null 5 12

  20. Building a Linked List If linked list data unsorted Linked list unsorted Ways to build linked list Forward New node always inserted at end of the linked list See example on page 274 See function buildListForward on page 277 Backward New node always inserted at the beginning of the list See example on page 277 See function buildListBackward on page 278

  21. Add forward and backward head Null 5 12 20 head Backward Null 5 12 2 1 20 head Forward 5 12 20 Null

  22. Linked List as an ADT 11 basic operations Two types of linked lists: sorted, unsorted class linkedListType Implements basic linked list operations as an ADT Derive two classes using inheritance unorderedLinkedList and orderedLinkedList Unordered linked list functions buildListForward and buildListBackward Two more functions accommodate both operations insertFirst and insertLast

  23. Structure of Linked List Nodes Node has two instance variables Simplify operations (insert, delete) Define class to implement linked list node as a struct Definition of the struct nodeType

  24. Member Variables of the class linkedListType class linkedListType Three instance variables first 22 15 37 last count 3 Null

  25. Linked List as an ADT (cont’d.) class linkedListType { public: void initializeList() {destroyList();} bool isEmpty() { return(first==NULL);} void print(); //next slides int length() {return count;} void destroyList(); //next slides int front() {return first->info;} int back() {return last->info;} virtual bool search(int searchItem)=0; virtual void insertFirst(int newItem)=0; virtual void insertLast(int newItem)=0; virtual void deleteNode(int newItem)=0; linkedListIterator begin() {linkedListIterator temp(first); return temp;} linkedListIterator end() {linkedListIterator temp(NULL); return temp;} linkedListType() {first=NULL; last=NULL; count=0;} linkedListType(linkedListType & otherList);{first= NULL; copyList();} ~linkedListType(); {destroyList();} protected: int count; nodeType *first; nodeType *last; private: void copyList(); //for you

  26. Linked List Iterators (cont’d.) Abstract class linkedListType Defines basic properties of a linked list as an ADT See code on page 282 Empty list: first is NULL Definition of function isEmptyList

  27. Linked List as an ADT (cont’d.) Default constructor Initializes list to an empty state Length of a list Number of nodes stored in the variable count Function length Returns value of variable count first Null last count 0 27

  28. Linked List as an ADT (cont’d.) Destroy the list Deallocates memory occupied by each node void destroyList() { nodeType *Temp; while(first!=NULL) { Temp=first;first=first->link;delete Temp; } last=NULL;count=0; } first 22 15 37 last count 3 Null Temp 28

  29. Linked List as an ADT (cont’d.) Destroy the list Deallocates memory occupied by each node void destroyList() { nodeType *Temp; while(first!=NULL) { Temp=first;first=first->link;delete Temp; } last=NULL;count=0; } first 22 15 37 last count 2 Null Temp 29 Then continue..

  30. Linked List as an ADT (cont’d.) Initialize the list Reinitializes list to an empty state Must delete the nodes (if any) from the list 1- (Destroy list) 2- Default constructor, copy constructor Initialized list when list object declared first Null last count 0 30

  31. Linked List as an ADT (cont’d.) Print the list void print() { nodeType *current; current=first; while(current!=NULL) {cout<<current->info<<“ “; current=current->link;} } Retrieve the data of the first node Function front Returns the info contained in the first node If list is empty, assert statement terminates program first 22 15 37 last count 3 Null current

  32. Linked List as an ADT (cont’d.) Retrieve the data of the last node Function back Returns info contained in the last node If list is empty, assert statement terminates program Begin and end Function begin returns an iterator to the first node in the linked list Function end returns an iterator to the last node in the linked list

  33. Copy the list C L first 3 8 5 1 4 last ListObj . copyList(L);

  34. Linked List as an ADT (cont’d.) Destructor When class object goes out of scope Deallocates memory occupied by list nodes Memory allocated dynamically Resetting pointers first and last Does not deallocate memory Must traverse list starting at first node Delete each node in the list Calling destroyList destroys list

  35. Linked List as an ADT (cont’d.) Copy constructor Makes identical copy of the linked list Function copyListc checks whether original list empty Checks value of first Must initialize first to NULL Before calling the function copyList Overloading the assignment operator Similar to copy constructor definition

  36. TABLE 5-6 Time-complexity of the operations of the class linkedListType

  37. Linked List Iterators To process each node Must traverse list starting at first node Iterator Object producing each element of a container One element at a time Operations on iterators: ++ and * See code on pages 280-281 class linkedListType Functions of class linkedListIterator 37

  38. Linked List Iterators class linkedListIterator { public: linkedListIterator(); {current=NULL;} linkedListIterator(nodeType *ptr); {current=ptr;} int operator*(); {return current->info;} linkedListIterator operator++(); {current=current->link; return *this;} bool operator==(linkedListIterator & right); {return (current==right.current); } bool operator!=(linkedListIterator & right); {return (current!=right.current); } private: nodeType *current; }; L 3 8 5 1 4 first LinkedListIterator obj(L.first); last Cout<<*obj; //3 count obj++; current 38

  39. Unordered Linked Lists Derive class unorderedLinkedList from the abstract class linkedListType Implement the operations search, insertFirst, insertLast, deleteNode See code on page 292 Defines an unordered linked list as an ADT class unorderedLinkedList LinkedListType UnorderedLinkedList OrederedLinkedList

  40. Unordered Linked Lists class unorderedLinkedList : public linkedListType { bool search(int searchItem) void insertFirst(int item); void insertlast(int item); void deleteNode(int deleteItem); //later on }; 40

  41. Unordered Linked Lists (cont’d.) Search the list Steps Step one: 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 Step two: Repeat Step one until either the item is found or no more data is left in the list to compare with the search item See function search on page 293

  42. Unordered Linked Lists bool search(int searchItem) { nodeType *current; bool found =false; current=first; while(current !=NULL && !found) { if (current ->info == searchItem) found =true; else current=current->link; } return found; } first 3 8 5 1 4 last current 42

  43. Unordered Linked Lists (cont’d.) Insert the first node Steps 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 one See function insertFirst on page 294

  44. Unordered Linked Lists (cont’d.) void insertFirst(int item); { nodeType *newNode; newNode= new NodeType; newNode->info=newItem; newNode->link=first; first=newNode; count++; if (last==NULL) last=newNode; } void insertlast(int item); { nodeType *newNode; newNode= new NodeType; newNode->info=newItem; last->link=newNode; last=newNode; count++; if (first==NULL) first=newNode; } first 8 5 1 4 3 last newNode 44

  45. Unordered Linked Lists (cont’d.) Insert the last node Similar to definition of member function insertFirst Insert new node after last See function insertLast on page 294

  46. Unordered Linked Lists (cont’d.) Delete a node Consider the following cases: The list is empty The node is nonempty and the node to be deleted is the first node The node is nonempty and the node to be deleted is not the first node, it is somewhere in the list The node to be deleted is not in the list See pseudocode on page 295 See definition of function deleteNode on page 297 case1 case2 case3 case4

  47. Case 1 For Deleting • The List is empty------- no delete • The node is nonempty and the node to be deleted is the first node Case 2 For Deleting last first 3 8 5 1 4 last first 3 8 5 1 4 p Delete p

  48. Case 3 For Deleting last • The list is nonempty and the node to be deleted is not the first node, it is somewhere in the list first 3 8 5 1 4 p q last first 3 8 5 1 4 p q Delete q Case 4 For Deleting • The node to be deleted is not in the list ---- no delete

  49. TABLE 5-7 Time-complexity of the operations of the class unorderedLinkedList Unordered Linked Lists (cont’d.)

  50. Header File of the Unordered Linked List Create header file defining class unorderedListType See class unorderedListType code on page 299 Specifies members to implement basic properties of an unordered linked list Derived from class linkedListType

More Related