Linked List
Linked List
Definition: A linked list is a data structure which is collection of zero or more nodes where each
node is connected to one or more nodes.
Data stores the value and link contains the next nodes address.
Representation of Linked List:
o Linked List can be defined as collection of objects called nodes that are randomly stored in
the memory.
o A node contains two fields.
o Data stored at that particular address and the pointer which contains the address of the next
node in the memory.
o The last node of the list contains pointer to the null.
Example
1
Singly Linked List
A singly linked list is a collection of zero or more nodes where each node has two or more fields but
only one link field which contains address of the next node.
Each node in the list can be accessed using the link field which contains address of the next node.
Example:
• The first node contains the address of the next node, i.e., 200.
• The second node contains the address of the last node, i.e., 300.
• Third node contains the NULL value in its address part as it does not point to any node.
• The pointer that holds the address of the initial node is known as a head pointer.
struct node
{
2
int data;
struct node *next;
}
Example
struct node
{
int data;
struct node *next;
struct node *prev;
}
3
Circular linked list
A circular singly linked list is a singly linked list where the link field of last node of the list contains
address of the first node.
Example:
The doubly circular linked list has the features of both the circular linked list and doubly linked list.
Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as follows...
4
Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the single linked list...
• Step 1 - Create a newNode with given value and newNode → next as NULL.
• Step 2 - Check whether list is Empty (head == NULL).
• Step 3 - If it is Empty then, set head = newNode.
• Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
• Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next is equal to NULL).
• Step 6 - Set temp → next = newNode.
Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as follows...
5
• Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate
the function.
• Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
• Step 4 - Check whether list is having only one node (temp → next == NULL)
• Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions)
• Step 6 - If it is FALSE then set head = temp → next, and delete temp.
Searching is performed in order to find the location of a particular element in the list. Searching any
element in the list needs traversing through the list and make the comparison of every element of
the list with the specified element. If the element is matched with any of the list element then the
location of the element is returned from the function.
Algorithm
write i+1
End of IF
o STEP 6: I = I + 1
o STEP 7: PTR = PTR → NEXT
[END OF LOOP]
o STEP 8: EXIT
Traversing is the most common operation that is performed in almost every scenario of singly linked
list. Traversing means visiting each node of the list once in order to perform some operation on that.
This will be done by using the following statements.
ptr = head;
7
while (ptr!=NULL)
{
ptr = ptr -> next;
}
Algorithm
[END OF LOOP]
o STEP 7: EXIT
/* Using dynamic variables and pointers Write a C program to construct a singly linked
list.The operations to be supported are
Inserting a node in the front of the list
Deleting the node based on Roll- No
displaying all the nodes in the list */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int rollno;
char name[20];
struct node *ptr;
};
typedef struct node Node;
Node *head,*temp,*temp1,*temp2;
main()
{
int ch;
head=NULL;
do
{
printf("\n 1:linsert\n 2:ldelete\n 3: display\n4:exit\n");
scanf("%d",&ch);
8
switch(ch)
{
case 1: linsert();
break;
case 2: ldelete();
break;
case 3: ldisplay();
break;
case 4: exit(0);
default : printf("wrong choice");
}
}while(ch!=4);
getch();
}
linsert()
{
temp=(Node *) malloc(sizeof(Node));
printf("Enter the roll number\n");
scanf("%d",&temp->rollno);
printf("Enter the name\n");
scanf("%s",temp->name);
if(head==NULL)
{
head=temp;
temp->ptr=NULL;
}
else
{
temp->ptr=head;
head=temp;
}
}
ldelete()
{
int r;
if(head==NULL)
printf("singly linked list is empty\n");
else
{
printf("Enter the rollno which is to be deleted\n");
scanf("%d",&r);
temp1=NULL;
temp=head;
while(temp!=NULL && temp->rollno!=r)
{
temp1=temp;
temp=temp->ptr;
}
9
if(temp==NULL)
printf("A node with rollno=%d is not present in list\n",r);
else if(head->rollno==r)
{
head=head->ptr;
free(temp);
}
else
{
temp2=temp->ptr;
temp1->ptr=temp2;
free(temp);
}
}
}
ldisplay()
{
if(head==NULL)
printf("Singly linked list is empty");
else
{
for(temp1=head;temp1->ptr!=NULL;temp1=temp1->ptr)
{
printf("Rollno=%d\tName=%s\n",temp1->rollno,temp1->name);
}
printf("Rollno=%d\t Name=%s\n",temp1->rollno,temp1->name);
}
}
10