0% found this document useful (0 votes)
1K views

Practical Number-8: Write A Program To Perform Insertion and Deletion in Single Linked List

The document describes a C program to perform insertion and deletion operations on a single linked list. The program contains functions to insert nodes at the beginning, end or a specified position in the list. It also contains functions to delete nodes from the beginning, end or a specified position. The main function uses a menu to allow the user to choose an insertion, deletion or display operation and test the functions by building a sample linked list of values 4->8->3.

Uploaded by

Ritik Tyagi
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)
1K views

Practical Number-8: Write A Program To Perform Insertion and Deletion in Single Linked List

The document describes a C program to perform insertion and deletion operations on a single linked list. The program contains functions to insert nodes at the beginning, end or a specified position in the list. It also contains functions to delete nodes from the beginning, end or a specified position. The main function uses a menu to allow the user to choose an insertion, deletion or display operation and test the functions by building a sample linked list of values 4->8->3.

Uploaded by

Ritik Tyagi
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/ 7

PRACTICAL NUMBER-8

18BCS1678

Shreya Maity

CSE-7(A)

AIM:

Write a Program to perform insertion and deletion in single linked list.

CODE:

#include<stdio.h>
#include<conio.h>
#include<process.h>

struct node
{
int data;
struct node *next;
}*start=NULL,*q,*t;

int main()
{
int ch;
void insert_beg();
void insert_end();
int insert_pos();
void display();
void delete_beg();
void delete_end();
int delete_pos();

while(1)
{
printf("\n\n---- Singly Linked List(SLL) Menu ----");
printf("\n1.Insert\n2.Display\n3.Delete\n4.Exit\n\n");
printf("Enter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1:
printf("\n---- Insert Menu ----");
printf("\n1.Insert at beginning\n2.Insert at end\n3.Insert at specified position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1: insert_beg();
break;
case 2: insert_end();
break;
case 3: insert_pos();
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
break;

case 2: display();
break;

case 3: printf("\n---- Delete Menu ----");


printf("\n1.Delete from beginning\n2.Delete from end\n3.Delete from specified
position\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1: delete_beg();
break;
case 2: delete_end();
break;
case 3: delete_pos();
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
break;
case 4: exit(0);
default: printf("Wrong Choice!!");
}
}
return 0;
}

void insert_beg()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
if(start==NULL) //If list is empty
{
t->next=NULL;
start=t;
}
else
{
t->next=start;
start=t;
}
}

void insert_end()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
t->next=NULL;

if(start==NULL) //If list is empty


{
start=t;
}
else
{
q=start;
while(q->next!=NULL)
q=q->next;
q->next=t;
}
}

int insert_pos()
{
int pos,i,num;
if(start==NULL)
{
printf("List is empty!!");
return 0;
}

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


printf("Enter data:");
scanf("%d",&num);
printf("Enter position to insert:");
scanf("%d",&pos);
t->data=num;

q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}

q=q->next;
}

t->next=q->next;
q->next=t;
return 0;
}

void display()
{
if(start==NULL)
{
printf("List is empty!!");
}
else
{
q=start;
printf("The linked list is:\n");
while(q!=NULL)
{
printf("%d->",q->data);
q=q->next;
}
}
}

void delete_beg()
{
if(start==NULL)
{
printf("The list is empty!!");
}
else
{
q=start;
start=start->next;
printf("Deleted element is %d",q->data);
free(q);
}
}

void delete_end()
{
if(start==NULL)
{
printf("The list is empty!!");
}
else
{
q=start;
while(q->next->next!=NULL)
q=q->next;

t=q->next;
q->next=NULL;
printf("Deleted element is %d",t->data);
free(t);
}
}

int delete_pos()
{
int pos,i;

if(start==NULL)
{
printf("List is empty!!");
return 0;
}

printf("Enter position to delete:");


scanf("%d",&pos);

q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}
q=q->next;
}

t=q->next;
q->next=t->next;
printf("Deleted element is %d",t->data);
free(t);

return 0;
}
OUTPUT:
---- Singly Linked List(SLL) Menu ----
1.Insert
2.Display
3.Delete
4.Exit

Enter your choice(1-


4):1

---- Insert Menu ----


1.Insert at beginning
2.Insert at end
3.Insert at specified position
4.Exit

Enter your choice(1-


4):1

Enter data:4
---- Singly Linked List(SLL) Menu ----
1.Insert
2.Display
3.Delete
4.Exit

Enter your choice(1-


4):1

---- Insert Menu


1.Insert at beginning
2.Insert at end
3.Insert at specified position
4.Exit

Enter your choice(1-


4):2

Enter data:
3

---- Singly Linked List(SLL) Menu ----


1.Insert
2.Display
3.Delete
4.Exit

Enter your choice(1-


4):1

---- Insert Menu ----


1.Insert at beginning
2.Insert at end
3.Insert at specified position
4.Exit

Enter your choice(1-


4):3

Enter data:8
Enter position to insert:2
---- Singly Linked List(SLL) Menu ----
1.Insert
2.Display
3.Delete
4.Exit

Enter your choice(1-


4):2

The linked list is:


4->8->3->

---- Singly Linked List(SLL) Menu ----


1.Insert
2.Display
3.Delete
4.Exit

Enter your choice(1-


4):3

---- Delete Menu ----


1.Delete from beginning
2.Delete from end
3.Delete from specified position
4.Exit

Enter your choice(1-


4):1

Deleted element is 4
---- Singly Linked List(SLL) Menu ----
1.Insert
2.Display
3.Delete
4.Exit

Enter your choice(1-


4):2

The linked list is:


8->3-
>
---- Singly Linked List(SLL) Menu ----
1.Insert
2.Display
3.Delete
4.Exit

Enter your choice(1-


4):4

...Program finished with exit code 0

Press ENTER to exit console.

You might also like