Pages

Friday 22 May 2015

Link List formation

Algorithm


node *n;
n=(node*)malloc(sizeof(node));
n->data=x,y,z;
n->next=21,22,Null;
C program of link list

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct newnode
{
int number;
struct newnode *nextnode;
};
struct newnode *firstnode;
void linklist()
{
char ch='a';
struct newnode *ptr,*n;
while(ch!='n')
{
printf("\nEnter elements");
n=(struct newnode*)malloc(sizeof(struct newnode));
scanf("%d",&n->nnumber);
n->nextnode=0;
if(firstnode==0)
{
firstnode=n;
}
else
{
for(ptr=firstnode ;ptr->nextnode!=0;ptr=ptr->nextnode);
{
ptr->nextnode=n;
}
}
printf("\npress y for continue and n for no");
ch=getch();
}
}
void display()
{
struct newnode *ptr;
printf("Elements are");
for(ptr=firstnode;ptr!=0;ptr=ptr->nextnode)
{
printf("\n%d",ptr->nnumber);
}
}
void main()
{
clrscr();
firstnode=0;
linklist();
display();
getch();
}

No comments:

Post a Comment