1 / 28

Linked List

Linked List. Chapter 17 Truly dynamic memory management. Motivation. A “List” is a very useful structure to hold a collection of data. Examples: List of students marks List of temperatures for a period of time Implementation in C++ using Static memory (array) – int marks[10];

loe
Télécharger la présentation

Linked List

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. Linked List Chapter 17 Truly dynamic memory management COMP103 - Linked Lists (Part A)

  2. Motivation • A “List” is a very useful structure to hold a collection of data. • Examples: • List of students marks • List of temperatures for a period of time • Implementation in C++ using • Static memory (array) – int marks[10]; Note: we need to know the size of the array • “Dynamic” memory – int n, *marks; Cin >> n; Marks = new int[n]; Note: once memory is allocated, the size of the array is fixed (i.e. static) • Linked list – truly dynamic COMP103 - Linked Lists (Part A)

  3. Motivation • Common operations on lists: • Traverse the list (useful in printing the list) • Search for an item • Add an item • Delete an item • Note: • Algorithms of these operations based on static arrays are not very efficient. Adding or deleting an item in the array involves a lot of copying of items in the list. And the actual memory “size” of the array remains fixed. • Using linked lists improves the efficiency. The size of the list can grow and shrink as we add or delete items from the list. COMP103 - Linked Lists (Part A)

  4. Linked Lists: Basic Idea 85 • A linked list is an ordered collection of data • Each element of the linked list has • Some data • A link to the next element • The link is used to chain (or connect) the data • Example: A linked list of integers: Link Data 20 45 75 COMP103 - Linked Lists (Part A)

  5. Linked Lists: Basic Idea • The list can grow and shrink List 20 45 add(75), add(85) 20 45 75 85 delete(85), delete(45), delete(75) 20 COMP103 - Linked Lists (Part A)

  6. Linked List Structure node =NULL • Node - Data + Link • Data - contains useful information • Link – chains(connects) the data together • Head pointer - points to the first element • Empty List - head pointer equals NULL COMP103 - Linked Lists (Part A)

  7. Nodes Data may be simple… …or more complex COMP103 - Linked Lists (Part A)

  8. Nodes in C++ data 10 Define the node type: struct NODE { int data; NODE *link; }; Simple: In the program: void main() { // p_new is of type NODE NODE *p_new; // allocating memory for one node p_new = new NODE; // assigning value to the data field in the node p_new->data = 10; // link points to NULL p_new->link = NULL; } COMP103 - Linked Lists (Part A)

  9. Nodes in C++ tel. no. ID key 5678 123 a Define the node type: struct DATA { char key; int ID; int tel; }; struct NODE { DATA data; NODE *link; }; Complex node: In the program: void main() { // p_new is of type NODE NODE *p_new; // allocating memory for one node p_new = new NODE; // assigning values to the data field in the node p_new->data.key = ‘a’; p_new->data.ID = 123; p_new->data.tel = 5678; // link points to NULL p_new->link = NULL; } COMP103 - Linked Lists (Part A)

  10. Linked List Order • Nodes in a linked list has an order • Nodes are usually ordered by a key, such as studentID • Unlike arrays, nodes in a linked list may not be stored in neighborhood memory cells • The first node of a list is kept by a head pointer (pHead), which must be kept safely • The last node of a list is a node that contains a NULL value in its link part COMP103 - Linked Lists (Part A)

  11. Linked Lists: Basic Operations • Transverse: visit each item in the list (useful for printing the list) • Search for an item in the list • Addan item to the list • Deletean item from the list COMP103 - Linked Lists (Part A)

  12. Linked List Traversal pWalker traverse (list) 1. Set pointer (pWalker) to the first node (pHead) in list 2. while (not end of the list) 2.1 process (current node, pWalker) 2.2 set pointer to next node end traverse When pWalker becomes NULL, it means that the end of the list is reached COMP103 - Linked Lists (Part A)

  13. Linked List Traversal pHead // Other statements NODE *pWalker; pWalker = pHead; while (pWalker) { cout << pWalker->data << endl; pWalker = pWalker->link; //points to next node } COMP103 - Linked Lists (Part A)

  14. Search for an item (target) in the list • target is in the list •  The search function returns true • pCurpoints to the node which contains target • pPrepoints to the preceding node 2 cases: (i) pPre==NULL, (ii) pPre!=NULL COMP103 - Linked Lists (Part A)

  15. Search for an item (target) in the list • target is NOT in the list • The search function returns false • pCurpoints to the node which should be aftertarget • pPrepoints to the node which should be beforetarget 3 cases: (i) pPre==NULL, (ii) pCur==NULL, (iii) pPre!=NULL AND pCur!=NULL COMP103 - Linked Lists (Part A)

  16. Algorithm for searching pPre = NULL; pCur = pHead; // Search until target <= list data key while (pCur && (target > pCur->data.key)) { pPre = pCur; pCur = pCur->link; } if (pCur && (target == pCur->data.key)) found = true; else found = false; return found; COMP103 - Linked Lists (Part A)

  17. Add an item: Array vs. Linked List 955992 Ada 955809 956498 956894 Mary John Peter • Array: it involves many copying operations (Remember: Data must be ordered!) 955809 Peter 956498 Mary 956894 John 955809 Peter 955992 Ada 956498 Mary 956894 John + 955992 Ada • For linked list, no copying operation is necessary Data can be anywhere in memory Only links are altered COMP103 - Linked Lists (Part A)

  18. Add an item, i.e. adding a Node • Item to be added should NOT be in the list, •  Search for this item will return a false value • pPre points to the item “less than” the one to be added • pCur points to an item “greater than” the one to be added Two cases: COMP103 - Linked Lists (Part A)

  19. Add an item to an Empty List pHead==NULL, means the list is empty pPre==NULL, means we are adding to an empty list, OR at the beginning of a list COMP103 - Linked Lists (Part A)

  20. Add an item at Beginning of the list Now, pHead!=NULL, so the list is not empty Also, pPre==NULL, so we are adding at the beginning of the list The code is identical! COMP103 - Linked Lists (Part A)

  21. Add an item in Middle of the list pPre != NULL AND pPre->link != NULL, so we are adding in the middle of the list COMP103 - Linked Lists (Part A)

  22. Add an item at the End of the list pPre != NULL AND pPre->link == NULL, so we are adding at the end of the list The code is identical to that of adding to the middle of the list COMP103 - Linked Lists (Part A)

  23. Add an item to the list Node *pNew; pNew = new Node; // allocate memory to new node if (!pNew) exit(0); // Memory overflow pNew->data = item; if (pPre == NULL) { pNew->link = pHead; // add before the first node pHead = pNew; // or to an empty list } else { pNew->link = pPre->link; // add in the middle or pPre->link = pNew; // at the end } COMP103 - Linked Lists (Part A)

  24. Delete an item (the first one) from the list • Item to be deleted should be in the list •  Search returns a Truevalue; • pCur points to the item; and • pPre points to the one before item. pPre == NULL, means we are deleting the first node COMP103 - Linked Lists (Part A)

  25. Delete - General Case pPre->link = pCur->link; delete (pCur); pPre->link = pCur->link delete (pCur); pPre!=NULL for all other cases COMP103 - Linked Lists (Part A)

  26. Delete a Node from a linked list // Delete a node from a linked list // Other Statements if (pPre == NULL) pHead = pCur->link; // delete first node else // delete other node pPre->link = pCur->link; delete (pCur); // return memory COMP103 - Linked Lists (Part A)

  27. Next Topic: Linked List Design • We have completed the basic operations in a linked list (Part A). • The last topic in the course (Part B of the notes) is to implement linked list as objects. To be covered in May after OO design and ADT. COMP103 - Linked Lists (Part A)

  28. Go to Part B COMP103 - Linked Lists (Part A)

More Related