You are on page 1of 41

Data Structures: Linked Lists

Department of CSE

1
Linked List Basics
• Linked lists and arrays are similar since they both store collections of data.
• The array's features all follow from its strategy of allocating the memory for all its
elements in one block of memory.
• Linked lists use an entirely different strategy: linked lists allocate memory for
each element separately and only when necessary.

• Linked lists are appropriate when the number of data elements to be


represented in the data structure at once is unpredictable.
• Linked lists are dynamic, so the length of a list can increase or decrease as
necessary.
• Each node does not necessarily follow the previous one physically in the
memory.
• Linked lists can be maintained in sorted order by inserting or deleting an element
2
at the proper point in the list.
Linked Lists

Single Linked Double Linked Circular Linked


Lists Lists Lists

• Traversing
• Search
• Insertion
• Deletion

3
Linked Lists

A B C \0

Head

• A linked list is a series of connected nodes


• Each node contains at least
• A piece of data (any type)
• Pointer to the next node in the list
node
• Head: pointer to the first node
A
• The last node points to NULL
data pointer

4
Singly Linked Lists

head

next next next next

20 5 15 17 \0

First node Last node

head
Data next
Create a Linked List
• Empty Linked list is a single pointer having the value of NULL.
head = NULL; head

• Let’s assume that the node is given by the following type declaration:
struct Node{
int data; head
struct Node *next; Data next
};

To start with, we have to create a node (the first node), and make head point to it.

head = (struct Node*)malloc(sizeof(struct Node));


6
#include<stdio.h>
#include<stdlib.h> Creating the linked List

struct Node {
int data;
struct Node *next;
};
struct Node *create_list(); struct Node *create_list()
{
void display(struct Node *); int k, n;
struct Node *p, *Head;
void main()
{ printf("\n How many elements to enter?");
struct Node *head; scanf("%d", &n);
head=create_list(); for (k=0; k<n; k++)
display(head); {
if (k == 0) {
} Head = (struct Node*)malloc(sizeof(struct Node));
p = Head;
void display (struct Node *head) }
{ else {
p->next = (struct Node*)malloc(sizeof(struct Node));
struct Node *p; p = p->next;
for(p = head; p!= NULL; p = p->next) }
printf("\n Enter an %dth element",k);
{ scanf("%d",&p->data);
printf ("\nNode data %d", p->data); }
} p->next = NULL;
printf ("\n"); return(Head);
} 7
}
Can we store any data in linked list?

struct node{
int roll;
char name[25];
int age;
struct node *next;
};
• To start with, we have to create a node (the first node), and make head
point to it.
head = (struct node *) malloc(sizeof(struct node));
head
roll
name next
age 8
struct student { void main()
int roll; Creating the linked List {
char name[25]; struct student *head;
int age; head=create_list();
struct student *next; display(head);
}; }
struct student *create_list()
{ void display (struct student *head)
int k, n;
struct student *p, *head; {
printf ("\n How many elements to enter?"); int count = 1;
scanf ("%d", &n); struct student *p;
for (k=0; k<n; k++)
{ p = head;
if (k == 0) {
head = (struct student*) malloc(sizeof(struct student)); while (p != NULL)
p = head;
} {
else { printf ("\nNode %d: %d %s %d", count,
p->next = (struct student*) malloc(sizeof(struct
student)); p->roll, p->name, p->age);
p = p->next;
} count++;
p = p->next;
scanf ("%d%s%d",&p->roll,p->name,&p->age);
} }
p->next = NULL; printf ("\n");
return (head); }
}
9
Basic Linked List Operations

•List Traversal (Display function)


•Searching an element in a linked list
•Insert a node (At begin, At end , In between)
•Delete a node (At begin, At end , In between)

10
Traversing a linked list
void display (struct Node *head)
{
struct Node *p;
int count=0;
for(p = head; p!= NULL; p = p->next)
{
printf ("\nNode data %d", p->data);
count=count+1;
}
printf ("\n");
}
head

20 5 15 17 \0
Searching an element in a linked list
void search(struct Node *head)
{ void main()
struct Node *p; {
int key, flag=0; struct Node *head;
head=create_list();
printf(“Enter an element to search:”); display(head);
scanf(“%d”,&key); search(head);
for(p = head; p!= NULL; p = p->next) }
{
if ((p->data)==key)
{
flag=1; break;
head
}
}
if(flag==1)
printf(“found”); 20 5 15 17 \0
else
printf(“not found”);
}
Insert a node At beginning
Steps:
head 48 17 142 \0
• Create a Node
• Set the node data Values
• Follow the steps and we get
• Connect the pointers
Step 1 Step 2

Step 3

head 93 48 17 142 \0

13
Insert a node At beginning of Linked List
struct Node *insertB(struct Node *head)
{
/* 1. create a new node */
struct Node* newnode;
newnode = (struct Node*) malloc(sizeof(struct Node));

/* 2. put in the data */


printf(“Enter an element to insert at beginning:”);
scanf(“%d”,&newnode->data);
void main()
/* 3. Make next of new node as head */ {
newnode->next = head; struct Node *head;
head=create_list();
/* 4. move the head to point to the new node */ head=insertB(head);
head=newnode; display(head);
return(head);
} }

14
Insert a node At end of Linked List
Steps:
head 48 17 142 \0
• Create a Node
• Set the node data Values
• Connect the pointers • Follow the steps and we get
Step 1 Step 2

93 \0

Step 3

93 \0

15
Insert a node At end of Linked List
struct Node * insertE(struct Node *head)
{
/* 1. create new node */
struct Node* newnode;
newnode = (struct Node*) malloc(sizeof(struct Node));
/* 2. put in the data */
printf(“Enter an element to insert:”);
scanf(“%d”,&newnode->data);
/* 3. This new node is going to be the last node, so make next of it as NULL*/
newnode->next = NULL;
/* 4. If the Linked List is empty, then make the new node as head */
if (head== NULL) void main()
{ head = newnode; {
return(head); } struct Node *head;
/* 5. Else traverse till the last node */ head=create_list();
struct Node *p=head; head=insertE(head);
while (p->next != NULL) display(head);
{ p = p->next; }
/* 6. Change the next of last node */ }
p->next = newnode;
return(head); 16
}
Insert a node At middle of Linked List
Steps:
head 48 17 142
• Create a Node
• Set the node data Values Step 1 Step 2
• Break pointer connection
• Re-connect the pointers
Step 3
142 \0

Step 4

142 \0

17
Insert a node At middle of Linked List
Steps:
• Create a Node 17 143
… …
• Set the node data Values
• Break pointer connection P
93
• Re-connect the pointers new_node

newnode = (struct Node*) malloc(sizeof(struct Node));


scanf(“%d”,&newnode->data);
newnode->next = p->next;
p->next = newnode;

18
Insert a node At middle of Linked List
struct Node * insertM(struct Node *head)
{
int newdata, loc=3,c=0;
/* 1. create a new node */
struct Node* newnode;
newnode = (struct Node*) malloc(sizeof(struct Node));
/* 2. put in the data */
printf(“Enter an element to insert:”);
scanf(“%d”,&newnode->data);
/* 3. If the Linked List is empty, then make the new node as head */
struct Node *p=head;
while(p!= NULL || c!=loc) void main()
{ p = p->next; {
c=c+1; } struct Node *head;
/* 4. move the next of prev_node as new_node */ head=create_list();
newnode->next=p->next; head=insertM(head);
p->next=newnode; display(head);
return(head);
}
} 19
Delete a node At beginning of Linked List
Steps head
1. Break the pointer connection
2. Re-connect the nodes 6 4 17 42 \0
3. Delete the node
head

6 4 17 42 \0

head
struct Node *DeleteB(struct Node *head)
{ 4 17 42 \0

If (head==NULL)
return(head); void main()
{ struct Node *head;
head=head->next; head=create_list();
head=DeleteB(head);
return(head); display(head);
20
} }
Delete a node At end of Linked List
Steps head
1. Break the pointer connection
2. Set previous node pointer to NULL 6 4 17 42 \0
3. Delete the node
head
struct Node *DeleteE(struct Node *head)
{
6 4 17 42 \0
If (head==NULL)
return(head); head
struct Node *curr=head, *prev;
while(curr->next!=NULL)
{ 6 4 17 \0
prev=curr;
curr=curr->next
} void main()
prev->next=NULL; { struct Node *head;
Free(curr); head=create_list();
return(head); head=DeleteE(head);
} display(head);
21
}
Delete a node At middle of Linked List
head

6 4 17 42 \0

head

6 4 17 42 \0

head

6 4 17 \0
Steps
1. Set previous Node pointer to next
node
2. Break Node pointer connection
3. Delete the node 22
Delete a node At middle of Linked List
struct Node *DeleteM(struct Node *head)
{
int key;
printf(“Enter an element to delete:”);
scanf(“%d”,&key);
// Store head node
struct Node* curr = head, *prev;
// If head node itself holds the key to be deleted
if (curr!= NULL && curr->data == key)
{
head= curr->next; // Changed head
free(curr); // free old head
return(head);
}
while (curr != NULL && curr->data != key)
{
prev = curr;
curr = curr->next;
} void main()
// If key was not present in linked list { struct Node *head;
if (curr == NULL) return(head); head=create_list();
// Unlink the node from linked list head=DeleteM(head);
prev->next = curr->next; display(head);
free(curr); // Free memory } 23
}
Doubly Linked Lists
head

\0 20 40 50 55 \0

Advantages: node
• Convenient to traverse the list backwards.
• Simplifies insertion and deletion because you no longer have A
to refer to the previous node.
Lpointer data Rpointer
Disadvantage:
• Increase in space requirements.

24
Create a Doubly Linked List
• Empty Linked list is a single pointer having the value of NULL.
head = NULL; head

• Let’s assume that the node is given by the following type declaration:
struct Node {
int data; head
struct Node *Rnext; Lnext Data Rnext
struct Node *Lnext;
};

To start with, we have to create a node (the first node), and make head point to it.

head = (struct Node *)malloc(sizeof(struct Node));


25
#include<stdio.h>
#include<stdlib.h> Creating the Doubly linked List
struct Node {
int data;
struct Node *Lnext; struct Node *create_list()
struct Node *Rnext; {
}; int k, n;
struct Node *curr, *post, *Head;
struct Node *create_list();
printf("\n How many elements to enter?");
void display(struct Node *); scanf("%d", &n);
void main() for (k=0; k<n; k++)
{ {
if (k == 0) {
struct Node *head; Head = (struct Node*)malloc(sizeof(struct Node));
head=create_list(); Head->Lnext=NULL;
curr = Head;
display(head); }
else {
} post = (struct Node*)malloc(sizeof(struct Node));
curr->Rnext=post;
void display (struct Node *head) post->Lnext=curr;
curr=post;
{ }
struct Node *p; scanf("%d",&curr->data);
for(p = head; p!= NULL; p = p->Rnext) }
{ curr->Rnext = NULL;
printf ("\nNode data %d", p->data); return(Head);
}
}
printf ("\n");
} 26
Basic Doubly Linked List Operations

•List Traversal (Display function) (Same as Single linked list)


•Searching an element in a linked list(Same as Single linked list)
•Insert a node (At begin, At end , In between)
•Delete a node (At begin, At end , In between)

27
Insert a node At middle of doubly Linked List
head current

newNode = (struct Node*)malloc(sizeof(struct Node)); newNode


newNode->Rnext = current->Rnext;
(current->Rnext)->Lnext=newnode
newNode->Lnext = current;
current->Rnext = newNode;
current = newNode;

28
Delete a node At middle of doubly Linked List
head current

oldNode = current;
(oldNode->Lnext)->Rnext = oldNode->Rnext;
(oldNode->Rnext)->Lnext = oldNode->Lnext;
free(oldNode);

29
Circular Linked Lists
• A Circular Linked List is a special type of single Linked List
• It supports traversing from the end of the list to the beginning by making the last node point
back to the head of the list
• A start pointer is often used instead of a Head pointer

10 20 40 55 70

head

10

head

30
#include<stdio.h>
#include<stdlib.h> Creating Circular linked List

struct Node {
int data;
struct Node *next;
};
struct Node *create_list(); struct Node *create_list()
void display(struct Node *); {
void main() int k, n;
struct Node *p, *Head;
{
struct Node *head; printf("\n How many elements to enter?");
scanf("%d", &n);
head=create_list();
display(head); for (k=0; k<n; k++)
} {
if (k == 0) {
void display (struct Node *head) Head = (struct Node*)malloc(sizeof(struct Node));
p = Head;
{ }
struct Node *p; else {
printf ("\nNode data %d", head->data); p->next = (struct Node*)malloc(sizeof(struct Node));
p = p->next;
for(p = head->next; p!= head; p = p->next) }
{
scanf("%d",&p->data);
printf ("\nNode data %d", p->data); }
}
printf ("\n"); p->next = Head;
return(Head); 31
} }
Insertion in Circular Linked List

• Insert to head of a Circular Linked List


New->next = head;
for(p=head->next;p->next!=head;p=p->next)
{}
p->next = New; // if p->next==head
head=new;

10 20 40 55 70

New head p p p
Insertion in Circular Linked List

• Insert to middle of a Circular Linked List between Pre and Cur


New->next = Cur;
Prev->next = New;

10 20 55 70

40
head Prev Cur

New
33
Deletion in Circular Linked List

• Delete the head node from a Circular Linked List


for(p=head->next;p->next!=head;p=p->next)
{}
p->next = head->next; // if p->next==head
head=head->next;

10 20 40 55 70

head p
34
Deletion in Circular Linked List

• Delete a middle node Cur from a Circular Linked List

Prev->next = Cur->next;
free(Cur);

10 20 40 55 70

head Prev Cur

35
Arrays Vs Linked Lists

36
COMPLEXITY OF VARIOUS OPERATIONS IN ARRAYS AND LINKED LIST

37
Doubly Circular Linked List
• It has properties of both Circular Linked List and Doubly Linked List
• We can traverse in both directions
• The last node of the list connects to the start node as Rnext and the start node has
the last node in the Lnext pointer.

Head->Lnext
head

20 40 50 55

(Head->Lnext)->Rnext

38
Insertion in Doubly Circular Linked List
Insertion at Head
1. Create New Node using malloc()
2. Update the Lnext and Rnext pointers in New Node
3. Update the Lnext of first node (Head)
4. Rnext of last node (Head->Lnext)
5. Update New Node as the new Head

head head

10 20 40 50 55

39
Insertion in Doubly Circular Linked List
#include<stdio.h> Insertion at Head
#include<stdlib.h> void insertBegin(struct Node** start, int value)
struct Node {
{ // Pointer points to last Node
int data; struct Node *last = (*start)-> Lnext;
struct Node *Rnext; struct Node* new_node = (struct
struct Node *Lnext; Node*)malloc(sizeof(struct Node));
}; new_node->data = value; // Inserting the data
/* Driver program to test above functions*/ // setting up previous and next of new node
int main() new_node->Rnext = *start;
{ new_node-> Lnext = last;
/* Start with the empty list */ // Update next and previous pointers of start and last.
struct Node* start = NULL; last->Rnext = (*start)-> Lnext = new_node;
insertBegin(&start, 4); // Update start pointer
*start = new_node;
return 0; }

40
Practice Problems on Linked List
1. Write a C program to create a single linked list with 5 nodes. (5 integers are taken from user
input).
2. Write a C program to display a single linked list.
3. Write a C program to search an element a single linked list.
4. Write a C program to insert a node at beginning of a single linked list.
5. Write a C program to insert a node at end of a single linked list.
6. Write a C program to insert a node at middle of a single linked list.
7. Write a C program to create a doubly linked list with 5 nodes and display it.
8. Write a C program to create a circular linked list with 5 nodes and display it.
9. Write a C program to implement the stack using linked list.
10. Write a C program to implement the queue using linked list.
11. Write a C program to create a circular doubly linked list with 5 nodes and display it. Provide
the user with options for (i) Insertion – at head, end and middle, and (i) Deletion – at head,
end and middle.
41

You might also like