Programming Linked Lists Linked Lists Dynamic n Efficient
- Slides: 68
Programming Linked Lists
Linked Lists Dynamic n Efficient use of memory n ¨ Allocate just as much as needed Easy insertion in front n Local deletion But… n Hard to get to any particular element n
Linked Lists n A list is a chain of nodes. typedef struct node_type { <data> struct node_type* next; } Node; head Data Next NULL
Linked Lists Operations n Insert ¨ front, back, middle Delete n Find n Size n
Course Management System n Maintain a list of students ¨ Keep their ID, name, grade etc. Allow for adding / removing a student n Find a student in the list n Produce Reports n ¨ For example: Average grade
Storing a Collection of Students Suggestion 1 Use an array of student structures n Problems: ¨ What size is the array? ¨ Remove students – either expensive or creates “holes” ¨ Insertion in a sorted array is expensive “holes”:
Linking Students Suggestion 2 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; } s. Student;
Creating a New Student s. Student* new_student(char* name, char* id, int grade) { s. Student *std =(s. Student*)malloc(sizeof(s. Student)); if (std != NULL) { strcpy(std->name, name); strcpy(std->id, id); std->grade = grade; std->next = NULL; } return std; }
Add in Front s. Student* add_front(s. Student *head, s. Student *std) { std->next = head; return std; } int main() { s. Student *std_list, *std; . . . std = new_student(. . . ); if (std != NULL ) std_list = add_front(std_list, std); . . . return 0; }
Sorted Add If the list is sorted (for example by grade), a student insertion should keep the list sorted n We will implement this in a separate function n
s. Student *add_student(s. Student *head, s. Student *to_add) { s. Student *curr_std, *prev_std = NULL; if (head == NULL) return to_add; handle empty list if (to_add->grade > head->grade) { to_add->next = head; return to_add; } handle beginning 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; the rest
Adding a student – beginning if (head == NULL) return to_add; if (to_add->grade > head->grade) { to_add->next = head; return to_add; } head to_add 95 100 80 70 …
Adding a student – mid / end 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; curr_std head 95 to_add 80 75 70 60
Adding a student – mid / end 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; curr_std head 95 to_add 80 75 70 60
Adding a student – mid / end 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; prev_std head 95 to_add 80 75 curr_std 70 60
Adding a student – mid / end 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; prev_std head 95 to_add 80 75 curr_std 70 60
Adding a student – mid / end 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; prev_std head 95 to_add 80 75 curr_std 70 60
Adding a student – mid / end 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; prev_std head 95 to_add 80 75 curr_std 70 60
Adding a student – mid / end 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; prev_std head 95 to_add 80 75 curr_std 70 60
Adding a student – mid / end 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; prev_std head 95 to_add 80 75 curr_std 70 60
Adding a student – mid / end 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; prev_std head 95 to_add 80 75 curr_std 70 60
Adding a student – mid / end 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; prev_std head 95 to_add 80 75 curr_std 70 60
Removing a student We would like to be able to remove a student by her/his ID. n The function that performs this is remove_student n
Removing a student - reminder Head Previous Current …
Removing a student – beginning if (head == NULL) return head; ID 14525 cur = head; if (strcmp(cur->id, id) == 0) { head = head->next; free(cur); return head; } head 14525 74823 cur 53621 25773 …
Removing a student – mid list while (cur != NULL && { prev = cur; cur = cur->next; } ID 53621 strcmp(cur->id, id) != 0) if (cur != NULL) { prev->next = cur->next; free(cur); } return head; cur head 14525 74823 53621 25773 …
Removing a student – mid list while (cur != NULL && { prev = cur; cur = cur->next; } ID 53621 strcmp(cur->id, id) != 0) if (cur != NULL) { prev->next = cur->next; free(cur); } return head; prev head 14525 cur 74823 53621 25773 …
Removing a student – mid list while (cur != NULL && { prev = cur; cur = cur->next; } ID 53621 strcmp(cur->id, id) != 0) if (cur != NULL) { prev->next = cur->next; free(cur); } return head; prev head 14525 74823 cur 53621 25773 …
Removing a student – mid list while (cur != NULL && { prev = cur; cur = cur->next; } ID 53621 strcmp(cur->id, id) != 0) if (cur != NULL) { prev->next = cur->next; free(cur); } return head; prev head 14525 74823 cur 53621 25773 …
Removing a student – mid list while (cur != NULL && { prev = cur; cur = cur->next; } ID 53621 strcmp(cur->id, id) != 0) if (cur != NULL) { prev->next = cur->next; free(cur); } return head; prev head 14525 74823 cur 25773 …
Deallocating all students void free_list(s. Student *head) { s. Student *temp = head; while (head != NULL) { temp = head; head = head->next; free(temp); } }
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head
Deallocating students while (head != NULL) { temp = head; head = head->next; free(temp); } temp head NULL
Deallocating students void free_list(s. Student *head) { if (head == NULL) return; free_list(head->next); free(head); }
Exercise n n Download find_student_ex. c from the tirgul home page Implement: s. Student* find_student(const s. Student *head, const char* id) n find_student searches for a student with a given id. It returns a pointer to the student if found, otherwise it returns NULL.
Reverse List Input: Output:
Recursive Reverse head
Recursive Reverse - Code Node * reverse_iter(Node * prev, Node* curr) { Node * tmp; if (curr == NULL) return prev; tmp = curr->next; // keep a link to the rest of the list curr->next = prev; // change direction of current node return reverse_iter(curr, tmp); // reverse the rest of the list } Node * reverse(Node * head) { return reverse_iter(NULL, head); }
' מועד ב , סמסטר א' תש"ע , 3 שאלה : יצירת מחסנית חדשה Stack * create. Stack() { Stack *s = (Stack*) malloc(sizeof(Stack)); if (s != NULL) { s->size = 0; s->head = NULL; } return s; }
' מועד ב , סמסטר א' תש"ע , 3 שאלה : יצירת איבר ברשימה Element * create. Element(int data) { Element *e = (Element*) malloc(sizeof(Element)); if (e != NULL) { e->data = data; e->next = NULL; } return e; מה השדות של ? Element המבנה }
' מועד ב , סמסטר א' תש"ע , 3 שאלה : ( הכנסת ערך למחסנית )בתחילת הרשימה המקושרת void push(Stack *s, int data) { Element *e = create. Element(data); if (e == NULL) { printf("Fatal error: unable to allocate memory!n"); exit(1); } e->next = s->head; s->head = e; s->size++; }
' מועד ב , סמסטר א' תש"ע , 3 שאלה typedef struct element { int data; struct element *next; } Element;
' מועד ב , סמסטר א' תש"ע , 3 שאלה int stack. Sum(Stack *s) { int sum = 0; while (!is. Empty(s)) sum += pop(s); return sum; }
' מועד ב , סמסטר א' תש"ע , 3 שאלה int main() { int val; Stack *s = create. Stack(); if (s == NULL) { printf("unable to create stack!"); return 1; } printf("Please enter inputn"); scanf("%d", &val); while (val >= 0) { push(s, val); scanf("%d", &val); } printf("The sum is %dn", stack. Sum(s)); free(s); return 0; }
Solution to Class Exercise /* find a student whose id matches the given id */ s. Student* find_student(s. 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; }
- Productively efficient vs allocatively efficient
- Allocative efficiency vs productive efficiency
- Productively efficient vs allocatively efficient
- Productively efficient vs allocatively efficient
- Productively efficient vs allocatively efficient
- Greedy vs dynamic programming
- Shuochao yao
- Advantage of linked list
- Difference between an array and a linked list
- Pengertian single linked list
- Dynamic linked list
- Transferered
- Gerrymandering dynamic programming
- A b a b c d e
- Backward approach in multistage graph
- Binomial coefficient excel
- Dynamic programming history
- Dynamic programming general method
- Characteristics of dynamic programming
- Dantzig
- Principle of optimality in algorithm
- Divide and conquer greedy algorithm
- Dynamic programming bottom up
- Dynamic programming
- Blackjack dynamic programming
- Dynamic programming equation
- Stage coach problem
- Dynamic programming bottom up
- Dynamic c programming
- Gap strategy in dynamic programming
- Parenthesization
- Dynamic programming excel
- Dynamic programming in excel
- Binomial coefficient using dynamic programming
- Tabulation dynamic programming
- Dynamic programming paradigm
- Dynamic programming algorithm
- Features of dynamic programming
- Rna secondary structure dynamic programming
- Dynamic programming
- Rna secondary structure dynamic programming
- Dynamic problem
- Assignment problem dynamic programming
- Fibonacci sequence dynamic programming
- Programingz
- Longest common subset
- Dynamic programming
- Dynamic programming slides
- Manhattan tourist problem dynamic programming
- Matrix chain multiplication example
- Disadvantages of array
- 4d3d41669541f1bf19acde21e19e43d23ebbd23b
- Dynamic programming vs divide and conquer
- Dynamic programming
- Dynamic programming
- Recursive thinking definition
- Advantages of dynamic programming
- Dynamic programming paradigm
- Egg acknowledgement
- Robot coin collection dynamic programming
- Programming
- Dynamic programming
- Dynamic programming recursion example
- Dynamic programming recursion example
- Perbedaan linear programming dan integer programming
- Perbedaan linear programming dan integer programming
- Windows 10 system programming, part 1
- Integer programming vs linear programming
- Python plot list of lists