Question
Linkes list progam in C won't save the student information entered. It only displays the last pieces of information entered by the user. Could someone
Linkes list progam in C won't save the student information entered. It only displays the last pieces of information entered by the user. Could someone please help?
Here is the code --
#include #include #include
typedef struct studentNode { char firstName[20]; char lastName[20]; unsigned int studID; char major[30]; double gpa; struct studentNode *nextPtr; } studentNode_t;
void insertNode(studentNode_t **ptrToHeadPtr, const studentNode_t *a, const studentNode_t *b, char lname[20] ); void printList(studentNode_t *headPtr);
int main(void) { int count = 0;
studentNode_t *tmp = NULL; tmp =(struct studentNode *)malloc(sizeof(studentNode_t));
do {
if (count == 0) { printf("Enter the first student's first name or -1 to end input: "); scanf("%s", tmp->firstName); } else { printf("Enter the next student's first name or -1 to end input: "); scanf("%s", tmp->firstName); } if (strcmp(tmp->firstName, "-1") != 0) {
printf("Enter the student's last name: "); scanf("%s", tmp->lastName); printf("Enter the student's ID number: "); scanf("%u", &(tmp->studID)); printf("Enter the student's major: "); scanf("%s", tmp->major); printf("Enter the student's gpa: "); scanf("%lf", &(tmp->gpa));
} count++; }while (strcmp(tmp->firstName, "-1") != 0); printList(tmp); return (0); }
void insertNode(studentNode_t **ptrToHeadPtr, const studentNode_t *a, const studentNode_t *b, char lname[20]) {
studentNode_t *newNodePtr = (studentNode_t *)malloc(sizeof(studentNode_t));
if (newNodePtr != NULL) { strcpy(newNodePtr->lastName, lname); // Place value in node
newNodePtr->nextPtr = NULL; // Node is not linked to another node
studentNode_t *prevPtr = NULL;
studentNode_t *currPtr = *ptrToHeadPtr;
while (currPtr != NULL && (strcmp(a->lastName, b->lastName)< 0)) {
prevPtr = currPtr;
currPtr = currPtr->nextPtr; }
if (strcmp(a->lastName,b->lastName) == 0) { while (currPtr != NULL && (strcmp(a->firstName, b->firstName) < 0)) {
prevPtr = currPtr;
currPtr = currPtr->nextPtr; }
}
if (NULL == prevPtr) // Inserting at the beginning of list { newNodePtr->nextPtr = *ptrToHeadPtr; *ptrToHeadPtr = newNodePtr; } else {
prevPtr->nextPtr = newNodePtr;
newNodePtr->nextPtr = currPtr; } }
return; } // End of insertNode function
void printList(studentNode_t *headPtr) { studentNode_t *tmp = headPtr; puts(" NAME OF STUDENT\t\t\tSTUDENT ID\tMAJOR\t\t\tGPA ");
while (tmp != NULL) { printf("%s %s\t\t\t%d\t\t%s\t\t\t%.2f ",tmp->firstName, tmp->lastName, tmp->studID, tmp->major, tmp->gpa); tmp = tmp -> nextPtr; }
return; }
**It should run and look just like this :
Enter the next student's first name or -1 to end input: Katherine
Enter the student's last name: Watson
Enter the student's ID number: 2016112345
Enter the student's major: Mechanical Engineering
Enter the student's gpa: 3.2
Enter the next student's first name or -1 to end input: Daniel
Enter the student's last name: Grimes
Enter the student's ID number: 2016412412
Enter the student's major: Computer Science
Enter the student's gpa: 2.87
Enter the next student's first name or -1 to end input: Trent
Enter the student's last name: Newsom
Enter the student's ID number: 2016521245
Enter the student's major: Mechanical Engineering
Enter the student's gpa: 3.3
Enter the next student's first name or -1 to end input: -1
NAME OF STUDENT STUDENT ID MAJOR GPA
Daniel Grimes 2016412412 Computer Science 2.87
Trent Newsom 2016521245 Mechanical Engineering 3.30
Katherine Watson 2016112345 Mechanical Engineering 3.20
I really just need help keeping all the data entered.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started