//Program to insert elements at the end of linked list
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node* link;
};
struct node*start=NULL;
void
insert()
{
struct node * ptr=(struct
node*)malloc(sizeof(struct node));
int data;
struct node * a= start;
printf("\nEnter data to be inserted :
");
scanf("%d",&ptr->data);
if(start==NULL)
{
ptr->link=start;
start=ptr;
}
while(a->link!=NULL)
{
a=a->link;
}
a->link=ptr;
ptr->link=NULL;
}
void display()
{
struct node*temp=start;
printf("\nList is : ");
while(temp!=NULL)
{
printf("%d ",temp->data );
temp=temp->link;
}
}
void main()
{
int n,i;
clrscr();
do
{
insert();
printf("\nPress 1 to insert more else
press 0 : " );
scanf("%d",&n);
}while(n==1);
display();
getch();
}
Output:
No comments:
Post a Comment