Sunday, January 15, 2023

Write a singly linked list function for remove first node and add at last position

 Hello Guys,

Today We Talk About means Write code About Question which asks by University.

Most of Time In Savitribai Phule Pune University's Question Paper this tyoe Question asks for 16 marks.



Q.1 Write a function to remove First node singly linked list and add it at the end of linked list.

//function to remove First node singly linked list and add it at the end of linked list.
//rf=remove first and al=add last
void rf_and_al()
{
struct node *a=head,*b=head;
while(b->next!=NULL)
{
    b=b->next;
}
if(head==b) //means Linked list contains only one Node
{
    b=head;
}
else
{
head=a->next;
b->next=a;
a->next=NULL;
}}

Full Program for Remove First node singly linked list and add it at the end of linked list:

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *newnode,*head,*temp;

void create()
{int num,i;
printf("\nHow many nodes you want to create:");
scanf("%d",&num);
for(i=0; i<num; i++)
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter Data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL)
{head=temp=newnode;}
else
{temp->next=newnode;
temp=temp->next;
}}}
//function to remove First node singly linked list and add it at the end of linked list.
//rf=remove first and al=add last
void rf_and_al()
{
struct node *a=head,*b=head;
while(b->next!=NULL)
{
    b=b->next;
}
if(head==b) //means Linked list contains only one Node
{
    b=head;
}
else
{
head=a->next;
b->next=a;
a->next=NULL;
}}

void display()
{
    struct node *p;
p=head;
while(p->next!=NULL)
{
printf("%d->", p->data);
p=p->next;
}
printf("%d->",p->data);
}
void main()
{
    create();
    display();
    rf_and_al();
printf("\nAfter Removing First Node and Add it last:");
    display();
    getch();
}

Output:


Copy Full program for executing code without any Error.

Thanks...

No comments: