The deletion of linked list is come in 3 case :-
deletion at first
deletion at last
deletion at given point
But this time we are working on deletion at last .
So, here the code for the same hope so it will help you to understand it easily.
/*pre processor*/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
/*Structure of node */
struct node
{
int data;
struct node *ref;
};
/*function that we gonna use throughout the program */
void create(struct node **);
void display(struct node *);
void delast(struct node**);
void main()
{
struct node *head=NULL;
int num,i,l;
clrscr();
printf("\n\tEnter no. of element you want to insert:- ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
create(&head);
}
printf("\nDo You want any deletion at last(1-->Yes and 0-->No):- ");
scanf("%d",&l);
if(l==1)
{
delast(&head);
}
display(head);
getch();
}
void create(struct node **q)
{
struct node *temp, *r;
temp=*q;
if(*q==NULL)
{
temp=(struct node *)malloc(sizeof(struct node ));
printf("\nEnter value:- ");
scanf("%d",&temp->data);
temp->ref=NULL;
*q=temp;
}
else
{
r=(struct node *)malloc(sizeof(struct node));
printf("\nEnter value:- ");
scanf("%d",&r->data);
r->ref=NULL;
while(temp->ref!=NULL)
{
temp=temp->ref;
}
temp->ref=r;
}
}
void display(struct node *q)
{
while(q!=NULL)
{
printf("\n\tOUTPUT:- %d",q->data);
q=q->ref;
}
}
void delast(struct node **q)
{
struct node *temp=*q;
struct node *t;
if(temp->ref==NULL)
{
free(temp);
*q=NULL;
}
else
{ while(temp->ref!=NULL)
{
t=temp;
temp=temp->ref;
}
free(t->ref);
t->ref=NULL;
}
}
deletion at first
deletion at last
deletion at given point
But this time we are working on deletion at last .
So, here the code for the same hope so it will help you to understand it easily.
/*pre processor*/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
/*Structure of node */
struct node
{
int data;
struct node *ref;
};
/*function that we gonna use throughout the program */
void create(struct node **);
void display(struct node *);
void delast(struct node**);
void main()
{
struct node *head=NULL;
int num,i,l;
clrscr();
printf("\n\tEnter no. of element you want to insert:- ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
create(&head);
}
printf("\nDo You want any deletion at last(1-->Yes and 0-->No):- ");
scanf("%d",&l);
if(l==1)
{
delast(&head);
}
display(head);
getch();
}
void create(struct node **q)
{
struct node *temp, *r;
temp=*q;
if(*q==NULL)
{
temp=(struct node *)malloc(sizeof(struct node ));
printf("\nEnter value:- ");
scanf("%d",&temp->data);
temp->ref=NULL;
*q=temp;
}
else
{
r=(struct node *)malloc(sizeof(struct node));
printf("\nEnter value:- ");
scanf("%d",&r->data);
r->ref=NULL;
while(temp->ref!=NULL)
{
temp=temp->ref;
}
temp->ref=r;
}
}
void display(struct node *q)
{
while(q!=NULL)
{
printf("\n\tOUTPUT:- %d",q->data);
q=q->ref;
}
}
void delast(struct node **q)
{
struct node *temp=*q;
struct node *t;
if(temp->ref==NULL)
{
free(temp);
*q=NULL;
}
else
{ while(temp->ref!=NULL)
{
t=temp;
temp=temp->ref;
}
free(t->ref);
t->ref=NULL;
}
}
No comments:
Post a Comment