This preview shows pages 1–9. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 1 Linked List Basics ringbutton3 Linked lists and arrays are similar since they both store collections of data. ringbutton3 The array's features all follow from its strategy of allocating the memory for all its elements in one block of memory. ringbutton3 Linked lists use an entirely different strategy: linked lists allocate memory for each element separately and only when necessary. 2 2 Disadvantages of Arrays 1. The size of the array is fixed . circle5 dynamically resizing the array from size S to 2S: we need 3S units of available memory. circle5 allocating "large enough ” arrays has problems: circle5 (a) most of the time there are just 20% or 30% elements in the array and 70% of the space in the array really is wasted. circle5 (b) If the program ever needs to process more than the declared size, the code breaks. 2. Inserting (and deleting) elements into the middle of the array is potentially expensive because existing elements need to be shifted over to make room 3 Linked lists ringbutton3 Linked lists are appropriate when the number of data elements to be represented in the data structure at once is unpredictable. ringbutton3 Linked lists are dynamic, so the length of a list can increase or decrease as necessary. ringbutton3 Each node does not necessarily follow the previous one physically in the memory. ringbutton3 Linked lists can be maintained in sorted order by inserting or deleting an element at the proper point in the list. 4 3 Singly Linked Lists 5 First node Last node a b c d head next next next next Empty List ringbutton3 Empty Linked list is a single pointer having the value of NULL. head = NULL; 6 head 4 Basic Ideas ringbutton3 Let’s assume that the node is given by the following type declaration: struct Node { Object element; Node *next; }; 7 Basic Linked List Operations ringbutton3 List Traversal ringbutton3 Searching a node ringbutton3 Insert a node ringbutton3 Delete a node 8 5 Traversing a linked list Node *pWalker; int count = 0; cout <<“List contains:\n”; for (pWalker=pHead; pWalker!=NULL; pWalker = pWalker>next) { count ++; cout << pWalker>element << endl; } 9 Searching a node in a linked list pCur = pHead; // Search until target is found or we reach // the end of list while (pCur != NULL && pCur>element !=target) { pCur = pCur>next; } //Determine if target is found if (pCur) found = 1; else found = 0; 10 6 Insertion in a linked list 11 a b … … x current tmp Insertion in a linked list 12 a b … … x current tmp tmp = new Node; tmp>element = x; tmp>next = current>next; current>next = tmp; Or simply (if Node has a constructor initializing its members): current>next = new Node(x,current>next); 7 Deletion from a linked list 13 a x b … … current Deletion from a linked list Node *deletedNode = current>next; current>next = current>next>next; delete deletedNode; 14 a x b … … current 8 Special Cases ringbutton3 Inserting before the first node (or to an empty list):...
View
Full
Document
This note was uploaded on 11/12/2010 for the course CENG 223 taught by Professor Yok during the Spring '10 term at Middle East Technical University.
 Spring '10
 Yok

Click to edit the document details