学生成绩管理c语言编程实现。解决方法
学生成绩管理c语言编程实现。
请问哪里出错了。
# include <stdio.h>
# include <malloc.h>
struct node
{
int no;
int sroce;
char name[10];
struct node * next;
};
// 建立链表。
struct node * list()
{
int n, i, no;
char name[10] = {0};
struct node *tail, *p, *h = NULL;
printf("请问要输入几个学生!\n");
scanf("%d",&n);
printf("please input data:\n");
tail = h;
for (i=0; i<n; i++)
{
p = (struct node *) malloc (sizeof(struct node));
printf("no : ");
scanf("%d",&p->no);
fflush(stdin);
printf("name : ");
gets(p->name);
printf("sroce : ");
scanf("%d",&p->sroce);
//fflush(stdin);
if (i == 0)
h = tail = p;
else
tail->next = p;
tail = p;
p->next = NULL;
}
return h;
}
//输出链表
void print(struct node * q)
{
while (q != NULL)
{
printf("%d\t%s\t%d\n",q->no, q->name, q->sroce);
q = q->next;
}
}
//查到链表
void find(struct node *q)
{
int no;
struct node *p;
printf("please input find no: ");
scanf("%d",&no);
p = q;
while (p != NULL && p->no != no)
p = p->next;
if (q != NULL)
printf("%d\t%s\t%d\n",q->no, q->name, q->sroce);
else
printf("error input.");
}
//插入链表
struct node * inster(struct node *head)
{
int n, j;
struct node * p, * q;
printf("add input n (n>0)");
scanf("%d",&n);
p = (struct node *)malloc (sizeof(struct node *));
printf("input no : ");
scanf("%d",&p->no);
fflush(stdin);
printf("input name : ");
gets(p->name);
printf("input sroce : ");
scanf("%d",&p->sroce);
if (n == 0)
{
p->next = head;
head = p;
}
else
{
q = head;
for (j=1; j<n; j++)
q= q->next;
if (q != NULL)
{
p->next = q->next;
q->next = p;
}
else
printf("eorr input\n");
}
return (head);
}
//删除链表。
struct node * deleted(struct node *head)
{
int n, j;
struct node * p, * q;
printf("deleted input n (n>0)");
scanf("%d",&n);
if (n == 1)
{
p = head;
head = head->next;
free(p);
}
else
{
q = head;
for (j=1; j<n-1; j++)
q= q->next;
if (q != NULL)
{
p = q->next;
q->next = p->next;
free(p);
}
else
printf("eorr input\n");
}
return (head);
}
//free链表
void fist (struct node * head)
{
struct node * p;
while (head != NULL)
{
p = head;
head = head->next;
free(p);
}
printf("nothing\n");
}
int main (void)
{
struct node *head;
head = list();
print(head);
find(head);
head = inster(head);
print(head);
head = deleted(head); //这步以前都还是行的,一执行到这里,就会报错。
print(head);
fist(head);
return 0;
}
------解决方案--------------------
1.inster函数中动态分配是不对的
p = (struct node *)malloc (sizeof(struct node *));//是创建指针不是数据
应该改成 p = (struct node *)malloc (sizeof(struct node));
2.deleted函数中删除数据
q->next = p->next;//没有判断牌p是否是空
应该改成
if(p)
请问哪里出错了。
# include <stdio.h>
# include <malloc.h>
struct node
{
int no;
int sroce;
char name[10];
struct node * next;
};
// 建立链表。
struct node * list()
{
int n, i, no;
char name[10] = {0};
struct node *tail, *p, *h = NULL;
printf("请问要输入几个学生!\n");
scanf("%d",&n);
printf("please input data:\n");
tail = h;
for (i=0; i<n; i++)
{
p = (struct node *) malloc (sizeof(struct node));
printf("no : ");
scanf("%d",&p->no);
fflush(stdin);
printf("name : ");
gets(p->name);
printf("sroce : ");
scanf("%d",&p->sroce);
//fflush(stdin);
if (i == 0)
h = tail = p;
else
tail->next = p;
tail = p;
p->next = NULL;
}
return h;
}
//输出链表
void print(struct node * q)
{
while (q != NULL)
{
printf("%d\t%s\t%d\n",q->no, q->name, q->sroce);
q = q->next;
}
}
//查到链表
void find(struct node *q)
{
int no;
struct node *p;
printf("please input find no: ");
scanf("%d",&no);
p = q;
while (p != NULL && p->no != no)
p = p->next;
if (q != NULL)
printf("%d\t%s\t%d\n",q->no, q->name, q->sroce);
else
printf("error input.");
}
//插入链表
struct node * inster(struct node *head)
{
int n, j;
struct node * p, * q;
printf("add input n (n>0)");
scanf("%d",&n);
p = (struct node *)malloc (sizeof(struct node *));
printf("input no : ");
scanf("%d",&p->no);
fflush(stdin);
printf("input name : ");
gets(p->name);
printf("input sroce : ");
scanf("%d",&p->sroce);
if (n == 0)
{
p->next = head;
head = p;
}
else
{
q = head;
for (j=1; j<n; j++)
q= q->next;
if (q != NULL)
{
p->next = q->next;
q->next = p;
}
else
printf("eorr input\n");
}
return (head);
}
//删除链表。
struct node * deleted(struct node *head)
{
int n, j;
struct node * p, * q;
printf("deleted input n (n>0)");
scanf("%d",&n);
if (n == 1)
{
p = head;
head = head->next;
free(p);
}
else
{
q = head;
for (j=1; j<n-1; j++)
q= q->next;
if (q != NULL)
{
p = q->next;
q->next = p->next;
free(p);
}
else
printf("eorr input\n");
}
return (head);
}
//free链表
void fist (struct node * head)
{
struct node * p;
while (head != NULL)
{
p = head;
head = head->next;
free(p);
}
printf("nothing\n");
}
int main (void)
{
struct node *head;
head = list();
print(head);
find(head);
head = inster(head);
print(head);
head = deleted(head); //这步以前都还是行的,一执行到这里,就会报错。
print(head);
fist(head);
return 0;
}
------解决方案--------------------
1.inster函数中动态分配是不对的
p = (struct node *)malloc (sizeof(struct node *));//是创建指针不是数据
应该改成 p = (struct node *)malloc (sizeof(struct node));
2.deleted函数中删除数据
q->next = p->next;//没有判断牌p是否是空
应该改成
if(p)