Solution
0 #include<stdio.h>
1 #include<string.h>
2
3 //node definition
4 struct node{
5 char name[50];
6 int roll_number;
7 struct node *next;
8 };
9 typedef struct node node;
10
11 node* addToBegining(node *first,char name[],int roll_no){
12 node *new_node=(node *)malloc(sizeof(node));
13
14 //initialise new node
15 strcpy(new_node->name,name);
16 new_node->roll_number=roll_no;
17 new_node->next=NULL;
18
19 //make first point to new node if first is NULL
20 if(first==NULL){
21 first=new_node;
22 return first;
23 }
24
25 //attach new node to begining of list
26 new_node->next=first;
27 first=new_node;
28
29 //return modified value
30 return first;
31
32 }
33
34 void printList(node *first){
35 node *temp=first;
36 while(temp!=NULL){
37 //print values
38 printf("Name:%s\n",temp->name);
39 printf("Number:%d\n",temp->roll_number);
40 printf("\n");
41 //traverse the list
42 temp=temp->next;
43 }
44 }
45
46 void readline(char str[],int n){
47 char c=0;
48 int i=0;
49 //I used this function because scanf cannot
50 //read spaces.
51 if((c=getchar())!='\n')
52 str[i++]=c;
53 for(;i<n-1 && (c=getchar())!=EOF && c!='\n';++i)
54 str[i]=c;
55
56 str[i]='\0';
57
58 }
59
60
61 int main(){
62 int i=0;
63 char name[50];
64 int roll_no;
65 node *first=NULL;
66
67 for(i=0;i<5;i++){
68 printf("Enter Name:");
69 readline(name,50);
70 printf("Enter Roll No:");
71 scanf("%d",&roll_no);
72
73 first=addToBegining(first,name,roll_no);
74 }
75 printList(first);
76 }
If you are clever enough you should be able to use this code with
out using a text editor to remove the line numbers.1 #include<string.h>
2
3 //node definition
4 struct node{
5 char name[50];
6 int roll_number;
7 struct node *next;
8 };
9 typedef struct node node;
10
11 node* addToBegining(node *first,char name[],int roll_no){
12 node *new_node=(node *)malloc(sizeof(node));
13
14 //initialise new node
15 strcpy(new_node->name,name);
16 new_node->roll_number=roll_no;
17 new_node->next=NULL;
18
19 //make first point to new node if first is NULL
20 if(first==NULL){
21 first=new_node;
22 return first;
23 }
24
25 //attach new node to begining of list
26 new_node->next=first;
27 first=new_node;
28
29 //return modified value
30 return first;
31
32 }
33
34 void printList(node *first){
35 node *temp=first;
36 while(temp!=NULL){
37 //print values
38 printf("Name:%s\n",temp->name);
39 printf("Number:%d\n",temp->roll_number);
40 printf("\n");
41 //traverse the list
42 temp=temp->next;
43 }
44 }
45
46 void readline(char str[],int n){
47 char c=0;
48 int i=0;
49 //I used this function because scanf cannot
50 //read spaces.
51 if((c=getchar())!='\n')
52 str[i++]=c;
53 for(;i<n-1 && (c=getchar())!=EOF && c!='\n';++i)
54 str[i]=c;
55
56 str[i]='\0';
57
58 }
59
60
61 int main(){
62 int i=0;
63 char name[50];
64 int roll_no;
65 node *first=NULL;
66
67 for(i=0;i<5;i++){
68 printf("Enter Name:");
69 readline(name,50);
70 printf("Enter Roll No:");
71 scanf("%d",&roll_no);
72
73 first=addToBegining(first,name,roll_no);
74 }
75 printList(first);
76 }