Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.

Similar presentations


Presentation on theme: "COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management."— Presentation transcript:

1 COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management

2 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

3 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.

4 COMP103 - Linked Lists (Part A)4 Linked Lists: Basic Idea 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: 204575 85 Data Link

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

6 COMP103 - Linked Lists (Part A)6 Linked List Structure 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 node =NULL

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

8 COMP103 - Linked Lists (Part A)8 Nodes in C++ Simple: data Define the node type: struct NODE { int data; NODE *link; }; 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; } 10

9 COMP103 - Linked Lists (Part A)9 Nodes in C++ Complex node: Define the node type: struct DATA {char key; int ID; int tel; }; struct NODE { DATA data; NODE *link; }; 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; } tel. no. ID key 5678 123 a

10 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

11 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 Add an item to the list Delete an item from the list

12 COMP103 - Linked Lists (Part A)12 Linked List Traversal 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 pWalker When pWalker becomes NULL, it means that the end of the list is reached

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

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

15 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 pCur points to the node which should be after target pPre points to the node which should be before target 3 cases: (i) pPre==NULL, (ii) pCur==NULL, (iii) pPre!=NULL AND pCur!=NULL

16 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;

17 COMP103 - Linked Lists (Part A)17 Add an item: Array vs. Linked List 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 + 955809Peter956498Mary956894John955992Ada For linked list, no copying operation is necessary Data can be anywhere in memory Only links are altered

18 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:

19 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

20 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!

21 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

22 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

23 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 }

24 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 True value; pCur points to the item; and pPre points to the one before item. pPre == NULL, means we are deleting the first node

25 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

26 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

27 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.

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


Download ppt "COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management."

Similar presentations


Ads by Google