/* *************************************************
21 . TO DELETE AN ELEMENT AT THE END
************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*malloc.h>
void main()
{
struct link
{
int info;
struct link*ptr;
}*head,*cur,*temp;
char ch='y';
clrscr();
head=(struct link*)malloc(sizeof(struct link));
printf("\nenter an element :");
scanf("%d",&head->info);
head->ptr=NULL;
cur=head;
do
{
temp=(struct link*)malloc(sizeof(struct link));
printf("\nenter number to be inserted :");
scanf("%d",&temp->info);
printf("\ndo you want to insert more.....");
ch=getch();
while(cur->ptr!=NULL)
{
cur=cur->ptr;
}
temp->ptr=NULL;
cur->ptr=temp;
}while(ch=='y');
cur=head;
temp=cur->ptr;
do
{
cur=cur->ptr;
temp=temp->ptr;
}while(temp->ptr!=NULL);
cur->ptr=NULL;
free(temp);
cur=head;
printf("\nelements after deletion :\n");
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->ptr;
}
getch();
}
output
enter an element : 1
enter number to be inserted : 2
do you want to insert more(yes,no) :
enter number to be inserted : 3
do you want to insert more(yes,no) :
elements after deletion :
1 2
21 . TO DELETE AN ELEMENT AT THE END
************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*malloc.h>
void main()
{
struct link
{
int info;
struct link*ptr;
}*head,*cur,*temp;
char ch='y';
clrscr();
head=(struct link*)malloc(sizeof(struct link));
printf("\nenter an element :");
scanf("%d",&head->info);
head->ptr=NULL;
cur=head;
do
{
temp=(struct link*)malloc(sizeof(struct link));
printf("\nenter number to be inserted :");
scanf("%d",&temp->info);
printf("\ndo you want to insert more.....");
ch=getch();
while(cur->ptr!=NULL)
{
cur=cur->ptr;
}
temp->ptr=NULL;
cur->ptr=temp;
}while(ch=='y');
cur=head;
temp=cur->ptr;
do
{
cur=cur->ptr;
temp=temp->ptr;
}while(temp->ptr!=NULL);
cur->ptr=NULL;
free(temp);
cur=head;
printf("\nelements after deletion :\n");
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->ptr;
}
getch();
}
output
enter an element : 1
enter number to be inserted : 2
do you want to insert more(yes,no) :
enter number to be inserted : 3
do you want to insert more(yes,no) :
elements after deletion :
1 2
0 comments:
Post a Comment