Skip to main content
Write a program to delete a record from last position in the singly link list.
CODING:
#include <stdio.h>
#include<conio.h>
#include <alloc.h>
void main()
{
 struct node{
  int no;
  struct node *next;
 }*node,*start;
 char ch='y';
 clrscr();

 start=(struct node *)malloc(sizeof(struct node));
 node=start;

 while(ch=='y')
 {
clrscr();
printf("\nInput no. : ");
flushall();
scanf("%d",&node->no);
printf("\nEnter y to enter another no. : ");
flushall();
scanf("%c",&ch);

if(ch=='y')
{
node->next=(struct node *)malloc(sizeof(struct node));
node=node->next;
}
else
{
node->next=NULL;
}
 }
 node=start;
 clrscr();
 printf("\nValues :");

 while(node->next!=NULL)
 {
printf("\n%d",node->no);
node=node->next;
 }
 printf("\n%d\n",node->no);
 node=start;

 if(start->next==NULL)
 {
printf("\nThere's only one node in the link list!");
getch();
exit();
 }

 while((node->next->next)!=NULL)
node=node->next;
 node->next=NULL;
 node=start;
 printf("\nValues after deleting ending node :");

 while((node->next)!=NULL)
 {
printf("\n%d",node->no);
node=node->next;
 }
 printf("\n%d",node->no);

 getch();
}

Comments