Professional Documents
Culture Documents
Announcements
Last date for course drop is 20th October. p
I will sign drop requests till 19th October.
Lec-28
Announcements
Change in weights of evaluation components
quizzes will be 25 % (instead of 20%) q ( ) end-sem exam will be 30% (instead of 35%) labs (10%), lab exam (15%), mid-sem (20%) remain the same.
Lec-28
Lec-28
Recap
Linked lists
Insertion at the beginning Insertion at the end Deletion at the beginning
Lec-28
Lec-28
// move the head of the list to the next node // temp can be freed return memory to system // the new list is the new head of the list
While finding the last node, have another temporary variable (say t1), which is pointing to the previous node
That is, the next pointer of t1 is same as t2
Finally t2 is pointing to the last node, and t1 is pointing to the second last node Now set the next pointer of t1 to be NULL Delete the node pointed to by t2 by calling free ( )
Lec-28 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 9
If list has only one element (initially, next pointer of head was NULL)
Just delete Head Head is now NULL ead NU
Lec-28
10
Lec-28
12
Searching a node
Set initially temp to be same as Head temp Now repeat in a loop
if data in the node pointed by temp is same as what is being searched
return (temp)
otherwise, set temp to the next pointer of temp if temp is NULL, we have reached the end of the list without NULL finding the node
return NULL
Lec-28
13
Searching a node
struct node * search_list (struct node * list, int number) { struct node * temp; temp = li list; while (temp != NULL) // Traverse the list { if (temp->info == number) // If node found, break break; temp = temp->next; // Go to the next node } return (temp); // Return pointer to found node }
Lec-28
14
Lec-28
15
Lec-28
17
10
Enjoy Antaragni
Lec-28
20
11