#include <stdio.h>
#include <stdlib.h>
#define N 10
typedef struct music_student *StudentPtr;
typedef struct music_student
{
int student_ID;
double hw_average;
int exam_grade;
int final_grade;
StudentPtr next;
} Music_student;
StudentPtr creatnew(int id,double hw,int exam);
StudentPtr Del(int id,StudentPtr list);
StudentPtr insert(int id,double hw,int exam,StudentPtr list);
void Proportion(float prop,StudentPtr list);
int main()
{
int req,id,exam;
double avg;
float prop;
StudentPtr list=NULL;
printf("Course Management Menu\nPlease choose among the following:\n");
printf("***\n");
printf("0: Quit.\n1: Insert student at first place on the list.\n2: Delete by ID.\n3: Find lucky student.\n4: Change homework exam ratio.\n5: Print List.\n");
while (N<15)
{
if(scanf("%d",&req)!=1)
{
printf("Input Error!");
return 1;
}
while(req>6||req<0)
{
printf("Invalid Selection\n");
if(scanf("%d",&req)!=1)
{
printf("Input Error!");
return 1;
}
}
switch (req)
{
case 1:
printf("insert student ID\n");
scanf("%d",&id);
while(id<10000||id>99999)
{
printf("Invalid student id try_again: value between 10,000 and 99,999\n");
scanf("%d",&id);
}
printf("insert hw grade\n");
scanf("%lf",&avg);
while(avg>100||avg<0)
{
printf("Invalid hw grade: value between 0 and 100\n");
scanf("%lf",&avg);
}
printf("insert exam grade\n");
scanf("%d",&exam);
while(exam>100||exam<0)
{
printf("Invalid exam grade: value between 0 and 100\n");
scanf("%d",&exam);
}
list=insert(id,avg,exam,list);
break;
case 2:
printf("Enter student to expunge from records:\n");
scanf("%d",&id);
Del(id,list);
break;
case 3:
Lucky(list);
break;
case 5:
lilPrint(list);
break;
case 0:
printf ("bye bye!");
return 0;
}
}
}
void lilPrint(StudentPtr list)
{
StudentPtr temp = list;
if(list==NULL)
return;
while (temp != NULL)
{
printf("Student Id: %d\nFinal Grade: %d\n",temp->student_ID,temp->final_grade);
temp = temp->next;
}
return;
}
StudentPtr Del(int id, StudentPtr list)
{
StudentPtr curr = list, prev = NULL;
if(curr->student_ID==id)
{
list=list->next;
free(curr);
curr=list;
}
while (curr != NULL)
{
if (curr->student_ID == id)
{
if (prev == NULL)
{
list = curr->next;
}
else
{
prev->next = curr->next;
}
free(curr);
return list;
}
prev = curr;
curr = curr->next;
}
return list;
}
StudentPtr insert(int id,double hw,int exam,StudentPtr list)
{
StudentPtr cell=creatnew(id,hw,exam);
if(cell==NULL)
{
free(list);
exit(1);
}
cell->next=list;
list=cell;
return list;
}
StudentPtr creatnew(int id,double hw,int exam)
{
StudentPtr cell=(StudentPtr)malloc(sizeof(Music_student));
if(cell == NULL)
{
printf("Allocation failed!");
return NULL;
}
cell->exam_grade=exam;
cell->student_ID=id;
cell->hw_average=hw;
cell->final_grade=(my_round(0.7*cell->exam_grade) my_round(cell->hw_average*0.3));
cell->next=NULL;
return cell;
}
im trying to enter 4 or 5 nodes in it and the use lilprint to print some info about the linked list. when i enter some nodes and then delete the firstnode(only delting the first one does this problem) the program doesnt delete it fully so it only delets the student_id in that node and returns the rest. so when i print it it prints normally but the student_id is trash value .
CodePudding user response:
This definition and using of N
with the magic number 10
along with another magic number 15
#define N 10
//...
while (N<15)
//...
makes your code unclear.
Also this typedef declaration for a pointer
typedef struct music_student *StudentPtr;
is a bad idea.
For example if you will write const StudentPtr
then it will be equivalent to struct music_student * const
instead of const struct music_student *
and the last type specification should be used in the function lilPrint
because within the function the list is not changed.
void lilPrint( const struct music_student *list);
And by the way you forgot to place the function declaration before main.
In the function insert
this statement
free(list);
does not make sense because it does not free all the allocated memory.
The function Del
can invoke undefined when the list is empty due to using a null pointer accessing memory as for example in this statement
if(curr->student_ID==id)
Also it seems the list should store nodes with unique student_ID
but the function Del
is designed such a way that it deletes at most two nodes with the same student_ID
due to the following while loop after the first if statement
if(curr->student_ID==id)
{
list=list->next;
free(curr);
curr=list;
}
while (curr != NULL)
{
if (curr->student_ID == id)
{
if (prev == NULL)
{
list = curr->next;
}
else
{
prev->next = curr->next;
}
free(curr);
return list;
}
//...
That is the function should delete either all nodes with the same student_ID
or only one node with the given id
.
And in main you forgot to assign the returned pointer to the pointer list
Del(id,list);
Instead you need to write
list = Del(id,list);