Programming Linked Lists. Collections Store collection of data Online store - Items University ...

68
Programming Linked Lists

description

Array Recap Allocated as one block in memory Convenient to declare  int arr[100]; Easy access to a single element  arr[i] Size is fixed  Wasting memory because of “large enough” allocation Adding / Deleting elements  need to shift large parts of the array

Transcript of Programming Linked Lists. Collections Store collection of data Online store - Items University ...

Page 1: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Programming

Linked Lists

Page 2: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Collections

Store collection of data Online store - Items University – Students Library – books

Until now we used arrays

Page 3: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Array Recap Allocated as one block in memory Convenient to declare

int arr[100]; Easy access to a single element

arr[i] Size is fixed

Wasting memory because of “large enough” allocation Adding / Deleting elements

need to shift large parts of the array

Page 4: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Linked Lists

Dynamic Efficient use of memory

Allocate just as much as needed Easy insertion in front Local deletion Hard to get to any particular element

Page 5: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Graphic Description

Array

Linked List

Each link (node) contains data and a pointer to the next link

Page 6: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Linked Lists

A list is a chain of nodes.typedef struct node_type{ <data> struct node_type* next;} Node;

NULL

Data

Next

Data

Next

Data

Next

head

Page 7: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Linked Lists Operation

Insert front, back, middle

Remove Find Size

Page 8: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Insertion (front)

Data

Next

head

1 .create the new node

2. have it point to the first element

3 .change the head of the list

Data

Next

Data

Next

NULL

Data

Next

Page 9: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Insertion (back)

Data

Next

head

1 .create the new node

2. locate the last element in the list

3 .have the last element point to the new one

Data

Next

Data

Next

NULL

last

NULL

Data

Next

Page 10: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Insertion (middle)

Data

Next

head

NULL

1. create the new node

2. locate the insertion point

3. new_item->next = curr->next

4. curr->next = new_item

Data

Next

Data

Next

curr

new_item

NULL

Data

Next

Page 11: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Delete Node

Data

Next

head

1 .find node to remove

2. have the previous node point to the one after the node to be removed

3 .remove the node

Data

Next

Data

Next

Data

Next

NULL

remove

Page 12: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Iterating

Data

Next

head

Data

Next

Data

Next

Data

Next

1 .start at the beginning

2. advance your iterator one node at a time iter = iter->next

3 .unti the end is reached

iter iter iter iter

NULL

iter

Page 13: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Length Count the number of nodes in a list

int length(Node *head){ int count = 0; while (head != NULL) { count++; head = head->next; } return count;}

Page 14: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Length (recursive)

Count the number of nodes in a list

int length(Node *head){ if (head == NULL) return 0;

return length(head->next) + 1;}

Page 15: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Using Lengthint main(void){ Node *head = build_list(); int length = length(head);

printf("The length of the list is: %d\n", length);

free_list(head);

return 0;}

Page 16: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Course Management System

Maintain a list of studentsKeep their ID, name, grade etc.

Allow for adding / removing a student Find a student in the list Produce Reports

Average grade

Page 17: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Storing a Collection of Students Use an array of student structures There are problems with this –

we must allocate a big-enough array before accepting students (how do we know what’s big enough?)

How shall we remove students from the list without creating “holes”?

How can we maintain the list sorted by grade? Insertion and deletion may be problematic

Page 18: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Linking Students Use a linked list Define a student node

typedef struct student { char id[ID_LENGTH]; char name[NAME_LENGTH]; int grade;

/* A pointer to the next node in the list */ struct student *next;

} Student;

Page 19: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Exercise Download find_student_ex.c from the tirgul home

page Implement Student* find_student(const Student *head, const char* id)

find_student searches for a student with a given id. It returns a pointer to the student if found, otherwise it returns NULL.

Page 20: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Solution/* find a student whose id matches the given id */Student* find_student(Student *head, char *id){ while (head != NULL) /* go over all the list */ { if (strcmp(head->id, id) == 0) /* same id */ return head; head = head->next; }

/* If we're here, we didn't find it */ return NULL;}

Page 21: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding students

Usually when using linked lists we don’t know how many elements will be in the list

Therefore we would like to be able to dynamically allocate new elements when the need arises

A possible implementation follows…

Page 22: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Creating a New StudentStudent* new_student(char* name, char* id, int grade){ Student *std =(Student*)malloc(sizeof(Student));

if (std != NULL) { strcpy(std->name, name); strcpy(std->id, id); std->grade = grade; std->next = NULL; } return std;}

Page 23: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Add in FrontStudent* add_front(Student *head, Student *std){ std->next = head; return std;}

int main(void){ Student *std_list, *std; ... std = new_student(...); std_list = add_front(std_list, std); ... return 0;}

Page 24: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Sorted Add

Adding a student to a list can be done in such a way that the list remains sorted by grade

We will implement this in a separate function

Page 25: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding a student - begining

Head

Page 26: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding a student – mid/end

Head

Insert new item:

Previous

Next

Page 27: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Student *add_student(Student *head, Student *to_add){ Student *curr_std, *prev_std = NULL;

if (head == NULL) return to_add;

if (to_add->grade > head->grade) { to_add->next = head; return to_add; }

curr_std = head; while (curr_std != NULL && to_add->grade < curr_std->grade) { prev_std = curr_std; curr_std = curr_std->next; }

prev_std->next = to_add; to_add->next = curr_std;

return head;}

handle empty list

handle beginning

the rest

Page 28: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding a student – beginning

if (head == NULL) return to_add;

if (to_add->grade > head->grade){ to_add->next = head; return to_add;}

95 80 70 …

to_add

100

head

Page 29: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding a student – mid / endcurr_std = head;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return head;

95 80 70 60head

to_add

75

curr_std

Page 30: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding a student – mid / endcurr_std = head;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return head;

95 80 70 60head

to_add

75

curr_std

Page 31: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding a student – mid / endcurr_std = head;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return head;

95 80 70 60head

to_add

75

curr_std

prev_std

Page 32: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding a student – mid / endcurr_std = head;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return head;

95 80 70 60head

to_add

75

curr_std

prev_std

Page 33: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding a student – mid / endcurr_std = head;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return head;

95 80 70 60head

to_add

75

curr_std

prev_std

Page 34: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding a student – mid / endcurr_std = head;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return head;

95 80 70 60head

to_add

75

curr_std

prev_std

Page 35: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding a student – mid / endcurr_std = head;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return head;

95 80 70 60head

to_add

75

curr_std

prev_std

Page 36: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding a student – mid / endcurr_std = head;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return head;

95 80 70 60head

to_add

75

curr_std

prev_std

Page 37: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding a student – mid / endcurr_std = head;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return head;

95 80 70 60head

to_add

75

curr_std

prev_std

Page 38: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Adding a student – mid / endcurr_std = head;while (curr_std != NULL && to_add->grade < curr_std->grade){ prev_std = curr_std; curr_std = curr_std->next;}

prev_std->next = to_add;to_add->next = curr_std;return head;

95 80 70 60head

to_add

75

curr_std

prev_std

Page 39: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Removing a student

We would like to be able to remove a student by her/his ID.

The function that performs this is remove_student

Page 40: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

HeadPrevious

Current

Removing a student - reminder

Page 41: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Removing a student – beginningif (head == NULL)

return head;

cur = head;

if (strcmp(cur->id, id) == 0){ head = head->next; free(cur); return head;}

74823 53621 25773head

cur

14525

ID

14525

Page 42: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Removing a student – mid list

14525 74823 53621 25773head

53621

ID

cur

while (cur != NULL && strcmp(cur->id, id) != 0){ prev = cur; cur = cur->next;}

if (cur != NULL){ prev->next = cur->next; free(cur);}

return head;

Page 43: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Removing a student – mid list

14525 74823 53621 25773head

53621

ID

prev cur

while (cur != NULL && strcmp(cur->id, id) != 0){ prev = cur; cur = cur->next;}

if (cur != NULL){ prev->next = cur->next; free(cur);}

return head;

Page 44: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Removing a student – mid list

14525 74823 53621 25773head

53621

ID

prev cur

while (cur != NULL && strcmp(cur->id, id) != 0){ prev = cur; cur = cur->next;}

if (cur != NULL){ prev->next = cur->next; free(cur);}

return head;

Page 45: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Removing a student – mid list

14525 74823 53621 25773head

53621

ID

prev cur

while (cur != NULL && strcmp(cur->id, id) != 0){ prev = cur; cur = cur->next;}

if (cur != NULL){ prev->next = cur->next; free(cur);}

return head;

Page 46: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Removing a student – mid listwhile (cur != NULL && strcmp(cur->id, id) != 0){ prev = cur; cur = cur->next;}

if (cur != NULL){ prev->next = cur->next; free(cur);}

return head;

14525 74823 25773head

53621

ID

prev cur

Page 47: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating all studentsvoid free_list(Student *head){ Student *temp = head;

while (head != NULL) { temp = head; head = head->next; free(temp); }}

Page 48: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 49: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 50: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 51: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 52: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 53: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 54: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 55: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 56: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 57: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 58: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 59: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 60: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 61: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 62: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 63: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 64: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 65: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

NULL

head

tempwhile (head != NULL) { temp = head; head = head->next; free(temp);}

Page 66: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Deallocating students

void free_list(Student *head){ if (head == NULL) return;

free_list(head->next); free(head);}

Page 67: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

Exercise Use change_grade_ex.c and implement the

change_grade function. The function takes as input the head of the list,

the ID whose grade we’d like to change, and the new grade

Hint – Create a new student with the same name and ID as the old one, but with the new grade. Remove the old student from the list and add the new one using the existing functions

Page 68: Programming Linked Lists. Collections Store collection of data  Online store - Items  University  Students  Library  books Until now we used arrays.

solutionStudent* find_student(Student* head, char* id){ while (head != NULL && strcmp(head->id, id) != 0) { head = head->next; }

return head;}

Student* change_grade(Student *head, char* id, int new_grade){ Student *std = find_student(head, id); std = new_student(std->name, id, new_grade);

head = remove_student(head, id); return add_student(head, std);}