-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.c
More file actions
60 lines (53 loc) · 1.63 KB
/
Copy pathtempCodeRunnerFile.c
File metadata and controls
60 lines (53 loc) · 1.63 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define a structure for student record
struct Student {
int rollNo;
char name[50];
float marks;
struct Student* next; // Pointer to the next student
};
int main() {
// Dynamically allocate memory for 5 student records
struct Student* head = NULL;
struct Student* temp = NULL;
for (int i = 0; i < 5; i++) {
struct Student* newStudent = (struct Student*)malloc(sizeof(struct Student));
if (newStudent == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Input student details
printf("Enter Roll No. for student %d: ", i+1);
scanf("%d", &newStudent->rollNo);
printf("Enter Name for student %d: ", i+1);
scanf("%s", newStudent->name);
printf("Enter Marks for student %d: ", i+1);
scanf("%f", &newStudent->marks);
newStudent->next = NULL;
// Add student record to the list
if (head == NULL) {
head = newStudent;
temp = head;
} else {
temp->next = newStudent;
temp = newStudent;
}
}
// Print student records
printf("\nStudent Records:\n");
struct Student* current = head;
while (current != NULL) {
printf("Roll No.: %d, Name: %s, Marks: %.2f\n", current->rollNo, current->name, current->marks);
current = current->next;
}
// Free allocated memory
current = head;
while (current != NULL) {
struct Student* nextStudent = current->next;
free(current);
current = nextStudent;
}
return 0;
}