C program to remove last of singly linked list and insert it at beginning of list

How to c program to remove last of singly linked list and insert it at beginning of list in C Programming Language ?



Solution:
/*write a c program to remove last of singly linked list and insert it at beginning of list */

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*start,*q,*temp,*t;
int cnt;
void create();
void display();
void first_insert();
void last_del();
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();
last_del();
first_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 last_del()
{
int i,end;
end=count();
if(end==0)
{
printf("\nlinked list is empty");
}
else
{
q=start;
for(i=0;i<end-2;i++)
{
q=q->next;
}
temp=q->next;
t=temp;
printf("\nlast element %d is deleted",temp->data);
q->next=NULL;
free(temp);
printf("\n\n");
}
}
void first_insert()
{
printf("\n\nlast element is deleted and it is placed at first position");
temp=malloc(sizeof(struct node));
temp->data=t->data;
temp->next=start;
start=temp;
printf("\n\n");
}
int count()
{
q=start;
while(q!=NULL)
{
q=q->next;
cnt++;
}
return cnt;
}
/*
enter the number:-2                                                          
                                                                             
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:-1                                                          
                                                                             
do you want to enter(Y|N):-y                                                  
                                                                             
enter the number:-5                                                          

do you want to enter(Y|N):-n

element in singly linked list are:-
2
6
7
1
5

last element 5 is deleted



last element is deleted and it is placed at first position


element in singly linked list are:-
5
2
6
7
1
*/


Learn More :