To create a singly link list using C


Levels of difficulty: / perform operation:

C Program

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct linklist
{
	int number;
	struct linklist *next;
};
typedef struct linklist node;
void create(node *);
void display(node *);
node *head;
void main()
{
	clrscr();
	head=(node *) malloc(sizeof(node));
	create(head);
	display(head);
	getch();
}
void create(node *list)
{
	char conf='y';
	printf("\nENTER A NUMBER: ");
	scanf("%d",&list->number);
	printf("\nWANT TO CONTINUE[Y/N]: ");
	conf=getche();
	printf("\n");
	if(conf=='n' || conf=='N')
		list->next=NULL;
	else
	{
		list->next=(node *) malloc(sizeof(node));
		create(list->next);
	}
}
void display(node *disp)
{
	printf("\nTHE VALUES OF THE LINK LIST ARE:\n");
	while(disp!=NULL)
	{
		printf("%d ",disp->number);
		disp=disp->next;
	}
}


Output







Quantitative Aptitude
Reasoning
Programming
Interview