Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists 2 3 9 head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.

Similar presentations


Presentation on theme: "Linked Lists 2 3 9 head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the."— Presentation transcript:

1 Linked Lists 2 3 9 head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the memory we use be contiguous. We can take a little bit of memory from here and a little bit of memory from there just so long as we can connect them together. This new data structure is called a linked list. Each element of a linked list contains not only the data we want to store, but also a pointer to the next element. The final element in the list has the NULL pointer, represented by a cross through the bottom half of the last node in this slide. To store a linked list, we just need to remember where the first element is. We can find the rest by following the pointers.

2 Nodes n next typedef struct node { int n; struct node* next; } node;
We’ll call each element of the linked list a node, which will contain the data we wish to store as well as a pointer to the next element in the list. Here, we use struct syntax to create our own data type to encapsulate a node. int n is the data we wish to store in the node and struct node* next defines a pointer to the next node in the linked list. Remember that a node won’t be typedef-ed until after all of these lines are executed. Thus, we have to write struct node instead of just node, both before the curly braces and inside the curly braces. On the very last line, we provide node to typedef as the name we want to use for this new data type for the rest of our program.

3 Search 2 3 9 head To search the list for 9, we'll need an additional pointer (depicted in yellow in the slide). This pointer will point at each node successively and allow us to check if n == 9. Note that this is linear search, and therefore the worst case runtime of this algorithm is O(n).

4 bool search(node* list, int n) { node* ptr = list; while (ptr != NULL)
if (ptr->n == n) return true; } ptr = ptr->next; return false; To begin with, ptr points to the first node of the list. As long as ptr != NULL, we will dereference it and check to see if ptr->n is the value we are searching for. If so, the function returns true. If we haven't yet found the value we're searching for, ptr must progress to the next node in the list. To point ptr to the next node in the list, we can’t simply write ptr++. The memory in a linked list is not contiguous, so the next node in the list is not necessarily right next to where ptr is pointing. Instead, ptr needs to point wherever ptr->next is pointing. If ptr has reached the end of the list without finding the value we are searching for, the function returns false.

5 Insertion 2 3 9 head Insertion and deletion of elements is potentially expensive with arrays. If we want to insert an element in an array anywhere except the end, we have to shift the rest of the array to the right -- with long arrays, this takes a lot of time. And if the array is full, we can't even add to the end, and have to copy it! In contrast, linked lists make element insertion and deletion very easy. A list is a good choice when the number of objects in your list is not known before you start to solve the problem, and the size of this list may grow and shrink during the task Let's insert an element into this linked list. To begin with, we need to request memory for a new node, and initialize it with data. The n field of our new node here has been set to 1, and its next field has been set to NULL. If we wish to maintain an ordered list, our new node belongs at the head of the list. 1

6 Insertion 2 3 9 head We need to be careful when updating pointers. If we first update head to point to our new node, then we orphan the remainder of the linked list. 1

7 Insertion 2 3 9 head Instead, we should point the new node's next field to the same node that head is pointing to. 1

8 Insertion 2 3 9 head 1 And then have head point to the new node.
Voila! Our new node has been inserted without ever losing hold of the rest of the linked list! 1

9 node* new = malloc(sizeof(node)); // check for NULL if (new == NULL)
void insert(int n) { // create new node node* new = malloc(sizeof(node)); // check for NULL if (new == NULL) exit(1); } // initialize new node new->n = n; new->next = NULL; // insert new node at head new->next = head; head = new; To insert at the head of a linked list: malloc a new node and initialize it with data set the new node's next field to point to the first node in the list set head to point to the new node

10 Doubly Linked Lists 2 3 9 head
A doubly-linked list is a linked list in which each element contains pointers to both the next and the previous elements. Doubly-linked lists are useful when you need to insert and remove objects from the center of the list, as well as from its ends, or when you need to traverse the list forward as well as backward.

11 DLL Nodes n next prev typedef struct node { int n; struct node* next;
struct node* prev; } node; n next prev Here's how to declare a node in a doubly-linked list. Note that this node has two pointers: one which points to the next node in the list, and one which points to the previous node in the list.


Download ppt "Linked Lists 2 3 9 head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the."

Similar presentations


Ads by Google