Vous êtes sur la page 1sur 9

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #27, October 11, 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-27

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

Recap
Typedef yp Self referential structures Linked list

Lec-27

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

Recap: Self-referential Structures


A structure having a member of the type pointer to the same structure
struct student { int roll; char *name; struct student *next; };

Lec-27

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

Recap: Linked List


A linked list is a chain of self referential structures self-referential Each node contains some data, and a pointer to the next such node One creates as many such nodes as necessary to store available information The beginning of such a list is maintained as a pointer to the fi t d ( th first node (generally called head) ll ll d h d)

Lec-27

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

Recap: Linked Lists


Dynamic data structure
Size of the linked list grows or shrinks during the execution of a program

Advantages:
Asks for only as much memory allocation as needed during any stage of the execution Provides flexibility in inserting and deleting elements by just re-arranging the links

Disadvantage:
Accessing a particular element (searching) is not easy

Lec-27

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

Recap: Linked Lists


Three major operations on linked lists:
Insertion Delete Searching

Lec-27

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

Linked List
struct node { int info; struct node };

*next; // pointer to struct of same type

struct node *head = NULL; struct node *t t t d *temp;

// An empty list

Lec-27

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

Creating the first node


Allocate memory for the node node. Its next pointer is set to NULL Its info field is set to desired value Let head point to this node

Lec-27

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

Insertion at the beginning


Allocate memory for a new node Let the next pointer of the new node be set to head Let the info field point to the desired value Let head point to this new node

Notice that this and the previous cases are actually same p y

Lec-27

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

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-27 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 10

Insertion at the end


Allocate memory for a new node (say, pointed by t1) Let the next pointer of the new node be set to NULL Let the info field point to the desired value Use another 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 p

Finally t2 is pointing to the last node. Now set the next pointer of t2 to be t1.

Lec-27

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

11

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-27 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 12

Youtube videos on linked list


Linked List in 10 minutes Linked list: Introduction by Dave Feinberg

Lec-27

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

13

Deletion from the beginning


Initialize temp to be head temp head Set head to be its next pointer Now temp is pointing to the old head, which is to be deleted. Delete this by calling free( ). Special cases:
If head was null to begin with
do nothing

If there was only one element in the list


head will now become NULL
Lec-27 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 14

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-27 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 15

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

Any Questions?

Lec-27

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

16