Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Linked Lists. 2 Implementing a student package We want to create (part of) a course- management program. We need to – Maintain a list of the participating.

Similar presentations


Presentation on theme: "1 Linked Lists. 2 Implementing a student package We want to create (part of) a course- management program. We need to – Maintain a list of the participating."— Presentation transcript:

1 1 Linked Lists

2 2 Implementing a student package We want to create (part of) a course- management program. We need to – Maintain a list of the participating students Keep track of their final grade Be able to add and remove students from the course And so on…

3 3 How will we store a student? One possible way is using a structure, like - typedef struct student_t { char id[ID_LENGTH+1]; char name[NAME_LENGTH+1]; int grade; } student;

4 4 Creating a student 1 student newStudent(char name[], char id[], int grade) { student s; s.name = name; // not legal s.id = id; // not legal s.grade = grade; return s; }

5 5 Creating a student 2 (a better idea) student newStudent(char name[], char id[], int grade) { student s; strcpy(s.name, name); strcpy(s.id, id); s.grade = grade; return s; }

6 6 Storing a list of students One way to go would be by using an array of student structures (or pointers to structures). There are some problems: 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

7 7 Linking students An alternative: use a linked list, by “self reference”. typedef struct student_t { char id[ID_LENGTH]; char name[NAME_LENGTH]; int grade; struct student_t *next; struct student_t *next; /* A pointer to the next item on the list */ } student;

8 8 Linked list of Students 21 Haim 96 next … Head 1. A list is always maintained by its head * why is this enough? 2. A list ends when the next field points to NULL. Student 32 Itzik 87 next 9 Dafna 100 next 25 Kobi 90 next Student

9 9 Linked lists - searching … Head ?? ! * A list is always maintained by its head (why is this enough?)

10 10 Linked lists - insertion … Head Insert new item: PreviousNext

11 11 Linked lists - deletion … Head PreviousCurrent

12 12 Creating a student Usually when using linked lists we don’t know in advance the number of elements Thus, we would like to be able to dynamically allocate new elements

13 13 Creating a student student *create_student(char name[], char id[], int grade) { student *std = (student *)malloc(sizeof(student)); if (std == NULL) { printf(“Memory allocation error!\n”); exit(1); } strcpy(std->name, name); strcpy(std->id, id); std->grade = grade; std->next = NULL; return std; }

14 14 Why not this, again? student *create_student(char name[], char ID[], int grade) { student std; strcpy(std.name, name); strcpy(std.id, id); std.grade = grade; std.next = NULL; return &std; }

15 15 Freeing students After we’re done, we need to free the memory we’ve allocated One implementation is as we saw in class void free_list(student *head) { student *to_free = head; while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

16 16 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

17 17 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

18 18 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

19 19 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

20 20 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

21 21 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

22 22 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

23 23 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

24 24 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

25 25 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

26 26 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

27 27 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

28 28 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

29 29 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

30 30 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

31 31 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

32 32 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

33 33 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

34 34 A Recursion Solution A perhaps simpler way to free a list is recursively. Any thoughts? void free_list(Student *head) { if (head== NULL) /* Finished freeing. Empty list */ return; free_list(head->next); /* Recursively free what’s ahead */ free(head); } Skip

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

57 57 Next 3 Slides are Important!

58 58 Linked List vs. Array Linked Lists: Fast insertion to a specified location Fast deletion Array: Fast search (on a sorted array) Direct access No/one dynamic allocations

59 59 How to solve a LL question 1. Sketch the solution 2. Write the code Example: add to the back of a list (on the blackboard)

60 60 LL Question Standard Draw and think Be careful with the pointers! Don’t “lose your head” Consider extreme cases (head/tail of list) Recursive Termination condition: an empty list Recursive call: (usually) on head->next

61 61 Now you can go back to your business…

62 62 Adding a student (Student_Package1.c) Adding a student to a sorted list We will implement this in a separate function

63 63 Adding a student - reminder … Head Insert new item: PreviousNext

64 64 Adding a student - 2 … Head Next Head

65 65 Adding a student – mid list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 807060 head next_std to_add 75 previous_std …

66 66 Adding a student – mid list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 807060 head next_std to_add 75 previous_std …

67 67 Adding a student – mid list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 807060 head next_std to_add 75 previous_std …

68 68 Adding a student – mid list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 807060 head next_std to_add 75 previous_std …

69 69 Adding a student – mid list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 807060 head next_std to_add 75 previous_std …

70 70 Adding a student – mid list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 807060 head next_std to_add 75 previous_std …

71 71 Adding a student – mid list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 807060 head next_std to_add 75 previous_std …

72 72 Adding a student – mid list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 807060 head next_std to_add 75 previous_std …

73 73 Adding a student – mid list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 807060 head next_std to_add 75 previous_std …

74 74 Adding a student – mid list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 807060 head next_std to_add 75 previous_std …

75 75 Adding a student – mid list if (previous_std == NULL) { to_add->next = head; return to_add; } previous_std->next = to_add; to_add->next = next_std; return head; 95 807060 head next_std to_add 75 previous_std …

76 76 Adding a student – mid list if (previous_std == NULL) { to_add->next = head; return to_add; } previous_std->next = to_add; to_add->next = next_std; return head; 95 807060 head next_std to_add 75 previous_std …

77 77 Adding a student – mid list if (previous_std == NULL) { to_add->next = head; return to_add; } previous_std->next = to_add; to_add->next = next_std; return head; 95 807060 head next_std to_add 75 previous_std …

78 78 Adding a student – mid list if (previous_std == NULL) { to_add->next = head; return to_add; } previous_std->next = to_add; to_add->next = next_std; return head; 95 807060 head next_std to_add 75 previous_std …

79 79 Adding a student – start list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 807060 head next_std to_add 100 previous_std …

80 80 Adding a student – start list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 807060 head next_std to_add 100 previous_std …

81 81 Adding a student – start list if (previous_std == NULL) { to_add->next = head; return to_add; } previous_std->next = to_add; to_add->next = next_std; return head; 95 807060 head next_std to_add 100 previous_std …

82 82 Adding a student – start list if (previous_std == NULL) { to_add->next = head; return to_add; } previous_std->next = to_add; to_add->next = next_std; return head; 95 807060 head next_std to_add 100 previous_std …

83 83 Adding a student – start list if (previous_std == NULL) { to_add->next = head; return to_add; } previous_std->next = to_add; to_add->next = next_std; return head; 95 807060 head next_std to_add 100 previous_std …

84 84 Adding a student – end list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 8070 head next_std to_add 60 previous_std

85 85 Adding a student – end list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 8070 head next_std to_add 60 previous_std

86 86 Adding a student – end list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 8070 head next_std to_add 60 previous_std

87 87 Adding a student – end list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 8070 head next_std to_add 60 previous_std

88 88 Adding a student – end list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 8070 head next_std to_add 60 previous_std

89 89 Adding a student – end list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 8070 head next_std to_add 60 previous_std

90 90 Adding a student – end list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 8070 head next_std to_add 60 previous_std

91 91 Adding a student – end list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 8070 head next_std to_add 60 previous_std

92 92 Adding a student – end list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 8070 head next_std to_add 60 previous_std

93 93 Adding a student – end list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 8070 head next_std to_add 60 previous_std

94 94 Adding a student – end list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 8070 head next_std to_add 60 previous_std

95 95 Adding a student – end list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 8070 head next_std to_add 60 previous_std

96 96 Adding a student – end list next_std = head; while (to_add->grade grade) { if (next_std->next == NULL) { next_std->next = to_add; return head; } previous_std = next_std; next_std = next_std->next; } 95 8070 head next_std to_add 60 previous_std

97 97 Exercise @ home Use Student_Package2.c and implement find_student, which receives a list and an id number and returns a pointer to a student whose id matches or NULL if no such student is found. Student *find_student(Student *head, char id[]) Hint: Use strcmp(s1, s2) which compares s1 and s2 and returns 0 if they are equal

98 98 Including files We can #include our own files in our program Say you wrote a file with some list functions called MyList.c You can include it in your program myProg.c by writing #include “MyList.c” This works given that both files reside in the same directory

99 99 Solution (Find_student.c) /* find a student whose id matches the given id number */ student *find_student(student *head, char id[]) { student *to_search = head; /* Start from head of list */ while (to_search != NULL) /* go over all the list */ { if (strcmp(to_search->id, id) == 0) /* same id */ return to_search; to_search = to_search->next; } /* If we're here, we didn't find */ return NULL; }

100 100 Removing a student We would like to be able to remove a student by his/her id The function that performs this is named remove_student See Student_Package3.c

101 101 Removing a student - reminder … Head PreviousCurrent

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

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

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

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

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

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

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

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

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

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

112 112 Removing a student – mid list if (last == NULL) head = head->next; else last->next = cur->next; /* Free the student */ free(cur); return head; 14525 748235362125773 head cur 53621 last … ID

113 113 Removing a student – mid list if (last == NULL) head = head->next; else last->next = cur->next; /* Free the student */ free(cur); return head; 14525 748235362125773 head cur 53621 last … ID

114 114 Removing a student – mid list if (last == NULL) head = head->next; else last->next = cur->next; /* Free the student */ free(cur); return head; 14525 748235362125773 head cur 53621 last … ID

115 115 Removing a student – mid list if (last == NULL) head = head->next; else last->next = cur->next; /* Free the student */ free(cur); return head; 14525 748235362125773 head cur 53621 last … ID

116 116 Removing a student – start list cur = head; while (strcmp(cur->id, id) != 0) { last = cur; cur = cur->next; if (cur==NULL) return head; } 14525 748235362125773 head cur 14525 last … ID

117 117 Removing a student – start list cur = head; while (strcmp(cur->id, id) != 0) { last = cur; cur = cur->next; if (cur==NULL) return head; } 14525 748235362125773 head cur 14525 last … ID

118 118 Removing a student – start list if (last == NULL) head = head->next; else last->next = cur->next; /* Free the student */ free(cur); return head; 14525 748235362125773 head cur 14525 last … ID

119 119 Removing a student – start list if (last == NULL) head = head->next; else last->next = cur->next; /* Free the student */ free(cur); return head; 14525 748235362125773 head cur 14525 last … ID

120 120 Removing a student – start list if (last == NULL) head = head->next; else last->next = cur->next; /* Free the student */ free(cur); return head; 14525 748235362125773 head cur 14525 last … ID

121 121 Removing a student – start list if (last == NULL) head = head->next; else last->next = cur->next; /* Free the student */ free(cur); return head; 14525 748235362125773 head cur 14525 last … ID

122 122 Exercise Use Student_Package3.c and add a change_grade function. The function should take as parameters 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, ID as the old one, with the new grade. Then remove the old student from the list and add the new one using the existing functions

123 123 solution /* Change a grade for a student */ student *change_grade(student *head, char id[],int new_grade) { /* First, check that the student exists */ student *student=find_student(head,id); if (student==NULL) return NULL; /* Create a new student node with new grade*/ new_student=create_student(student->id,student->name,new_grade); if (new_student==NULL) return NULL; /* Remove student with old grade from list */ head=remove_student(head,id); head=add_student(head,new_student); /* add student with new grade to list */ return head; }


Download ppt "1 Linked Lists. 2 Implementing a student package We want to create (part of) a course- management program. We need to – Maintain a list of the participating."

Similar presentations


Ads by Google