/* ****************************************************
16 . TO INSERT AN ELEMENT AT THE BEGINING
************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*malloc.h>
void main()
{
struct link
{
int info;
struct link*next;
};
struct link*head,*cur,*temp;
char ch='y';
clrscr();
head=(struct link*)malloc(sizeof(struct link));
printf("\n enter head element : ");
scanf("%d",&head->info);
head->next=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 do you want to insert more (yes,no) : ");
ch=getch();
temp->next=cur;
cur=temp;
}while(ch=='y');
head=cur;
printf("\n\n");
while(cur!=NULL)
{
printf("\t%d",cur->info);
cur=cur->next;
}
getch();
}
output
enter head 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) :
3 2 1
16 . TO INSERT AN ELEMENT AT THE BEGINING
************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*malloc.h>
void main()
{
struct link
{
int info;
struct link*next;
};
struct link*head,*cur,*temp;
char ch='y';
clrscr();
head=(struct link*)malloc(sizeof(struct link));
printf("\n enter head element : ");
scanf("%d",&head->info);
head->next=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 do you want to insert more (yes,no) : ");
ch=getch();
temp->next=cur;
cur=temp;
}while(ch=='y');
head=cur;
printf("\n\n");
while(cur!=NULL)
{
printf("\t%d",cur->info);
cur=cur->next;
}
getch();
}
output
enter head 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) :
3 2 1
0 comments:
Post a Comment