Thursday, 7 March 2013

c program for adding and displaying nodes in link list

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct node
{
int data ;
struct node *link;

}node;

node *start;

void main()
{
char ch ;
start = '\0';
printf("\nwant to create node?");
ch = getche();
while (ch=='y'||ch=='Y')
{
create();
printf("\nwant to add more y/n");
ch = getche();
}

printf("\nwant to display ?y/n");
ch = getche();
if(ch =='y'||ch =='Y')
{
display();
}
getch();
}


create()
{
node *x,*t;
x=(node*)malloc(sizeof(node));
printf("\nenetr data ");
scanf("%d",&x->data) ;
printf("%d",x->data) ;
if (start =='\0')
{
x->link = '\0' ;
start=x;
}
else
{
t=start;
while(t->link!='\0')
{
t=t->link;
}
t->link=x;
x->link='\0';
}
}

display()
{
node *t;
t=start;
while(t!='\0')
{
printf("\ndata = %d",t->data);
t=t->link;
}
}

No comments:

Post a Comment