0% found this document useful (0 votes)
6 views4 pages

p3

The document outlines a C program for implementing operations on a singly linked list, including creation, insertion, and deletion. It provides a menu-driven interface for users to interact with the linked list and perform various operations. The program defines a structure for the nodes and includes functions for each operation, handling memory allocation and user input appropriately.

Uploaded by

trumantest73
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)
6 views4 pages

p3

The document outlines a C program for implementing operations on a singly linked list, including creation, insertion, and deletion. It provides a menu-driven interface for users to interact with the linked list and perform various operations. The program defines a structure for the nodes and includes functions for each operation, handling memory allocation and user input appropriately.

Uploaded by

trumantest73
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/ 4

PROGRAM 3

Write a C program to implement operations of singly Linked List:


Creation,Insertion, and Deletion.

#include<stdlib.h>
#include <stdio.h>
void create();
void display();
void insert_pos();
void delete_pos();
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
int main()
{
int choice;
while(1){
printf("\n MENU \n");
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insert at specified position\n");
printf("4.Delete from specified position\n");
printf("5.Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_pos();
break;
case 4:
delete_pos();
break;
case 5:
exit(0);
break;
default:
printf("\n Wrong Choice:\n");
break;
}
return 0;
}
void create()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n Out of Memory Space:\n");
exit(0);
}
printf("\n Enter the data value for the node:\t");
scanf("%d",&temp->info);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("\n List is empty:\n");
return;
}
else
{
ptr=start;
printf("\n The List elements are:\n");
while(ptr!=NULL)
{
printf("%d ",ptr->info );
ptr=ptr->next ;
}
}
}
void insert_pos()
{
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n Out of Memory Space:\n");
return;
}
printf("\n Enter the position for the new node to be inserted: ");
scanf("%d",&pos);
printf("\n Enter the data value of the node: ");
scanf("%d",&temp->info) ;
temp->next=NULL;
if(pos==0)
{
temp->next=start;
start=temp;
}
else
{
for(i=0,ptr=start;i<pos-1;i++) { ptr=ptr->next;
if(ptr==NULL)
{
printf("\n Position not found:[Handle with care]\n");
return;
}
}
temp->next =ptr->next ;
ptr->next=temp;
}
}
void delete_pos()
{
int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\n The List is Empty:\n");
exit(0);
}
else
{
printf("\n Enter the position of the node to be deleted: ");
scanf("%d",&pos);
if(pos==0)
{
ptr=start;
start=start->next ;
printf("\n The deleted element is:%d ",ptr->info );
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++)
{
temp=ptr;
ptr=ptr->next ;
if(ptr==NULL)
{
printf("\n Position not Found:\n");
return;
}
}
temp->next =ptr->next ;
printf("\n The deleted element is:%d ",ptr->info );
free(ptr);
}
}
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy