Linked List
Linked List
8/21/2023 1
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
8/21/2023 2
Contd.
• A completely different way to represent a list:
– Make each item in the list part of a structure.
– The structure also contains a pointer or link to the
structure containing the next item.
– This type of list is called a linked list.
Structure 1 Structure 2 Structure 3
8/21/2023 3
Contd.
• Such a structure can be represented as:
struct node
{
int item;
struct node *next;
node
};
item next
struct node_name
{
type member1;
type member2;
………
struct node_name *next;
};
8/21/2023 5
• Keeping track of a linked list:
– Must know the pointer to the first element of the
list (called start, head, etc.).
8/21/2023 6
Array versus Linked Lists
• Arrays are suitable for:
– Inserting/deleting an element at the end.
– Randomly accessing any element.
– Searching the list for a particular value.
• Linked lists are suitable for:
– Inserting an element.
– Deleting an element.
– Applications where sequential access is required.
– In situations where the number of elements cannot
be predicted beforehand.
8/21/2023 7
Basic Operations on a List
• Creating a list
• Traversing the list
• Inserting an item in the list
• Deleting an item from the list
• Concatenating two lists into one
8/21/2023 8
Create a list
1. #include <stdio.h>
2. #include <stdlib.h>
3. //Represent a node of singly linked list
4. struct node{
5. int data;
6. struct node *next;
7. };
8.
9. //Represent the head and tail of the singly linked list
10.struct node *head, *tail = NULL;
8/21/2023 9
1. //addNode() will add a new node to the list
2. void addNode(int data) {
3. //Create a new node
4. struct node *newNode = (struct node*)malloc(sizeof(struct node));
5. newNode->data = data;
6. newNode->next = NULL;
7.
8. //Checks if the list is empty
9. if(head == NULL) {
10. //If list is empty, both head and tail will point to new node
11. head = newNode;
12. tail = newNode;
13. }
14. else {
15. //newNode will be added after tail such that tail's next will point to newNode
16. tail->next = newNode;
17. //newNode will become new tail of the list
18. tail = newNode;
19. }
20. }
8/21/2023 10
1. //display() will display all the nodes present in the list
2. void display() {
3. //Node current will point to head
4. struct node *current = head;
5.
6. if(head == NULL) {
7. printf("List is empty\n");
8. return;
9. }
10. printf("Nodes of singly linked list: \n");
11. while(current != NULL) {
12. //Prints each node by incrementing pointer
13. printf("%d ", current->data);
14. current = current->next;
15. }
16. printf("\n");
17.}
8/21/2023 11
1. int main()
2. {
3. //Add nodes to the list
4. addNode(1);
5. addNode(2);
6. addNode(3);
7. addNode(4);
8.
9. //Displays the nodes present in the list
10. display();
11.
12. return 0;
13. }
8/21/2023 12
Output-
8/21/2023 13
Illustration: Insertion
A B C
Item to be
tmp X inserted
A B C
curr
X
8/21/2023 14
In essence ...
• For insertion:
– A record is created holding the new item.
– The next pointer of the new record is set to link it to
the item which is to follow it in the list.
– The next pointer of the item which is to precede it
must be modified to point to the new item.
• For deletion:
– The next pointer of the item immediately preceding
the one to be deleted is altered, and made to point
to the item following the deleted item.
8/21/2023 15
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;
}
8/21/2023 16
Inserting a Node at the Beginning
1 7 3 4 2 6 5 X
START
9 1 7 3 4 2 6 5 X
START
START, PTR
1 7 3 4 2 6 5 9 X
START
tmp
curr
A B C
8/21/2023 20
Pseudo-code for deletion
typedef struct nd {
struct item data;
struct nd * next;
} node;
8/21/2023 21
Types of Lists
• Depending on the way in which the links are
used to maintain adjacency, several different
types of linked lists are possible.
A B C
8/21/2023 22
– Circular linked list
• The pointer from the last element in the list points back
to the first element.
head
A B C
8/21/2023 23
– 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
head tail
the list, head and tail.
A B C
8/21/2023 24