You are on page 1of 11

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #28, October 13, 2011

Please switch off your mobile phones.

Announcements
Last date for course drop is 20th October. p
I will sign drop requests till 19th October.

Friday Lab Section please note


Lab on 14th suspended due to Gymkhana Holiday for Antaragni It will be conducted on 18th (Tuesday)
St d t who reported clash on 18th h Students h t d l h have b been assigned other days i d th d

Tuesday Lab Section please note


10th lab on Saturday, 22nd October, instead of 25th October.

Lec-28

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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.

Lab exam in the week of 14th to 18th November


Labs 11 and 12 will not be pre-announced to give you practice

End-sem exam is on 25th November, 8:00 AM


C i can be seen on 28th afternoon. Copies b f

We will try to record lectures and put them up on moodle.

Lec-28

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Best Programmers in Lab 8


Section E1 E3 E5 E7 E9 E11 E13 E15 Name Chandramauli Singh Unnat Jain Tanvi Soni Vaibhav Gupta Rahul Agrawal Sarthak Chandra none none Section E2 E4 E6 E8 E10 E12 E14 Jayant Shefali Garg Rishika Garg Sayan Das Prithvi Sharma Shreshth Gandhi Amandeep Chandra Name

Lec-28

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Linked lists
Insertion at the beginning Insertion at the end Deletion at the beginning

Lec-28

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Insertion at the beginning


struct node * insert_first (struct node * list, int number) // list contains the address of the first node in the list (or NULL) { struct node *temp; temp = (struct node *) malloc (sizeof (struct node)); // Allocate space temp->info = number; // Store the information temp->next = list; // points to the first node return temp; // this will become new head of list } head = insert_first (head, 20);
Lec-28 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 5

Lec-28

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Insertion at the end


struct node * insert_last (struct node * list, int number) // list contains the address of the first node in the list (or NULL) { struct node *t1, *t2; *t1 t1 = (struct node *) malloc (sizeof (struct node)); // Allocate space t1->info = number; // Store the information t1->next = NULL; // t1 will be the last node if (list == NULL) return t1; // special case of empty list t2 = list; // t2 is head while (t2->next ! NULL) (t2 next != // keep going till you come to the t2 = t2->next; // last node of the list t2->next = t1; // insert t1 as the last node return list; // head remains the same }
Lec-28 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 7

Recap: Deletion from the beginning


struct node * delete_first (struct node * list) ( ) // list points to the first node in the list { struct node *temp; temp = list; // temp points to first node if (temp == NULL) return temp; // special case of empty list list = list->next; free (temp); return list; }
Lec-28 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8

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

Deletion at the end


Use a pointer variable (say t2) to traverse the list
Initially t2 is set to Head repeat in a loop
if the next pointer of t2 is not NULL set t2 to its next pointer

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

Deletion at the end


Special cases:
If list is empty (initially, head was NULL)
Do nothing

If list has only one element (initially, next pointer of head was NULL)
Just delete Head Head is now NULL ead NU

Lec-28

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

10

Deletion at the end


struct node * delete_last (struct node * list) // list contains the address of the first node in the list (or NULL) { struct node *t1, *t2; if (list == NULL) return NULL; // Special case of empty list t2 = list; if (t2->next == NULL) // Special case of single item list { free (t2); return NULL; } while (t2->next != NULL) // Keep traversing the list till { t1 = t2; // t2 is pointing to the last node t2 = t2->next; t2 next; // t1 is always the second last node } t1->next = NULL; free (t2); // free the last node return list; // head remains the same }
Lec-28 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 11

Lec-28

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

14

Lec-28

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

15

Deleting a node in the middle


struct node * delete_mid (struct node * list, int key) { struct node * temp, *prev; // Will point to two successive nodes if (list == NULL) return list; // Special case: Empty list if (list->info == key) (list >info // Special case: Key in first node { temp = list->next; free (list); return temp; } prev = list; temp = list->next; // prev is previous to temp, always while (temp != NULL) // Traverse the list { if (temp->info == key) // If node found { prev->next = temp->next; // Remove node from list free (temp); break; // Free that memory } // Break the loop prev = temp; temp = temp->next; // Go to the next node } return list; // Return original head pointer } // Verify that this will work even if node is absent
Lec-28 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 16

Lec-28

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

17

Insertion in a sorted list


struct node * insert_mid (struct node * list, int number) { struct node *prev *temp *new; *prev, *temp, new = (struct node *) malloc (sizeof (struct node)); // Allocate space new->info = number; // Store the information if (list == NULL) // special case of empty list { new->next = list; return new; } if (list->info > number) // special case of new node first { new->next = list; return new; }
Lec-28 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 18

Insertion in a sorted list


prev = list; // prev is always previous to temp temp = list->next; while (temp != NULL) // keep going till you reach end { if (temp->info > number) // First node with larger info found break; // We will insert previous to this prev = temp; // Otherwise, move on to temp = temp->next; // check the next node } prev->next = new; // new comes after prev new->next = temp; // new comes before temp return list; // head remains the same } // Verify that this works if node is to be inserted at the end
Lec-28 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 19

10

Enjoy Antaragni

Lec-28

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

20

11

You might also like