Download presentation
Presentation is loading. Please wait.
1
Hassan Khosravi / Geoffrey Tien
Linked Lists October 06, 2017 Hassan Khosravi / Geoffrey Tien
2
Hassan Khosravi / Geoffrey Tien
Unordered arrays Arrays are easy to code and to visualise, and offer rapid access to elements using the [] operator e.g. printf("salary is $%.2f\n", staff[85].salary); Consider an array with a capacity of š¯‘› Note that capacity may be different from the number of items stored in the array October 06, 2017 Hassan Khosravi / Geoffrey Tien
3
Hassan Khosravi / Geoffrey Tien
Unordered arrays What is the time complexity of inserting an item into an array without holes, when order does not matter? 18 9 26 41 12 75 37 60 Insert 15? What is the time complexity of removing an item into an array without holes, when order does not matter? 18 9 26 41 12 75 37 60 Remove 18? October 06, 2017 Hassan Khosravi / Geoffrey Tien
4
Hassan Khosravi / Geoffrey Tien
Ordered arrays What is the time complexity of inserting an item into an array without holes, when order matters? 9 12 18 26 37 41 60 75 Insert 15? What is the time complexity of removing an item into an array without holes, when order matters? 9 12 18 26 37 41 60 75 Remove 18? October 06, 2017 Hassan Khosravi / Geoffrey Tien
5
Hassan Khosravi / Geoffrey Tien
Linked list nodes A linked list is a dynamic data structure that consists of nodes linked together A node is a data structure that contains data the location (address) of the next node The data portion of a node can contain one or more items or structures typedef struct node { int data; struct node* next; } node; typedef struct player { int jersey_number; char* name; struct player* next; } player; October 06, 2017 Hassan Khosravi / Geoffrey Tien
6
Hassan Khosravi / Geoffrey Tien
Linked lists A linked list is a chain of nodes, where each node indicates where in (heap) memory the next item can be found stack heap front 32 76 11 54 32 76 11 54 October 06, 2017 Hassan Khosravi / Geoffrey Tien
7
Modifying linked lists
Inserting an item into a linked list 32 76 54 11 76 22 11 54 32 22 New nodes created using malloc() as needed Removing an item from a linked list 32 76 22 54 11 32 76 11 54 Removed nodes must be deallocated using free()! October 06, 2017 Hassan Khosravi / Geoffrey Tien
8
Linked list dis/advantages
Linked list is constructed from nodes in the heap, can be added as needed, and removed at runtime The size of the list does not need to be "guessed" ahead of time Disadvantages To access a particular node (starting from the front of the list), may need to traverse the list to reach ā€“ š¯‘‚ š¯‘› Linked list nodes have additional overhead to store pointers Harder to program, debug, and test October 06, 2017 Hassan Khosravi / Geoffrey Tien
9
Hassan Khosravi / Geoffrey Tien
Linked list traversal Traversal through a linked list can be done with a node pointer variable e.g. to access the 4th element in a list: // assuming we have node* front node* ptr = front; int i; for (i = 0; i < 4; i++) { ptr = ptr->next; } // exited loop, ptr references 4th node printf("%d", ptr->data); ptr front 76 22 11 54 32 October 06, 2017 Hassan Khosravi / Geoffrey Tien
10
Readings for this lesson
Thareja Chapter 6 (Linked lists) Algorithm and data structure visualisations Linked list: October 06, 2017 Hassan Khosravi / Geoffrey Tien
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.