C Program To Remove First Node Of Singly Linked List And Insert It At End Of The List

How to write a C program to remove first node of singly linked list and insert it at end of the list in C Programming Language ?


Solution:
/*write a c program to remove first node of singly linked list and insert it at end of the list */


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*start,*q,*temp;
int cnt,num;
void create();
void display();
void first_del();
void last_insert();
int count();
void main()
{
int ch='y';
clrscr();
while(ch=='y'||ch=='Y')
{
create();
printf("\ndo you want to enter(Y|N):-");
scanf("%s",&ch);
}
display();
first_del();
last_insert();
display();
getch();
}
void create()
{
temp=malloc(sizeof(struct node));
printf("\nenter the number:-");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q->next!=NULL)
{
q=q->next;
}
q->next=temp;
}
}
void display()
{
if(start==NULL)
printf("\nlinked list is empty");
else
{
printf("\nelement in singly linked list are:-\n");
q=start;
while(q!=NULL)
{
printf("%d\n",q->data);
q=q->next;
}
}
}
void first_del()
{
temp=start;
num=start->data;
printf("\nthe %d element is deleted",num);
start=start->next;
free(temp);
}
void last_insert()
{
int i,end;
q=start;
end=count();
if(end==0)
printf("linked list is empty");
else
{
printf("\n\nfirst node is deleted and it is placed at last position");
for(i=0;i<end-1;i++)
{
q=q->next;
if(q==NULL)
{
printf("invalid position");
return;
}
}
temp=malloc(sizeof(struct node));
temp->data=num;
temp->next=q->next;
q->next=temp;
}
}
int count()
{
struct node *q=start;
cnt=0;
while(q!=NULL)
{
q=q->next;
cnt++;
}
return cnt;
}
/*
enter the number:-4                                                          
                                                                             
do you want to enter(Y|N):-y                                                  
                                                                             
enter the number:-6                                                          

do you want to enter(Y|N):-y                                                  
                                                                             
enter the number:-7                                                          
                                                                             
do you want to enter(Y|N):-y                                                  
                                                                             
enter the number:-2                                                          
                                                                             
do you want to enter(Y|N):-n                                                  
                                                                             
element in singly linked list are:-                                          
4                                                                            
6                                                                            
7
2

the 4 element is deleted

first node is deleted and it is placed at last position
element in singly linked list are:-
6
7
2
4
*/


Learn More :