Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming Linked Lists. Collections Store collection of data  Online store - Items  University – Students  Library – books Until now we used arrays."— Presentation transcript:

1 Programming Linked Lists

2 Collections Store collection of data  Online store - Items  University – Students  Library – books Until now we used arrays

3 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

4 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

5 Graphic Description Array Linked List  Each link (node) contains data and a pointer to the next link

6 Linked Lists A list is a chain of nodes. typedef struct node_type { struct node_type* next; } Node; NULL Data Next Data Next Data Next head

7 Linked Lists Operation Insert  front, back, middle Remove Find Size

8 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

9 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

10 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

11 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

12 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 NULL iter

13 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; }

14 Length (recursive) Count the number of nodes in a list int length(Node *head) { if (head == NULL) return 0; return length(head->next) + 1; }

15 Using Length int 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; }

16 Course Management System Maintain a list of students  Keep their ID, name, grade etc. Allow for adding / removing a student Find a student in the list Produce Reports  Average grade

17 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

18 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;

19 Exercise Download find_student_ex.c from the tirgul home pagefind_student_ex.c 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.

20 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; }

21 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…

22 Creating a New Student Student* 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; }

23 Add in Front Student* 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; }

24 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

25 Adding a student - begining … Head

26 Adding a student – mid/end … Head Insert new item: PreviousNext

27 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 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

28 Adding a student – beginning if (head == NULL) return to_add; if (to_add->grade > head->grade) { to_add->next = head; return to_add; } 958070 … to_add 100 head

29 Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; 95 807060 head to_add 75 curr_std

30 Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; 95 807060 head to_add 75 curr_std

31 Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; 95 807060 head to_add 75 curr_stdprev_std

32 Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; 95 807060 head to_add 75 curr_stdprev_std

33 Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; 95 807060 head to_add 75 curr_stdprev_std

34 Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; 95 807060 head to_add 75 curr_stdprev_std

35 Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; 95 807060 head to_add 75 curr_stdprev_std

36 Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; 95 807060 head to_add 75 curr_stdprev_std

37 Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; 95 807060 head to_add 75 curr_stdprev_std

38 Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; 95 807060 head to_add 75 curr_stdprev_std

39 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

40 … Head PreviousCurrent Removing a student - reminder

41 Removing a student – beginning if (head == NULL) return head; cur = head; if (strcmp(cur->id, id) == 0) { head = head->next; free(cur); return head; } 748235362125773 head cur 14525 … ID 14525

42 Removing a student – mid list 14525 748235362125773 head 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;

43 Removing a student – mid list 14525 748235362125773 head 53621 … ID prevcur while (cur != NULL && strcmp(cur->id, id) != 0) { prev = cur; cur = cur->next; } if (cur != NULL) { prev->next = cur->next; free(cur); } return head;

44 Removing a student – mid list 14525 748235362125773 head 53621 … ID prevcur while (cur != NULL && strcmp(cur->id, id) != 0) { prev = cur; cur = cur->next; } if (cur != NULL) { prev->next = cur->next; free(cur); } return head;

45 Removing a student – mid list 14525 748235362125773 head 53621 … ID prevcur while (cur != NULL && strcmp(cur->id, id) != 0) { prev = cur; cur = cur->next; } if (cur != NULL) { prev->next = cur->next; free(cur); } return head;

46 Removing a student – mid list while (cur != NULL && strcmp(cur->id, id) != 0) { prev = cur; cur = cur->next; } if (cur != NULL) { prev->next = cur->next; free(cur); } return head; 14525 7482325773 head 53621 … ID prevcur

47 Deallocating all students void free_list(Student *head) { Student *temp = head; while (head != NULL) { temp = head; head = head->next; free(temp); }

48 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

49 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

50 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

51 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

52 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

53 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

54 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

55 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

56 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

57 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

58 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

59 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

60 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

61 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

62 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

63 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

64 Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

65 Deallocating students NULL headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

66 Deallocating students void free_list(Student *head) { if (head == NULL) return; free_list(head->next); free(head); }

67 Exercise Use change_grade_ex.c and implement the change_grade function.change_grade_ex.c 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

68 solution Student* 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); }


Download ppt "Programming Linked Lists. Collections Store collection of data  Online store - Items  University – Students  Library – books Until now we used arrays."

Similar presentations


Ads by Google