0% found this document useful (0 votes)
18 views9 pages

Adobe Scan 24 Nov 2024

The document outlines basic operations for linked lists including insertion, deletion, display, and search functionalities. It provides detailed explanations and code snippets for inserting nodes at the beginning, end, and specific positions, as well as deleting nodes and displaying the list. Additionally, it includes methods for counting nodes and searching for specific elements within the linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

Adobe Scan 24 Nov 2024

The document outlines basic operations for linked lists including insertion, deletion, display, and search functionalities. It provides detailed explanations and code snippets for inserting nodes at the beginning, end, and specific positions, as well as deleting nodes and displaying the list. Additionally, it includes methods for counting nodes and searching for specific elements within the linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Basic Ope ratio ns

• Insertion - Adds an elem ent into the list


• Deletion - Deletes an elem ent from the lis"
• Display - Displays the comp lete list
• Search - Searches an elem ent using the given
key

Linked list nod e crea tion


struc t Node {
int data;
struc t Node * next;
} *head ;
Explanation:
decla red struc ture of type "NODE",
First field store s actual data and another field
stores address
Insertion

• insertion operation can be performed in thr


ways
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific position in the list

Insert at Beginning of the list

0 NUU

l)newnode->next=head;
2)head=newnode
newnode
e g in n in g o f t h e li st
I n s e r t in g A t B
ew node at n in g (i n t value)
st e p s to in se rt a n vo id in se rt A tB e g in
n g le lin ke d lis t
b e g in n in g o f th e si {
ewnode w it h o d e;
Step 1 - C re a te a n st ru ct N o d e •n e w n
g iv e n va lu e. =
n e w n o d e (struet
p 2 - Check w h e th e
r lis t of(st ru ct Node));
Ste N o d e • )malloc(size
is Empty (head ==
NUU)
newnode->data = va
lue;
ty th e n , == NULL)
Step 3 - If it is E m p if( h e a d
= NUU
se t newnode ➔ next {
and head = n e w Node. =ULL;
n e w n o d e -> n e xt N
Empty th e n ,
Step 4 - If it is N o t d head = n e w n o d e;
xt = head a n d hea
se t newnode ➔ ne
}
= newnode
else
{
ad;
n e w n o d e -> n e xt = he
head = n e w n o d e;

g in n in g o f t h e l i s t
I n s e r t in g A t B e
n in g (i n t value)
vo id in se rt A tB e g in
{
n o d e;
st ru ct N o d e •n e w o f( st ru ct Node));
No d e •) m a llo c( si ze
n e w n o d e = (s tr u ct
value;
n e w n o d e -> d a ta =
h e a d;
n e w n o d e -> n e xt =
head = newnode;
}
Insert at End of the list

e3d

A B C

D~l~ Next

tMp
NULL

1) Traverse -Now temp points to last node


2) temp ->next=newnode;

Insert at End of the list


void insertAtEnd(lnt value)
steps to Insert a new node at end of the {
single linked list struct Node •newnode ;
Step 1 - Create a newnode with given newnode = (struct
value and newnode ➔ next as NULL. Node*)malloc(sizeof(struct Node));
Step 2 - Check whether list newnode->data = value;
is Empty (head =: NULL). newnode->next = NULL;
Step 3 • If it is Empty then, if(head == NULL)
set head = newnode. head = newnode;
Step 4 - If i,t is Not Empty then, define a else
node pointer temp and initialize
with head. {
Step 5 • Keep moving the temp to its struct Node •temp = head;
next node until it to the last node In while(temp->next I= NULL)
the list (until temp ➔ next is equal temp = temp->next;
to NULL). temp->next = newnode;
Step 6 • Set temp ➔ next = newnode. }
}
Insert at a given position

A 8 C D NULL

Data Next

tmp

void insertatmiddle(int value, int pos)


{
struct Node •newNode, •temp;
Int i,pos;
newnode = (struct Node•)malloc(sizeof(struct Node));
newnode->data = value;
temp=head;
for(i=l;i<pos-l;i++)
{
temp=temp->next;
}
lf(temp==head)
{
newnode->next=head;
head=newnode;
}
else
(
newnode->next=temp->next;
temp->next=newnode;
}
Deletion Operation
• locate the target node to be removed

• left (previous) node of the target node now


should point to the next node of the target
node
• LeftNode.next -> TargetNode.next;

• remove the target node is pointing at


- TargetNode.next -> NULL;

-- Oll ;a- ~
_ o..-.- ~

7-r IU.L
Delete at beg in

Void deleteatbegin{)
{
struct node *tem p;
temp=head;
print f{"% d is dele ted", temp ->da ta);
head=temp->next;
free{temp);
}

Delete at las t
void deleteatlast()
{
struct node •1ast, •secondlast;
last=head;
secondlast=head • I

while(last->next l=N uLL)


{
secondlast= last;
last=last->next;
}
lf(last==head)
{
head=NULL;
}
else
{
secondlast->next=NULL .
free(last); '
}
Displaying the linked list
Void display()
{
struc t node *tem p;
temp=head;
while(temp!=NULL)
{
printf ("%d ", temp ->da ta);
temp =tem p->n ext;
}
}

Counting the no. of nodes in a Ll


void count()
{
int c=O;
struct node *temp ;
temp=head;
while(templ=NULL)
{
c++;
temp=temp->next;
}
"Number of nodes in the LL is %d", c)·,
} printf(
Searching in a LL
void search()
{
Int key;
printf("enter the element to search");
scanf("%d",&key);
temp= head;
// Iterate till last element until key is not found
while (temp I= NULL && temp->data != key)
{
temp = temp->next;
}
if(temp->data==key)
printf("element found");
else
printf("element not found");
}

Delete first by key


void deleteFirstByKey()
{
Int key;
struct node •tempprev;
r t
Check if head node contains key •
printf("enter the element to delete");
scanf("%d" ,&key);
while (head I= NULL && head->data = key)
{
II Get reference of head node
temp= head;
II Adjust head node link
head = head->next;
II Delete prev since it contains reference to head node
free(temp);
}

You might also like