Presentation is loading. Please wait.

Presentation is loading. Please wait.

Edvin von Otter & Todd Barker

Similar presentations


Presentation on theme: "Edvin von Otter & Todd Barker"— Presentation transcript:

1 Edvin von Otter & Todd Barker
Linked lists Edvin von Otter & Todd Barker

2 Arrays Incredibly common Disadvantages
By now you are all familiar with arrays. They are one of the most common data structures used to store collections of elements and are very useful for many applications. But an array has its disadvantages: An array has to be pre-allocated, making it a rather blunt tool in the sense that it’s size cannot be altered after it has been determined. There are means of “dynamic allocation” but they require some pretty heavy programming and a lot of effort. Therefore, they will not be covered in the presentation. To exemplify this “allocation-problem”, say we are iterating through some kind of loop and want to store data along the way without knowing how many entries we’re going to need. Because there is no way for us to pre-allocate an array of the appropriate size, we face two paths. The ugly one: Making sure the array is large enough by simply allocating more space than can possibly be necessary. In immediate consequence, either space will be wasted or the code will break if the size of the array is exceeded. Our studies have shown that this is a lot more conventional than you’d expect. The nice one: Using a linked list instead! The vital difference between a linked list and an ordinary array is that the linked list is a lot less constricted; entries can be changed, deleted, added, moved, all in real time. Linked lists aren’t the solution to everything, but they happen to have their strengths right where the arrays have their weaknesses. Linked lists are incredibly useful tools for all of us, so listen closely.

3 Introducing the linked list structure
struct node { int data; struct node* next; }; Thankfully, the key to the linked list structure is a concept that all of you are already familiar with; pointers. I believe that during the last lecture, Yasemin talked about how we can use struct to create a customized data-type of our own, ringing a bell? Good! By applying this method… …we construct a variable type called node, containing two sorts of information: Data (an integer in this case) and a pointer called next. To fully understand the beauty of this, lets take time to recall how pointers are declared; the type of data that is being pointed at, followed by it’s name. So in this case, the pointer “next “is designed to point at another node. A sequence of nodes joined together by their pointers is what we call a linked list. The principle can be illustrated like this:

4 Linked list structure This is a map of a function that has created a list with three nodes. As we see, the head pointer points to the first node, that points to the second node, that points to the third node that points to NULL. This means that we only need to know the memory adress of the first pointer to be able to access the list. We can think of a linked list as a train, with a locomotive – the head pointer – and the cars – the nodes.

5 Applications Queues Stacks Trees Hash tables
So when can a linked list come in handy? I want to go as far as saying that as soon as you find yourself to working with any kind of memory that needs to be dynamic – meaning that the sizes and locations of the memory need to be variable – a linked list is your best friend. It could be a queue, imagine it yourselves: The elements need to know their internal order, they might need to moved, removed or even added – a linked list lets all of these operations be carried out easily - Edvin is going to show us in just a minute. Other obvious applications are stacks or trees. Linked lists can also be used in hash tables, I’ll save that for the next group though.

6 Some useful functions Push() Pop() InsertNth() DeleteNth()
DeleteList() Length() Loads of other functions… These are some useful functions that are also commonly used with Arrays. We will show you all of these, but will go into depth on three of them: Push, Pop and DeleteList.

7 Push() This is a memory map of the Push() function. We call the function with the callers head-pointer and the data we want to add. Then we push the current first node by assigning the head to the new node and link with the new second node

8 Push() Allocate memory Link with next Link head
void Push(struct node** headRef, int data){ struct node* newNode = malloc(sizeof(struct node)); newNode->data = data; newNode->next = *headRef; *headRef = newNode; } Push(&head,data); First Allocate the node in the heap and set its data to whatever you want 2. Set the .next pointer of the new node to point to the current first node of the list. This is actually just a pointer assignment — remember: "assigning one pointer to another makes them point to the same thing.” 3. Change the head pointer to point to the new node, so it is now the first node in the list. Now, there is one part here that is very important. Note that we use the double asterix in the parameter list! We need Push() to be able to change some of the caller's memory — namely the head variable. The traditional method to allow a function to change its caller's memory is to pass a pointer to the caller's memory instead of a copy. So in C, to change an int in the caller, pass a int* instead. To change a struct fraction, pass a struct fraction* intead. To change an X, pass an X*. So in this case, the value we want to change is struct node*, so we pass a struct node** instead. The two stars (**) are a little scary, but really it's just a straight application of the rule. It just happens that the value we want to change already has one star (*), so the parameter to change it has two (**). Or put another way: the type of the head pointer is "pointer to a struct node." In order to change that pointer, we need to pass a pointer to it, which will be a "pointer to a pointer to a struct node".

9 Pop()

10 Pop() Create and assign “refrence nodes”
If current doesn’t point to NULL: Get data Link with next pointer Free the memory space Assign next as head Return value Otherwise, return 0 int Pop(struct node** headRef){ struct node* current = *headRef; struct node* next; if(current != NULL){ int value = current->data; next = current->next; free(current); *headRef = next; return value; } return 0;

11 DeleteList()

12 DeleteList() Derefrence to get real head
void deleteList(struct node** headRef){ struct node* current = *headRef; struct node* next; while (current != NULL){ next = current->next; free(current); current = next; } *headRef = NULL; Derefrence to get real head Go through the list and delete all the values with free() function Assign the ”real” headpointer the value NULL

13 DEMO


Download ppt "Edvin von Otter & Todd Barker"

Similar presentations


Ads by Google