Sie sind auf Seite 1von 13

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #29, October 17, 2011

Please switch off your mobile phones.

Announcements
Last date for course drop is 20th October. I will sign drop requests till 19th October. Friday Lab Sections: Lab 9 will be conducted on 18th (Tuesday) Monday Lab Sections: Lab 10 will not be today, but next week Tuesday Lab Sections: Lab 10 will not be tomorrow may be on 23rd. Lab exam in the week of 14th to 18th November Labs 11 and 12 will not be pre-announced to give you practice b d ill b d i i th November, 8:00 AM End-sem exam is on 25 Copies can be seen on 28th afternoon.

Lec-29

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

Recap
Linked lists
Deletion at the end Searching for a node Deletion in the middle Insertion in a sorted list

Lec-29

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

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

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

Complex Self-referential structures


Doubly linked list
a node contains pointers to both previous and next nodes in the list struct student { int roll; char name[30]; struct student *prev; struct student *next; };
Lec-29 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 5

Doubly Linked List

Lec-29

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

Complex Self-referential structures


Binary tree
A hierarchical structure of nodes where each node can have pointers to two nodes each node being called a child struct node { int key; struct node struct node };

*left; *right;

Lec-29

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

Binary Tree

Lec-29

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

Family Tree (Lab 10 assignment)


struct familynode { struct person struct familynode struct familynode struct familynode struct familynode y struct familynode };

p; *father; *mother; *spouse; *sibling; g *child;

Lec-29

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

Family Tree

Lec-29

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

10

Lec-29

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

11

Family Tree
How would you
name the parents name the spouse name ALL siblings name ALL children

Lec-29

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

12

Stack
Last-in First-out Last in First out Example: A stack of books
you can only put books on top, or take out a book from the top trying to take out a book from the middle or put a book in the middle may cause all books to topple

Operations:
Push: Insert at the beginning Pop: Delete from the beginning

Lec-29

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

13

Stack
Some of you have implemented it using an array in a lab Can be easily implemented using regular (single pointer) linked list. Push
same as inserting a node at the beginning of the linked list
code given in lecture # 27

P Pop
same as deleting a node from the beginning of the linked list
code given in lecture #27

Lec-29

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

14

Queue
First-in First-out First in First out Example: queue at the railway reservation counter
you can only enter the queue at the end only the person at the head of the queue can leave the queue (hopefully, after getting the reservation)

Operations:
E Enqueue: Insert at the end I h d Dequeue: Delete from the beginning

Lec-29

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

15

Queue
Some of you have implemented it using an array in a lab Can be easily implemented using regular (single pointer) linked list. Enqueue
same as inserting a node at the end of the linked list
code given in lecture # 27

D Dequeue
same as deleting a node from the beginning of the linked list
code given in lecture #27

Lec-29

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

16

Queue
Note that either end of the linked list could have been called beginning/end
only thing that we need to do is to be consistent with this definition in dequeue and enqueue

So the alternate implementation of queue operations will be:


Enqueue
same as inserting node at the beginning of the linked list C d given in lecture #27 Code i i l t

Dequeue
same as deleting a node at the end of the linked list Code given in lecture #28
Lec-29 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 17

Queue
Note that in either implementation of queue
one operation is done in a single step the other operation requires traversal of the entire list

Can we do both operations in single step?


Yes, using doubly linked lists

Lec-29

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

18

Doubly Linked List


struct node { int key; struct node *prev; struct node *next; }; struct node *head = NULL; t t d *h d NULL struct node *tail = NULL;

Lec-29

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

19

10

Creating the first node


struct node *s; s; s = (struct node *) malloc (sizeof (struct node)); s->key = <KEY>; s->prev = NULL; s->prev = NULL; head = s; tail = s;

Lec-29

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

20

Inserting after a given node


Will not change head May change tail, if the given node (n) is the last node L t the new node be s Let th d b
s->prev = n; s->next = n->next; if (n->next == NULL) tail = s; else n->next->prev = s; n->next = s;
Lec-29 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 21

11

Inserting before a given node


Will not change tail May change head, if the given node (n) is the first node L t the new node be s Let th d b
s->prev = n->prev; s->next = n; if (n->prev == NULL) head = s; else n->prev->next = s; n->prev = s;
Lec-29 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 22

Removing a node
Let n be the pointer to the node that needs to be removed It will change head, if it is the first node. It will change tail, if it is the last node It will change both head and tail (to NULL), if it is the only node.

Lec-29

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

23

12

Removing a node
if (n->prev == NULL) (n prev head = n->next; else n->prev->next = n->next; if (n->next == NULL) tail = n->prev; else l n->next->prev = n->prev; free (n);

Lec-29

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

24

Any Questions?

Lec-29

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

25

13

Das könnte Ihnen auch gefallen