6.Write a C Program to delete a node at the end of a singly linked list.
#include< conio.h>
#include< stdio.h>
#include< stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *start;
void insert_begin();
void delete_last();
void print();
void main ()
{
int ch=0;
while(ch!=8)
{
printf("\nEnter the operation to be performed\n");
printf("\n1.Insert in the begining \n 2.Delete from last \n 3.Show \n 4.Exit\n");
scanf("\n%d",&ch);
switch(ch)
{ /*function calls of all the operations */
case 1:
insert_begin();
break;
case 2:
delete_last();
break;
case 3:
print();
break;
case 4:
exit(0);
break;
default:
printf("Enter valid option");
}
}
getch();
}
void insert_begin()
{
struct node *p;
int value;
p=(struct node *) malloc(sizeof(struct node *));
if(p==NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&value);
p->data=value;
p->next=start;
start=p;
}
}
void delete_last() //to delete the node present in the last of the linked list
{
struct node *p,*p1;
if(start==NULL)
{
printf("\nlist is empty");
}
else if(start->next==NULL)
{
start=NULL;
free(start);
printf("\nOnly node of the list deleted ...\n");
}
else
{
p=start;
while(p->next!=NULL)
{
p1=p;
p=p->next;
}
p1->next=NULL;
free(p);
}
}
void print() //to print the values in the linked list
{
struct node *p;
p=start;
if(p==NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values\n");
while (p!=NULL)
{
printf("\n%d",p->data);
p=p->next;
}
}
}