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.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Linked Lists CS-212 Dick Steflik. Linked Lists A sequential collection of information Can be unordered; i.e. in no specific order Can be ordered; may.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Stacks, Queues, and Linked Lists
Linked Lists.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Linked List Variations
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists... An introduction to creating dynamic data structures.
Linked Lists
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
Exercise 11 Dynamic allocation, structures, linked lists.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Motivation * Doubly linked lists are useful for playing video and sound files with “rewind”
Chapter 3: Arrays, Linked Lists, and Recursion
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
ENEE150 – 0102 ANDREW GOFFIN Linked List/Project 3.
 Head pointer  Last node  Build a complete linked list  Node deletion  Node insertion  Helpful hints.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Programming Linked Lists. Collections Store collection of data  Online store - Items  University – Students  Library – books Until now we used arrays.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Programming Circular Linked List.
Linked Lists Chapter 6 Section 6.4 – 6.6
Problems with dynamic arrays
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Pointers and Linked Lists
Chapter 16-2 Linked Structures
Programming Linked Lists.
- Dynamic Allocation - Linked lists
Linked List.
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Exercise 12 Linked lists.
Presentation transcript:

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 students Keep track of their final grade Be able to add and remove students from the course And so on…

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 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 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 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 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 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 Linked lists - searching … Head ?? ! * A list is always maintained by its head (why is this enough?)

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

11 Linked lists - deletion … Head PreviousCurrent

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 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 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 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 Freeing students NULL headto_free while (to_free != NULL) { head = head->next; free(to_free); to_free = head; }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 Freeing students NULL head void free_list(Student *head) { if (head== NULL) return; free_list(head->next); free(head); }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

57 Next 3 Slides are Important!

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 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 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 Now you can go back to your business…

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

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

64 Adding a student - 2 … Head Next Head

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; } head next_std to_add 75 previous_std …

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; } head next_std to_add 75 previous_std …

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; } head next_std to_add 75 previous_std …

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; } head next_std to_add 75 previous_std …

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; } head next_std to_add 75 previous_std …

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; } head next_std to_add 75 previous_std …

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; } head next_std to_add 75 previous_std …

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; } head next_std to_add 75 previous_std …

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; } head next_std to_add 75 previous_std …

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; } head next_std to_add 75 previous_std …

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; head next_std to_add 75 previous_std …

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; head next_std to_add 75 previous_std …

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; head next_std to_add 75 previous_std …

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; head next_std to_add 75 previous_std …

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; } head next_std to_add 100 previous_std …

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; } head next_std to_add 100 previous_std …

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; head next_std to_add 100 previous_std …

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; head next_std to_add 100 previous_std …

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; head next_std to_add 100 previous_std …

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; } head next_std to_add 60 previous_std

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; } head next_std to_add 60 previous_std

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; } head next_std to_add 60 previous_std

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; } head next_std to_add 60 previous_std

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; } head next_std to_add 60 previous_std

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; } head next_std to_add 60 previous_std

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; } head next_std to_add 60 previous_std

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; } head next_std to_add 60 previous_std

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; } head next_std to_add 60 previous_std

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; } head next_std to_add 60 previous_std

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; } head next_std to_add 60 previous_std

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; } head next_std to_add 60 previous_std

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; } head next_std to_add 60 previous_std

97 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 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 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 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 Removing a student - reminder … Head PreviousCurrent

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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