0% found this document useful (0 votes)
482 views3 pages

Exp. Name: All Operations On Singly Linked List.: S.No: 1 Date:2023-01-14

The document describes a C program that performs various operations on a singly linked list, including creation, insertion, deletion, traversal, and counting of elements. The operations are implemented using functions like insert(), delete(), display(), and count(). The main() function contains a menu that allows the user to choose an operation and call the corresponding function. Sample test cases are provided that insert and display elements in the linked list.

Uploaded by

Ajay Bhoopal
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)
482 views3 pages

Exp. Name: All Operations On Singly Linked List.: S.No: 1 Date:2023-01-14

The document describes a C program that performs various operations on a singly linked list, including creation, insertion, deletion, traversal, and counting of elements. The operations are implemented using functions like insert(), delete(), display(), and count(). The main() function contains a menu that allows the user to choose an operation and call the corresponding function. Sample test cases are provided that insert and display elements in the linked list.

Uploaded by

Ajay Bhoopal
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/ 3

21AG1A1253_Data_Structures_19_2023_01_14.pdf https://aceec.codetantra.com/secure/labs-q.jsp?sNo=1&qId=63a1346...

S.No: 1 Exp. Name: All operations on Singly linked list. Date:2023-01-14

Aim:
Write a program that uses functions to perform the following operations on singly linked list
i)Creation ii)insertion iii)deletion iv) Traversal
Source Code:

Page No: 1
singlelinkedlistalloperations.c

# include <stdio.h>
# include <stdlib.h>

ID: 21AG1A1253
struct node{
int data;
struct node *next;
};
struct node *head;

void insert();
void Delete();
void display();
void count();

void main(){
printf("Singly Linked List Example - All Operations\n");
int choice=0;
while(choice!=5){

printf("Options\n");
printf("1 : Insert elements into the linked list\n");
printf("2 : Delete elements from the linked list\n");
printf("3 : Display the elements in the linked list\n");
printf("4 : Count the elements in the linked list\n");
printf("5 : Exit()\n");
printf("Enter your option : ");
scanf("%d",&choice);
switch(choice){

2021-2025-IT
case 1:
insert();
break;
case 2:
Delete();
ACE Engineering College

break;
case 3:
display();
break;
case 4:
count();
break;
case 5:
break;
default :
printf("Enter options from 1 to 5\n");
return;

1 of 6 14-01-2023, 10:32
21AG1A1253_Data_Structures_19_2023_01_14.pdf https://aceec.codetantra.com/secure/labs-q.jsp?sNo=1&qId=63a1346...

}
}

void insert(){

struct node *ptr,*temp;

Page No:
int item;
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL){
return;

ID: 21AG1A1253
}
else{
printf("Enter elements for inserting into linked list : ");
scanf("%d",&item);
ptr->data=item;

if(head==NULL)
{
ptr->next=NULL;
head=ptr;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
ptr->next=NULL;
}
}
}

void display(){

2021-2025-IT
struct node *ptr;
ptr=head;
if(ptr==NULL)
{
return;
ACE Engineering College

}
else
{
printf("The elements in the linked list are : ");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
}

2 of 6 14-01-2023, 10:32
21AG1A1253_Data_Structures_19_2023_01_14.pdf https://aceec.codetantra.com/secure/labs-q.jsp?sNo=1&qId=63a1346...

void Delete(){
struct node *ptr,*ptr1;
int loc;
printf("Enter position of the element for deleteing the element : ");
scanf("%d",&loc);
ptr=head;
for(int i=0;i<loc-1;i++)
{

Page No:
ptr1=ptr;
ptr=ptr->next;
if(ptr==NULL)
{

ID: 21AG1A1253
return;
}
}
ptr1->next=ptr->next;
free(ptr);
printf("Deleted successfully\n");
}

void count(){
struct node *ptr;
int c=0;
ptr=head;
while(ptr!=NULL){
ptr=ptr->next;
c=c+1;
}
printf("No of elements in the linked list are : %d\n",c);
}

Execution Results - All test cases have succeeded!

Test Case - 1

2021-2025-IT
User Output
Singly Linked List Example - All Operations 1
Options 1
1 : Insert elements into the linked list 1
2 : Delete elements from the linked list 1
ACE Engineering College

3 : Display the elements in the linked list 1


4 : Count the elements in the linked list 1
5 : Exit() 1
Enter your option : 1
Enter elements for inserting into linked list : 100
Options 1
1 : Insert elements into the linked list 1
2 : Delete elements from the linked list 1
3 : Display the elements in the linked list 1
4 : Count the elements in the linked list 1
5 : Exit() 1
Enter your option : 1
Enter elements for inserting into linked list : 200

3 of 6 14-01-2023, 10:32

You might also like