Program in C I have program that needs to store a students name roll(or ID#) and
ID: 3715895 • Letter: P
Question
Program in C I have program that needs to store a students name roll(or ID#) and precentage in a linked list. This is the basic outline for the structure i used. I just need to know how to store the data as a linked list and print out the information. #include <stdio.h> #include <stdlib.h> struct student { char name[20]; int roll; float perc; }; int main() { struct student *pstd; int n,i; printf("Enter total number of elements: "); scanf("%d",&n); pstd=(struct student*)malloc(n*sizeof(struct student)); for(i=0; i<n; i++) { printf(" Enter detail of student [%3d]: ",i+1); printf("Enter name: "); scanf(" "); /*clear input buffer*/ gets((pstd+i)->name); printf("Enter roll number: "); scanf("%d",&(pstd+i)->roll); printf("Enter percentage: "); scanf("%f",&(pstd+i)->perc); } printf(" Entered details are: "); for(i=0; i<n; i++) { printf("%30s %5d %.2f ",(pstd+i)->name,(pstd+i)->roll,(pstd+i)->perc); } return 0; }
Explanation / Answer
Main.c
#include <stdio.h>
#include <stdlib.h>
struct student
{
char name[20];
int roll;
float perc;
struct student *next; // pointer to point to next student's information
};
int main()
{
struct student *temp, *current=NULL,*firstStudent=NULL;
int n,i;
// Total number of students is entered by user as input
printf("Enter total number of students: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
//allocate memory for storing one student information in the list
temp = (struct student *) malloc(sizeof(struct student));
printf(" Enter detail of student [%3d]: ",i+1);
printf("Enter name: ");
scanf(" "); /*clear input buffer*/
scanf("%s",&temp->name); //Name of the student entered as input by user
printf("Enter roll number: ");
scanf("%d", &temp->roll); // Roll number of student entered as input by user
printf("Enter percentage: ");
scanf("%f",&temp->perc); // percentage of marks obtained by student entered as input by user
temp->next = NULL; //Variable to hold a single student information, hence next pointer is made NULL
// if the list is empty, then make the firstStudent variable to point to newly created student above
if (firstStudent == NULL)
{
firstStudent = temp;
}
//if it is not empty, then traverse through the student's list till the end(where the next pointer is NULL)
else
{
current = firstStudent;
//traverse through the list till end
while (current->next != NULL)
current = current->next;
//add the newly created student to the end of the list
current->next = temp;
}
}
printf(" Entered details are: ");
current = firstStudent;
while(current != NULL)
{
printf("%30s %5d %.2f ",current->name,current->roll,current->perc);
current=current->next;
}
return 0;
}
Output:
Enter total number of students: 4
Enter detail of student [ 1]:
Enter name: john
Enter roll number: 20
Enter percentage: 70
Enter detail of student [ 2]:
Enter name: smith
Enter roll number: 40
Enter percentage: 80
Enter detail of student [ 3]:
Enter name: Robert
Enter roll number: 50
Enter percentage: 90
Enter detail of student [ 4]:
Enter name: cleve
Enter roll number: 60
Enter percentage: 100
Entered details are:
john 20 70.00
smith 40 80.00
Robert 50 90.00
cleve 60 100.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.