/* *************************************************
20 . TO INSERT 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(“\n enter an element : “);
scanf(“%d”,&head->info);
head->ptr=NULL;
cur=head;
do
{
temp=(struct link*)malloc(sizeof(struct link));
printf(“\n\n enter number to be inserted : “);
scanf(“%d”,&temp->info);
printf(“\n\n do you want to insert more (yes,no) :”);
ch=getch();
while(cur->ptr!=NULL)
{
cur=cur->ptr;
}
temp->ptr=NULL;
cur->ptr=temp;
}while(ch==’y’);
printf(“\n\n the elements after deletion “);
printf(“\n”);
cur=head;
while(cur!=NULL)
{
printf(“\t%d”,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) :
the elements after deletion
1 2 3
20 . TO INSERT 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(“\n enter an element : “);
scanf(“%d”,&head->info);
head->ptr=NULL;
cur=head;
do
{
temp=(struct link*)malloc(sizeof(struct link));
printf(“\n\n enter number to be inserted : “);
scanf(“%d”,&temp->info);
printf(“\n\n do you want to insert more (yes,no) :”);
ch=getch();
while(cur->ptr!=NULL)
{
cur=cur->ptr;
}
temp->ptr=NULL;
cur->ptr=temp;
}while(ch==’y’);
printf(“\n\n the elements after deletion “);
printf(“\n”);
cur=head;
while(cur!=NULL)
{
printf(“\t%d”,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) :
the elements after deletion
1 2 3
0 comments:
Post a Comment