-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2-student-link-list.c
90 lines (75 loc) · 1.81 KB
/
2-student-link-list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Program to create a link list with the details - struct student, int roll no, char grade and display the roll numbers which have grade B
#include<stdio.h>
#include<stdlib.h>
struct student {
int rollNo;
char grade[2];
struct student *next;
};
void insertDetails(struct student **head, int roll) {
struct student *newNode = (struct student *)malloc(sizeof(struct student));
newNode -> rollNo = roll;
printf("Grade: ");
scanf("%s", &newNode -> grade);
printf("\n");
newNode -> next = NULL;
if(*head == NULL) {
*head = newNode;
} else {
struct student *temp = *head;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newNode;
}
}
void displayRoll(struct student *head){
struct student *temp = head;
printf("The roll numbers are: \n");
while(temp != NULL){
printf(" %d | ", temp -> rollNo);
temp = temp -> next;
}
}
void displayGradeB(struct student *head){
struct student *temp = head;
while (temp != NULL){
// if((*temp -> grade) == 'B') // this works
if(*temp -> grade == 66) // this also works where the ASCII code of B is 66
printf(" Roll No - %d, Grade - %s | ", temp -> rollNo, temp -> grade);
temp = temp -> next;
}
}
int main(){
int roll, i;
char grade[2];
struct student *head = NULL;
printf("Enter the roll numbers with their grade: \n");
for(i = 1; i <= 5; i++)
{
printf("%d Roll No: ", i);
scanf("%d", &roll);
insertDetails(&head, roll);
}
displayRoll(head);
printf("\n\nThe roll numbers with the grade B are: \n");
displayGradeB(head);
return 0;
}
// Output
/*
Enter the roll numbers with their grade:
1 Roll No: 1
Grade: B
2 Roll No: 2
Grade: A
3 Roll No: 3
Grade: C
4 Roll No: 4
Grade: B
5 Roll No: 5
Grade: D
The roll numbers are:
1 | 2 | 3 | 4 | 5 |
The roll numbers with the grade B are:
Roll No - 1, Grade - B | Roll No - 4, Grade - B |
*/