MA 511: Computer Programming
Lecture 12: Insert & delete nodes
[Link]
Partha Sarathi Mandal
psm@[Link]
Dept. of Mathematics, IIT Guwahati
Semester 1, 2010-2011
How to insert node in a linked list?
1. At the beginning of the list node *insert(node *start){
node *search(node *, int);
2. At the middle of the list node *newnode, *terget;
3. At the end of the list int target_no;
: printf(“type target ”);
: scanf(“%d”, target_no);
if(start->data == target_no){ //add before target:at the beginning
newnode = (node *) malloc(sizeof(node));
scanf(“%d”, &(newnode->data));
node *insert(node *pt); newnode->next= start;
start = newnode;
}
main(){ else{ // finder return ptr of the preceding to the target node
node *start; target = search(start, target_no);
start = (node*)malloc(sizeof(node)); if (terget ==NULL) printf(“target no is not in list”);
else { // add in the middle of the list
create(start); newnode = (node *) malloc(sizeof(node));
print(start); scanf(“%d”, &(newnode->data));
start=insert(start); newnode->next = target->next;
terget->next= newnode;
print(start); }
} }
return(start);
}
MA511: Computer Programming
Partha S Mandal, IITG
Searching the target node
node *search(node *temp, int target_no){
//return a ptr to the node before the target node
while(1){
if(temp->next->data==target_no)
return(temp);
else if(temp->next->next==NULL)
return(NULL);
else temp=temp->next;
} temp
temp }
number next number next number next
102 112 122
number next
target_no :122
999 NULL
MA511: Computer Programming
Partha S Mandal, IITG
How to insert node in middle of the list?
newnode = (node *) malloc(sizeof(node));
scanf(“%d”, &(newnode->data));
newnode->next = target->next;
target ->next= newnode;
start target
number next number next
X
102 112 122 NULL
newnode
115
MA511: Computer Programming
Partha S Mandal, IITG
How to insert node to the beginning of the list?
newnode = (node *) malloc(sizeof(node));
scanf(“%d”, &(newnode->data));
newnode->next= start;
start = newnode;
number next number next number next
start X 102 112 122 NULL
newnode
next
80
MA511: Computer Programming
Partha S Mandal, IITG
How to delete node from a linked list?
node *delete(node *start){
node *search(node*, int) printf(“Place before target 999 at end ”);
scanf(“%d”, target_no);
node *target, *temp;
if(start->data == target_no){ // delete from beginning
int target_no; temp= start->next;
free(start);
start = temp;
}
else{ // finder return ptr of the preceding to the target node
target = search(start, target_no);
if (terget ==NULL) printf(“target no is not in list”);
else { // delete from the middle of the list
temp = target->next->next;
free(target->next);
target->next= temp;
}
}
return(start);
} MA511: Computer Programming
Partha S Mandal, IITG
Assignment
1. Write a C-Program for inserting a new node in a linked list maintaining
order with respect to the key value of nodes. Where key value of a new
node should enter in from the console.
2. Insert a node after a given target key.
3. Write a C-Program for deleting the node(s) from a given linked list which
is(are) matched with a given target key.
4. Form a given linked list (key values are +ve integers) filters out (deletes) all
nodes with odd integers and create a new linked list with the add integers.
[finally there will be two linked lists one with even integers and other
(new list) with odd integers]. Print the given list before deletion and print
final two lists separately.
For above all problems the given list you have to create first, taking input from
console
MA511: Computer Programming
Partha S Mandal, IITG