Q. Write a C program that create a singly linked list.
Ans.
/*c program for creating singly linked list*/
#include<stdio.h>
#include<conio.h>
struct single_link_list
{
int age;
struct single_link_list *next;
};
typedef struct single_link_list node;
node *makenode(int );
int main()
{
int ag;
node *start,*last,*nn; //nn=new node
start=NULL;
while(1)
{
printf("Enter your age : ");
scanf("%d",&ag);
if(ag==0)
break;
nn=makenode(ag);
if(start==NULL)
{
start = nn;
last = nn;
}
else
{
last->next = nn;
last = nn;
}
}
printf("\n\t****Single linked list****\n\n");
for(; start!=NULL; start=start->next)
printf("%d\t",start->age);
getch();
return 0;
}
/*creation of node*/
node *makenode(int tmp)
{
node *nn;
nn = (node *)malloc(sizeof(node));
nn->age = tmp;
nn->next = NULL;
return nn;
}
Ans.
/*c program for creating singly linked list*/
#include<stdio.h>
#include<conio.h>
struct single_link_list
{
int age;
struct single_link_list *next;
};
typedef struct single_link_list node;
node *makenode(int );
int main()
{
int ag;
node *start,*last,*nn; //nn=new node
start=NULL;
while(1)
{
printf("Enter your age : ");
scanf("%d",&ag);
if(ag==0)
break;
nn=makenode(ag);
if(start==NULL)
{
start = nn;
last = nn;
}
else
{
last->next = nn;
last = nn;
}
}
printf("\n\t****Single linked list****\n\n");
for(; start!=NULL; start=start->next)
printf("%d\t",start->age);
getch();
return 0;
}
/*creation of node*/
node *makenode(int tmp)
{
node *nn;
nn = (node *)malloc(sizeof(node));
nn->age = tmp;
nn->next = NULL;
return nn;
}
/**************** OUTPUT *****************/
Singly linked list |
Related post:
- What is linked list in C?
- Traversing the list
- Search item in linked list
- Insert an item
- Deleting an item
- All linked list operation in program
No comments:
Post a Comment