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

Experiment 6

The document outlines an experiment to implement a singly linked list in C, detailing operations such as insertion and deletion at various positions. It provides algorithms for each operation along with corresponding C code to demonstrate their implementation. Additionally, it discusses applications of linked lists in dynamic memory management, data structures, and undo functionality in software applications.

Uploaded by

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

Experiment 6

The document outlines an experiment to implement a singly linked list in C, detailing operations such as insertion and deletion at various positions. It provides algorithms for each operation along with corresponding C code to demonstrate their implementation. Additionally, it discusses applications of linked lists in dynamic memory management, data structures, and undo functionality in software applications.

Uploaded by

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

EXPERIMENT – 6

AIM: Write a program in C to implement singly linked list and perform the following
operations on it
A. Insertion in beginning
B. Insertion at the end
C. insertion at specific location
D. deletion in beginning
E. deletion at the end
F. deletion at specific location
Algorithm:
A) Insertion in beginning
1) Create the node which is to be inserted, say newnode.
2) If the list is empty, the head will point to the newnode, and we will return.
3) Else, If the list is not empty:
4) Make newnode → next = head. This step ensures that the new node is
being added at the beginning of the list.
5) Then after that, make head = newnode, as the new node is now the first
node of the list, hence the new head.
6) Exit.
B) Insertion in beginning
1) Create the node which is to be inserted, say NewNode, and the
nodewhose address is given is prevnode.
2) If the address of the given node is NULL, simply return.
3) Else, if address is not NULL:
Make the next of NewNode point to the next of prevnode and then
make next of prevnode point to NewNode .
NewNode → next = prevnode→ next
prevnode – > next= NewNode
4) After performing the above steps, our new node NewNode will
get inserted after the prevnode.
5)Exit.

1
C) Insertion at specific location
1) Create the node which is to be inserted, say newnode.
2) If the list is empty, make head point to the newnode, and return.
3) lse, if the list is not empty
Using a pointer p , traverse the list up to the last node of the list.
After the traversal, p will be standing at the last node, and now,
to insert the newnode at the end of the list, make p→ next =
newnode and then newnode → next = NULL, as newnode is the
new new last node of the linked list.
4 ) Exit.
D) Deletion in beginning
1) Create a struct Node* function deleteFirst which will return
the pointer to the new head after deleting the current head.
2)We’ll pass the current head pointer in the function.
3) Create a new struct Node* pointer ptr, and make it point to
the current head.
4) Assign head to the next member of the list, by head = head->
next because this is going to be the new head of the linked list.
5) Free the pointer ptr. And return the head.
E) Deletion at end
1)Deleting the last node is quite similar to deleting from any other
index. The difference holds in the limit of the while loop. Here
we run a loop until the pointer reaches the end and points to NULL.
2)Assign NULL to the next member of the p structure using newnode-> next = NULL;
3)Break the connection between temp and NULL by freeing the ptr temp.
4)Return head.
F) Deletion at specific location
1) Create a struct Node* function deleteAtIndex which will return the pointer to the head.
2) In the function, we'll pass the current head pointer and the index where the node is to be
deleted.

2
3) Create a new struct Node* pointer ptr pointing to head.
4) Create a new struct Node* pointer ptr2 pointing to head->next, and run a loop until this
pointer reaches the index, from where we are deleting the node.
5) Assign q->next to the next member of the p structure using ptr-> next = ptr2->next.
Free the pointer ptr2, because it has zero connections with the list now.
6) Return head.
7)Exit.

CODE:
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node* next;
};
// for print the data
void print_data(struct node* head){
if(head==NULL){
printf("empty list");}
struct node* ptr=NULL;
ptr=head;
while(ptr!=NULL){
printf("\n%d\n",ptr->data);
ptr=ptr->next;
}
}
// insertion at begnning
struct node* insert_at_first(struct node* head, int data){
struct node* newnode=malloc(sizeof(struct node));
newnode->next=head;

3
newnode->data=data;

return newnode;
}
// insertion at specific location
struct node* insert_at_between(struct node* head, int data,int index){
struct node* newnode=malloc(sizeof(struct node));
struct node* p= head;
int i=0;
while(i!= index-1){
p= p->next;
i++;
}
newnode->data=data;
newnode->next = p->next;
p->next=newnode;
return head;
}
// insert at end
struct node* insert_at_end(struct node* head, int data){
struct node* newnode=malloc(sizeof(struct node));
newnode->data=data;
struct node* p=head;
while(p->next!=NULL){
p=p->next;
}
p->next=newnode;
newnode->next=NULL;
return head;

4
}
// deletion in beginning
struct node* delete_first(struct node* head){
struct node* ptr=head;
head=head->next;
free(ptr);
ptr=NULL;
return head;
}
// deletion at the end
struct node* delete_at_last(struct node* head){
struct node* newnode=head;
struct node* temp=head->next;
while(temp->next!=NULL){
newnode=newnode->next;
temp= temp->next;
}
newnode->next = NULL;
free(temp);
return head;
}
// deletion at specific location
struct node* delete_at_index(struct node* head, int index){
struct node* ptr=head;
struct node* ptr2=head->next;
for(int i=0;i< index-1;i++){
ptr=ptr->next;
ptr2=ptr2->next;
}

5
ptr-> next= ptr2->next;
free(ptr2);
return head;
}
int main(){
struct node* head=NULL;
head=malloc(sizeof(struct node));
head->data=10;
head->next=NULL;

struct node* ptr=malloc(sizeof(struct node));


ptr->data=23;
ptr->next=NULL;
head->next=ptr;

ptr=malloc(sizeof(struct node));
ptr->data=30;
ptr->next=NULL;
head->next->next=ptr;

ptr=malloc(sizeof(struct node));
ptr->data=40;
ptr->next=NULL;
head->next->next->next=ptr;
printf("linked list\n");
print_data(head);
printf("insertion at beginning");
head=insert_at_first(head, 11);
print_data(head);

6
printf("insert at specific location (2)");
head=insert_at_between(head,45,2);
print_data(head);
printf("insertion at end");
head=insert_at_end(head,66);
print_data(head);
printf("deletion at beginning");
head=delete_first(head);
print_data(head);
printf("deletion at end");
head=delete_at_last(head);
print_data(head);
printf("deletion at specific location");
head=delete_at_index(head,4);
print_data(head);

return 0;
}
OUTPUT;

Figure1: output of insertion at beginning

7
Figure2: output of insertion at specific location

figure3: output of insertion at end

figure4: output of above deletion at beginning

8
Figure5: output of deletion at end and deletion at specific location.
APPLICATION:
1. Dynamic Memory Management
Insertion and deletion in linked lists allow for dynamic allocation of memory, where nodes
are created and deleted as needed. This flexibility makes linked lists ideal for managing data
where the size can change during runtime, such as in memory allocation systems.
2. Implementation of Data Structures
Linked lists serve as the backbone for many advanced data structures, where insertion and
deletion operations play a crucial role. Examples include:
Stacks (using a singly linked list).
Queues (using a singly or doubly linked list).
Hash Tables (for resolving collisions using separate chaining).
3. Undo Functionality in Applications
Many applications, such as word processors, photo editors, and IDEs, use linked lists to
implement undo/redo features. Each action can be inserted or deleted from a linked list,
allowing users to backtrack or redo actions in sequence.

You might also like