esc101-lec29
TRANSCRIPT
-
7/31/2019 esc101-lec29
1/13
1
ESc101: Fundamentals of Com utin
2011-12-Monsoon Semester
Lecture #29, October 17, 2011
Please switch off your mobile phones.
Announcements
Last date for course drop is 20 th October. I will sign drop requests till 19 th October.
Friday Lab Sections: Lab 9 will be conducted on 18 th (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 23 rd.
Lab exam in the week of 14 th to 18 th November
Lec-29
La s 11 an 12 wi not e pre-announce to give you practice End-sem exam is on 25 th November, 8:00 AM
Copies can be seen on 28 th afternoon.
1Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
-
7/31/2019 esc101-lec29
2/13
2
Recap
Linked lists Deletion at the end Searching for a node Deletion in the middle Insertion in a sorted list
Lec-29 2Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
Recap: Insertion in a sorted list
struct node * insert_mid (struct node * list, int number){
, ,new = ( struct node *) malloc (sizeof (struct node)); // Allocate spacenew->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 KanpurESc101, 2011-12-Monsoon
3
-
7/31/2019 esc101-lec29
3/13
-
7/31/2019 esc101-lec29
4/13
4
Doubly Linked List
Lec-29 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
6
Complex Self-referential structures
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 *right;
};
Lec-29 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
7
-
7/31/2019 esc101-lec29
5/13
5
Binary Tree
Lec-29 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
8
Family Tree (Lab 10 assignment)
struct person p;struct familynode *father;struct familynode *mother;struct familynode *spouse;struct familynode *sibling;struct familynode *child;
};
Lec-29 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
9
-
7/31/2019 esc101-lec29
6/13
6
Family Tree
Lec-29 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
10
Lec-29 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
11
-
7/31/2019 esc101-lec29
7/13
7
Family Tree
name the parents name the spouse name ALL siblings name ALL children
Lec-29 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
12
Stack
- - Example: A stack of books
you can only put books on top, or take out a book from thetop
trying to take out a book from the middle or put a book inthe middle may cause all books to topple
Push: Insert at the beginning Pop: Delete from the beginning
Lec-29 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
13
-
7/31/2019 esc101-lec29
8/13
8
Stack
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
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 KanpurESc101, 2011-12-Monsoon
14
Queue
- - 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: nqueue: nsert at t e en Dequeue: Delete from the beginning
Lec-29 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
15
-
7/31/2019 esc101-lec29
9/13
9
Queue
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
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 KanpurESc101, 2011-12-Monsoon
16
Queue
Note that either end of the linked list could have been called
only thing that we need to do is to be consistent with thisdefinition 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 o e g ven n ec ure
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 KanpurESc101, 2011-12-Monsoon
17
-
7/31/2019 esc101-lec29
10/13
10
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 KanpurESc101, 2011-12-Monsoon
18
Doubly Linked List
struct nodeint key;struct node *prev;
struct node *next;};
s ruc no e ea = ;struct node *tail = NULL;
Lec-29 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
19
-
7/31/2019 esc101-lec29
11/13
11
Creating the first node
struct node *s
s = (struct node *) malloc (sizeof (struct node));s->key = ;s->prev = NULL;s->prev = NULL;
head = s;tail = s;
Lec-29 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 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 e e new no e e s
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 KanpurESc101, 2011-12-Monsoon
21
-
7/31/2019 esc101-lec29
12/13
-
7/31/2019 esc101-lec29
13/13
13
Removing a node
if n-> rev == NULL head = n->next;
elsen->prev->next = n->next;
if (n->next == NULL)tail = n->prev;
e sen->next->prev = n->prev;
free (n);
Lec-29 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
24
Any Questions?
Lec-29 25Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon