C Program to Create a Linked List with insert node, delete node, update, print and open file
Write a C Program for linked list to display a menu containing the following functions:-update, print,deletenode,addnode and finally open file.
In this C Program, we will create a linked list & display the elements in the list. The linked list is an ordered set of data elements, each containing a link to its successor. This program is to create a linked list and display all the elements present in the created list.
C Programming Code
void init(struct honda *ptr3, int a);
int update(struct honda *ptr1);
int print(struct honda *ptr2);
int add (struct honda *ptr);
int delnode(struct honda *ptr);
int delopr (struct honda *ptr);
int addnode(struct honda *ptr);
int display_data(struct honda *ptr4);
int update_scan(struct honda *scan1);
int update_print(struct honda *print1);
int delete (struct honda *ptr);
int numofel(struct honda *ptr);
int file_open(struct honda *ptr);
int file_write(struct honda *ptr);
int (*functptr[7])(struct honda *ptr1)={update,print,update_scan,update_print,delnode,addnode,file_open};
int main()
{
int n=0,l;
struct honda *head, *ptr;
int a,b;
head=(struct honda *)malloc(sizeof(struct honda));
ptr=head;
printf(“Enter the value of n\n”);
scanf(“%d”,&n);
init(ptr,n);
ptr=head;
display_data(ptr);
return 0;
}
void init(struct honda *st1, int no)
{
int i;
struct honda *st2;
st1->back=’\0′;
st1->front= (struct honda*)malloc(sizeof(struct honda));
for(i=1;i<no;i++)
{
st2=st1;
st1=st1->front;
st1->back=st2;
st1->front=(struct honda *)malloc(sizeof(struct honda));
}
st1->front=’\0′;
}
int addnode(struct honda *ptr)
{
struct honda *headc,*copy,*newptr;
headc=ptr;
ptr=headc;
newptr=ptr;
int p,i,num;
num=numofel(ptr);
printf(“There are %d number of nodes\n”,num);
printf(“Enter the place for the node to be added\n”);
scanf(“%d”,&p);
if(p<=num)
{
for(i=1;i<=p;i++)
{
ptr=ptr->front;
}
copy=ptr->front;
newptr=ptr->front=(struct honda*)malloc(sizeof(struct honda));
ptr->back=ptr;
newptr->front=copy;
copy->back=newptr;
printf(“Done\n\n”);
}
else
{
printf(“Please select between 0 and %d\n”,num);
}
num=numofel(headc);
printf(“%d\n”,num);
return 0;
}
int file_open(struct honda *ptr)
{
char name;
char data[50]= {“Hi my name is Kevin I am an engineer” };
int p;
char *dptr1,*dptr2,*temp;
dptr1=data;
dptr2=data;
struct honda *copy;
FILE *ftr;
size_t s,n;
copy=ptr;
ftr=fopen(“hello.txt”,”r+”);
if(ftr==’\0′)
{
printf(“Failed\n”);
return 0;
}
printf(“Opened\n”);
s=fread(data,1,1000,ftr);