{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04-LinkedLists - 1 Linked List Basics Linked lists and...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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
Background image of page 2
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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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
Background image of page 4
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
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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);
Background image of page 6
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
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
8
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}