Linked List
Linked List
Topics Covered
• Introduction to Linked List
• Insertion in linked list
• Deletion from Linked List
• Circular Linked List
• Doubly Linked List
Introduction
• A linked list is a data structure which can change
during execution.
– Successive elements are connected by pointers.
– Last element points to NULL.
– It can grow or shrink in size during execution of a
program.
– It can be made just as long as required.
head
– It does not waste memory space.
A B C
• Keeping track of a linked list:
– Must know the pointer to the first element of the
list (called start, head, etc.).
Item to be
tmp X inserted
A B C
curr
X
Pseudo-code for insertion
typedef struct nd {
struct item data;
struct nd * next;
} node;
tmp=(node *) malloc(sizeof(node));
tmp->next=curr->next;
curr->next=tmp;
}
Illustration: Deletion
Item to be deleted
A B C
tmp
curr
A B C
Pseudo-code for deletion
typedef struct nd {
struct item data;
struct nd * next;
} node;
A B C
– Circular linked list
• The pointer from the last element in the list points back
to the first element.
head
A B C
– Doubly linked list
• Pointers exist between adjacent nodes in both
directions.
• The list can be traversed either forward or backward.
• Usually two pointers are maintained to keep track of
the list, head and tail.
head tail
A B C