Linked List
Linked List
LINKED LIST
Linked List
Linked List can be defined as collection of objects called nodes that are randomly
stored in the memory.
A node contains two fields i.e. data stored at that particular address and the pointer
which contains the address of the next node in the memory.
The last node of the list contains pointer to the null .
1 Insertion at beginning It involves inserting any element at the front of the list. We just
need to a few link adjustments to make the new node as the
head of the list.
2 Insertion at end of the list It involves insertion at the last of the linked list. The new node
can be inserted as the only node in the list or it can be inserted
as the last one. Different logics are implemented in each
scenario.
3 Insertion after specified nod It involves insertion after the specified node of the linked list.
e We need to skip the desired number of nodes in order to reach
the node after which the new node will be inserted. .
Insertion at the Beginning Insertion At Last {
struct node *ptr; struct node *ptr,*temp; temp = temp -> next;
int item; int item; }
ptr = (struct node *) ptr = (struct node*)malloc(sizeof(struct temp->next = ptr;
malloc(sizeof(struct node *)); node)); ptr->next = NULL;
if(ptr == NULL) if(ptr == NULL) printf("\nNode inserted");
{ {
printf("\nOVERFLOW"); printf("\nOVERFLOW"); }
} } }
else else }
{ {
printf("\nEnter value\n"); printf("\nEnter value?\n");
scanf("%d",&item); scanf("%d",&item);
ptr->data = item; ptr->data = item;
ptr->next = head; if(head == NULL)
head = ptr; {
printf("\nNode inserted"); ptr -> next = NULL;
} head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
Random Insert at Position
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
Begin Delete
struct node *ptr; Delete Last node
if(head == NULL) struct node *ptr,*ptr1;
{ if(head == NULL)
printf("\nList is empty\n"); {
} printf("\nlist is empty");
else }
ptr = head; {
free(ptr); free(head);
printf("\nNode deleted from the begining ...\n"); printf("\nOnly node of the list deleted ...\n");
} }
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
Random Delete node at Position
if(ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
else
Find node in Linked List
{
struct node *ptr;
flag=1;
int item,i=0,flag;
}
ptr = head;
i++;
if(ptr == NULL)
ptr = ptr -> next;
{
}
printf("\nEmpty List\n");
if(flag==1)
}
{
else
printf("Item not found\n");
{
}
printf("\nEnter item which you want to search?\
n"); }
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
Display Linked List
ptr = (struct node *)malloc(sizeof(struct node)); ptr = (struct node *) malloc(sizeof(struct node)); printf("\nnode inserted\n");
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
Deletion at Begining
Deletion at last
struct node *ptr; Deletion at Position
if(head == NULL)
{ struct node *ptr, *temp;
printf("\n UNDERFLOW");
int val;
}
printf("\n Enter the data after which the node is to be deleted : ");
else if(head->next == NULL)
scanf("%d", &val);
{
ptr = head;
head = NULL;
while(ptr -> data != val)
free(head);
{
printf("\nnode deleted\n");
}
ptr = ptr -> next;
else }
{ if(ptr -> next == NULL)
ptr = head; {
while(ptr->next != NULL) printf("\nCan't delete\n");
{ }
ptr = ptr -> next; else
} {
ptr -> prev -> next = NULL; temp = ptr -> next;
free(ptr); ptr -> next = temp -> next;
printf("\nnode deleted\n"); temp -> next -> prev = ptr;
}
free(temp);
printf("\nnode deleted\n");
}
CIRCULAR LINKED LIST
In a circular Singly linked list, the last node of the list contains a pointer to the first node
of the list. We can have circular singly linked list as well as circular doubly linked list.
We traverse a circular singly linked list until we reach the same node where we started.
The circular singly liked list has no beginning and no ending. There is no null value
present in the next part of any of the nodes.
The following image shows a circular singly linked list.
Begin Insert Last Insert
struct node *ptr,*temp; struct node *ptr,*temp;
int item; int item;
ptr = (struct node *)malloc(sizeof(struct node)); ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL) if(ptr == NULL)
{ {
printf("\nOVERFLOW"); printf("\nOVERFLOW\n");
} }
else else
{ {
printf("\nEnter the node data?"); printf("\nEnter Data?");
scanf("%d",&item); scanf("%d",&item);
ptr -> data = item; ptr->data = item;
if(head == NULL) if(head == NULL)
{ {
head = ptr; head = ptr;
ptr -> next = head; ptr -> next = head;
} }
else else
{ {
temp = head; temp = head;
while(temp->next != head) while(temp -> next != head)
temp = temp->next; {
ptr->next = head; temp = temp -> next;
temp -> next = ptr; }
head = ptr; temp -> next = ptr;
} ptr -> next = head;
printf("\nnode inserted\n"); }
}
printf("\nnode inserted\n");
}
Begin Delete Last Delete
struct node *ptr; struct node *ptr, *preptr;
printf("\nnode deleted\n");
}
}
else
{
else
ptr = head;
{ ptr = head; while(ptr ->next != head)
while(ptr -> next != head) {
ptr = ptr -> next; preptr=ptr;
ptr->next = head->next; ptr = ptr->next;
free(head); }