0% found this document useful (0 votes)
3 views

Linked List

Uploaded by

deepti_chaudhari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Linked List

Uploaded by

deepti_chaudhari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

UNIT 3

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 .

Uses of Linked List


• The list is not required to be contiguously present in the memory. The node can
reside any where in the memory and linked together to make a list. This achieves
optimized utilization of space.
• list size is limited to the memory size and doesn't need to be declared in advance.
• Empty node can not be present in the linked list.
• We can store values of primitive types or objects in the singly linked list.
Node Creation :
struct node
{
int data;
struct node *next;
};
struct node *head, *ptr;
ptr = (struct node *)malloc(sizeof(struct node *));
Insertion in Linked list
SN Operation Description

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 }

{ else if(head -> next == NULL)

ptr = head; {

head = ptr->next; head = NULL;

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

struct node *ptr,*ptr1;


int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;

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

struct node *ptr;


ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
DOUBLY LINKED LIST
What is a doubly-linked list?
A doubly linked list is another type of the linked list. It is called a doubly linked list because it contains
two addresses while a singly linked list contains a single address. It is a list that has total three parts, one
is a data part, and others two are the pointers, i.e., previous and next. The previous pointer holds the
address of the previous node, and the next pointer holds the address of the next node. Therefore, we can
say that list has two references, i.e., forward and backward reference to traverse in either direction.

In c the structure of a node is describe as :


struct node
{
struct node *prev;
int data;
struct node *next;
}
MEMORY REPRESENTATION OF A DOUBLY
LINKED LIST
 Memory Representation of a doubly linked list is shown in the following image. Generally,
doubly linked list consumes more space for every node and therefore, causes more expansive
basic operations such as insertion and deletion. However, we can easily manipulate the
elements of the list since the list maintains pointers in both the directions (forward and
backward).
 In the following image, the first element of the list that is i.e. 13 stored at address 1. The head
pointer points to the starting address 1. Since this is the first element being added to the list
therefore the prev of the list contains null. The next node of the list resides at address 4
therefore the first node contains 4 in its next pointer.
 We can traverse the list in this way until we find any node containing null or -1 in its next part.
Insert at Begining Insert at Last }

struct node *ptr; struct node *ptr,*temp;


int item; int item; }

ptr = (struct node *)malloc(sizeof(struct node)); ptr = (struct node *) malloc(sizeof(struct node)); printf("\nnode inserted\n");

if(ptr == NULL) if(ptr == NULL)


{ {
printf("\nOVERFLOW"); printf("\nOVERFLOW");
} }
else else
{ {
printf("\nEnter Item value"); printf("\nEnter value");
scanf("%d",&item); scanf("%d",&item);
if(head==NULL) ptr->data=item;
{ if(head == NULL)
ptr->next = NULL; {
ptr->prev=NULL; ptr->next = NULL;
ptr->data=item; ptr->prev = NULL;
head=ptr; head = ptr;
} }
else else
{ {
ptr->data=item; temp = head;
ptr->prev=NULL; while(temp->next!=NULL)
ptr->next = head; {
head->prev=ptr; temp = temp->next;
head=ptr; }
} temp->next = ptr;
printf("\nNode inserted\n"); ptr ->prev=temp;
} ptr->next = NULL;
Insert at Position struct node *ptr;
struct node *ptr,*temp; if(head == NULL)
int item,loc,i; {
ptr = (struct node *)malloc(sizeof(struct node)); printf("\n UNDERFLOW");
if(ptr == NULL) }
{ else if(head->next == NULL)
printf("\n OVERFLOW"); {
} head = NULL;
else free(head);
{ printf("\n node deleted\n");
temp=head; }
printf("Enter the location"); else
scanf("%d",&loc); {
for(i=1;i<loc-1; i++) ptr = head;
{ head = head -> next;
temp = temp->next; head -> prev = NULL;
if(temp == NULL) free(ptr);
{ printf("\nnode deleted\n");
printf("\n There are less than %d elements", loc); }
return;
}
}
printf("Enter value");
scanf("%d",&ptr->data);
ptr -> prev = temp;
ptr->next = temp->next;

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;

if(head == NULL) if(head==NULL)


{
{
printf("\nUNDERFLOW");
printf("\nUNDERFLOW");
}
}
else if (head ->next == head)
else if(head->next == head) {
{ head = NULL;
head = NULL; free(head);
free(head); printf("\nnode deleted\n");

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); }

head = ptr->next; preptr->next = ptr -> next;


free(ptr);
printf("\nnode deleted\n");
printf("\nnode deleted\n");
}
}
Search node {
struct node *ptr; if(ptr->data == item)
int item,i=0,flag=1; {
ptr = head; printf("item found at location %d ",i+1);
if(ptr == NULL) flag=0;
{ break;
printf("\nEmpty List\n"); }
} else
else {
{ flag=1;
printf("\nEnter item which you want to search?\ }
n"); i++;
scanf("%d",&item); ptr = ptr -> next;
if(head ->data == item) }
{ }
printf("item found at location %d",i+1); if(flag != 0)
flag=0; {
} printf("Item not found\n");
else }
{ }
while (ptr->next != head)
Display linked list
struct node *ptr;
ptr=head;
if(head == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n printing values ... \n");

while(ptr -> next != head)


{

printf("%d\n", ptr -> data);


ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
Dynamic memory allocation in C
The concept of dynamic memory allocation in c language enables the
C programmer to allocate memory at runtime. Dynamic memory
allocation in c language is possible by 4 functions of stdlib.h header
file.
malloc()
calloc()
realloc()
free()
Before learning above functions, let's understand the difference
between static memory allocation and dynamic memory allocation.
static memory allocation dynamic memory allocation
memory is allocated at compile memory is allocated at run time.
time.
memory can't be increased while memory can be increased while
executing program. executing program.
used in array. used in linked list.

malloc() allocates single block of


requested memory.
calloc() allocates multiple block of
requested memory.
realloc() reallocates the memory occupied
by malloc() or calloc() functions.
free() frees the dynamically allocated
memory.
malloc
ptr=(cast-type*)malloc(byte-size)
Calloc
ptr=(cast-t ype*)calloc(number, byte-size)
 The calloc() function allocates multiple block of requested
memory.
 It initially initialize all bytes to zero.

 It returns NULL if memory is not sufficient.

You might also like