Lecture 3
Lecture 3
A B C
Item to be
tmp X inserted
A B C
curr
X
tmp=(node *) malloc(sizeof(node));
tmp->next=curr->next;
curr->next=tmp;
}
05/11/24 Programming and Data Structure 9
Position of insertion
Inserting at the beginning -> Time complexity: O(1)
A B C
tmp
curr
A B C
Insert
List
implementation
Delete
and the
related functions
Traverse
struct stud {
int roll;
char name[25];
int age;
struct stud *next;
};
name next
age
A B C
p = *head;
{
q = p;
p = p->next;
} The pointers
q and p
if (p == NULL) /* At the end */ always point
{ to consecutive
q->next = new; nodes.
new->next = NULL;
}
else if (p->roll == rno)
/* In the middle */
{
q->next = new;
new->next = p;
}
}
}
05/11/24 Programming and Data Structure 29
Deleting a node from the list
p = *head;
if (p->roll == rno)
/* Delete the first element */
{
*head = p->next;
free (p);
}
{
q = p;
p = p->next;
}