How to c program to remove last of singly linked list and insert it at beginning of list in C Programming Language ?
/*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 :
Data Structure
- C Program To Read A Parenthesised Infix Expression From The User And Check Whether It Is Well Parenthesised Or Not
- C Program To Remove Last Of Singly Linked List And Insert It At Beginning of List
- C Program To Remove First Node Of Singly Linked List And Insert It At End Of The List
- C Program To Read The Adjecancy Matrix of Directed Graph And Convert It Into Adjecancy List
- C Program To Create Two Singly Linked List and Perform Following Operation
- C Program - Hash Table to store information about a student
- Create Two Singly Linked List Perform Differences Display It C Program
- GJK C Program Example-1
- Adding two polynomial functions C Program Using Structure
- Program to Add Two Polynomials Using Linked List C Program
- Adding Two Polynomial Functions in C Program