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

3.1 Single Linked List

The document provides an overview of linked lists, a linear data structure consisting of nodes that store data and pointers to the next node. It details the types of linked lists (singly, doubly, and circular), their memory representations (static and dynamic), and various operations such as creation, insertion, deletion, and traversal. Additionally, it includes code snippets for implementing these operations in a singly linked list.

Uploaded by

karanam bharggav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

3.1 Single Linked List

The document provides an overview of linked lists, a linear data structure consisting of nodes that store data and pointers to the next node. It details the types of linked lists (singly, doubly, and circular), their memory representations (static and dynamic), and various operations such as creation, insertion, deletion, and traversal. Additionally, it includes code snippets for implementing these operations in a singly linked list.

Uploaded by

karanam bharggav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 58

MODULE 3

Linked list
A linked list is a linear data structure where
data is stored in the form of a node.

 Infofield represents the data and next field


represents the address of next node
Types of Linked List
 Singly Linked list

 Doubly Linked List

 Circular Linked list


SIGNLY LINKED LIST
Linked list
 Representation of Linked list in Memory:

 Thereare two ways to represent a linked list


in memory
 Static representation using array
 Dynamic representation .
Static Representation
Static representation of a linked list maintains two
arrays: one array for the data and the other for
next. Static representation for the linked list is as
shown in below figure
Dynamic Representation
The efficient way of representing a linked list is using
pointer, one such simple representation is shown below.

Here head is a pointer that points to the head node in the


list and last node next part contains a NULL denoting
that it is the end of the list
Basic Condition
 Head is a pointer which point of he first node in the list.
 If the list is empty head=NULL

 The last node next filed contains NULL indicating that

it’s the last node in the list.

Note: What ever operations we do on linked list we should


not violate the above basic Conditions.
Operations on Singly Linked list
 Creation of the list
 Insertion at the Front end

 Insertion a the rear end

 Insertion at a specified position

 Deletion at the Front end

 Deletion at the Rear end

 Deletion of a specified node

 Traversal ( Display )
Creation of list
struct slist
{

int info;
slist *next;
};
typedef struct slist* node;
INSERTION AT FRONT END
Insertion at the Front end
Insertion at the Front end
1. create a new node ( say temp) containing the
information to be inserted.
2. slist newnode;
3. newnode->info=data;
4. newnode->next=NULL;
5. If the list is empty i.e. head = NULL then make
the new node as the head node in the list
6. head=newnode;
Insertion at the Front end
7. If the list is not empty then
- Store the address of head node in the next part
of the new node and
-Make the new node as the head node
newnode->next=head;
head = newnode;
Insertion at the Front end
1. create a new node ( say temp) containing
the information to be inserted.
2. node temp;
3. temp->info=data;
4. temp->next=NULL;
Insertion at the Front end
5. If the list is empty i.e. head = NULL then
make the new node as the head node in the
list
Insertion at the Front end
5. If the list is empty i.e. head = NULL then
make the new node as the head node in the
list
6. head=temp; head
Insertion at the Front end
7. If the list is not empty then
- Store the address of head node in the next
part of the new node
temp->next=head
Insertion at the Front end

8. -Make the new node as the head node


head = temp;
CODE
void beginsert()
{ int data;
printf("\nEnter value\n");
scanf("%d",&data);
temp->info = data;
temp->next=NULL;
if(head == NULL)
{
head=temp;
}
else
{
temp->next = head;
head = temp;
printf("\nNode inserted"); } }
INSERTION AT REAR END
Insertion at Rear end
Insertion at the Rear end
1. create a new node ( say temp) containing the
information to be inserted.
2. node temp;
3. temp->info=data;
4. temp->next=NULL;
5. If the list is empty i.e. head = NULL then
make the new node as the head node in the
list
6. head=temp;
Insertion at the Rear end
7. If the list is not empty then
 -create a temporary pointer (say curn) and store the

address of the head node and move to the end of


the list.
 -After reaching the last node store the address of the

new node in the next part of the last node


slist curn;
Curn=head;
While (curn->next!=NULL)
Curn=curn->next;
Curn->next=newnode;
Insertion at the Rear end
1. create a new node ( say temp) containing
the information to be inserted.
2. node temp;
3. temp->info=data;
4. temp->next=NULL;
Insertion at the Rear end
5. If the list is empty i.e. head = NULL then
make the new node as the head node in the
list
Insertion at the Rear end
5. If the list is empty i.e. head = NULL then
make the new node as the head node in the
list
6. head=temp; head
Insertion at the Rear end
7. If the list is not empty then
 -create a temporary pointer (say curn) and

store the address of the head node and move


to the end of the list.

 -After reaching the last node store the address


of the new node in the next part of the last
node
Insertion at the Rear end
 slist curn =head;
Insertion at the Rear end
While (curn->next!=NULL)
Curn=curn->next;

curn-> next is 200 which is not equal to NULL so curn moves to next node
Insertion at the Rear end
While (curn->next!=NULL)
Curn=curn->next;

curn-> next is 300 which is not equal to NULL so curn moves to next node
Insertion at the Rear end
While (curn->next!=NULL)
Curn=curn->next;

curn-> next is NULL which is not equal to NULL so we stop here


Insertion at the Rear end
 Curn->next=temp;
CODE
void lastinsert()
{ int data;
printf("\nEnter value\n"); else
{
scanf("%d",&info); curn = head;
temp->info = data; while (curn -> next != NULL)
{
temp->next=NULL; curn = curn -> next;
if(head == NULL) }
curn->next = temp;
{
head=temp; printf("\nNode inserted");
}
}
}
}
Insertion at a specified
else
position {
void randominsert() curn=head;
{ for(i=0;i<loc;i++)
printf("\nEnter element value"); {
curn = curn->next;
scanf("%d",&item);
if(curn == NULL)
temp->data = item;
{
temp->next=NULL; printf("\ncan't insert\
printf("\ n");
nEnter the location after which you want return;
to insert "); }
scanf("\n%d",&loc);
if(head == NULL) }
{ temp ->next = curn -
>next;
printf("list is empty"); curn ->next = temp;
} printf("\nNode inserted");
}
DELETION AT FRONT END
Deletion at Front end
Deletion at Front end
void deletion_front(slist *head)
{
if(head==NULL) //CASE-1
printf(“LIST IS EMPTY");
else //CASE 2 & 3
{
printf("\nDeleted element is %d",head->info) ;
head=head->next ;
}
}
Deletion at Front end
CASE-1
1. If the list is empty i.e. head=NULL just
display a message that list is empty.

Print list empty


Deletion at Front end
CASE-2 & 3
1. Print deleted node is head->info;

2. Head=head->next
DELETION AT REAR END
Deletion at Rear end
Deletion at Rear end
void deletion_rear(slist * head)
{
slist * curn, *prev;
if(head==NULL) // CASE-1
printf(“LIST IS EMPTY");
else // CASE-2 & 3
{
curn=head;
prev=curn;
while(curn->next !=NULL)
{
prev=curn;
curn=curn->next ;
}
printf("\nDeleted element is %d ",curn->info) ;
delete curn;
prev->next =NULL;
}
}
Deletion at rear end
CASE-1
1. if(head==NULL)

printf(“List is empty”);
Deletion at Rear end
CASE-2 & 3
1. curn=head;
2. prev=curn;
Deletion at Rear end
CASE-2 & 3
1. curn=head;
2. prev=curn;
3. while(curn->next !=NULL)
4. {
5. prev=curn;
6. curn=curn->next ;
7. }

Curn-> next is 200 which is not NULL so we enter into while loop and
curn moves to next node
Deletion at Rear end
CASE-2 & 3
1. curn=head;
2. prev=curn;
3. while(curn->next !=NULL)
4. {
5. prev=curn;
6. curn=curn->next ;
7. }

Curn-> next is 300 which is not NULL so curn moves to next node
Deletion at Rear end
CASE-2 & 3
1. curn=head;
2. prev=curn;
3. while(curn->next !=NULL)
4. {
5. prev=curn;
6. curn=curn->next ;
7. }

Now Curn-> next is NULL so we stop


Deletion at Rear end
CASE-2 & 3
1. print curn->info deleted
2. delete curn;
3. prev->next =NULL;
Deletion of a specified node

void random_delete() if(curn == NULL)


{ {
printf("\ printf("\nCan't delete");
n Enter the location"); scanf("% return;
d",&loc); }
curn=head; }
for(i=0;i<loc;i++) prev->next = curn ->next;
{ free(curn);
printf("\nDeleted node %d ",loc+1);
prev= curn; }
curn = curn->next;
DISPLAY
Display
Display
void display(slist *head)
{
slist * curn;
if(head==NULL) // CASE-1
printf("LIST IS EMPTY");
else // CASE-2
{
curn=head;
while(curn !=NULL)
{
Printf(“\t%d”,curn->info );
curn =curn->next ;
}
}
}
Display
CASE-1
1. if(head==NULL)

printf(“List is empty”);
Display
CASE-2
1. else
2. { curn=head;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Curn is 100 its not equal to NULL, so curn->info that’s 10 is displayed and
curn moves to next node

10
Display
CASE-2
1. else
2. { curn=head;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Curn is 200 its not equal to NULL, so curn->info that’s 20 is displayed and
curn moves to next node

10 20
Display
CASE-2
1. else
2. { curn=head;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Curn is 300 its not equal to NULL, so curn->info that’s 30 is displayed and
curn moves to next node

10 20 30
Display
CASE-2
1. else
2. { curn=head;
3. while(curn !=NULL)
4. {
5. Printf(“\t%d”,curn->info );
6. curn =curn->next ;
7. }
8. }
9. }
Now Curn is NULL so while is terminated.

10 20 30

You might also like