C语言链表问题

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void establish();
void eliminate();
void search();
void show();

int total=0; /*总人数*/

struct person
{ /*结点的数据结构*/
long ID; /*准考证号*/
float fen; /*考分*/
struct person* next; /*指向下一个结点*/
};
struct person Head={0L,0.0,NULL};//赋初值
struct person*p,*q,*old;//声明结构体指针型p和q
void bubble(struct person*);//声明函数,函数参数为结构体指针型person
long ID; float fen;int roop;long wuy;

void main()
{
while(1)
{
int menu;
system("cls");
printf("- 1 创建链表增加节点 -\n");
printf("- 2 删除节点 -\n");
printf("- 3 改正节点 -\n");
printf("- 4 查找节点 -\n");
printf("- 5 链表排序 -\n");
printf("- 6 输出链表 -\n");
printf("- 7 退出系统 -\n");
printf("- It is %d data in this program~",total);
scanf("%d",&menu);
switch(menu)
{
case 1: establish(); break;//创建链表
case 2: eliminate(); break;//删除节点
case 3: ; break;//改正节点
case 4: search(); break;//查找节点
case 5: bubble(&Head); break;//链表排序
case 6: show(); break;//输出链表信息
case 7: exit(0); break;//退出系统
}
}
}
void establish()
{ int stunum;
q=&Head;//结构体指针q指向head
printf("how many student took the exam?");
scanf("%d",&stunum);
if(stunum<1||stunum>50)
{
printf("please input correct number!,please try again");
scanf("%d",&stunum);
}
for(;stunum!=0;stunum--)
{
printf("No.%d: ID,score=",total+1);
scanf("%ld%*c%f",&ID,&fen);
p=(struct person*)malloc(sizeof(struct person));
q->next=p;
p->next=NULL;
p->ID=ID;
p->fen=fen;
total++;
q=p;
}
printf("the information you entered has been confirmed");
system("pause");
}
这里只写了第一个输入函数,问题在于调用一次函数之后第二次开始就会丢失第一次调用输入函数输入的数据,从第二次调用的时候输入的数据开始输出,求逻辑,求解决方法。。。

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void establish();
void eliminate();
void search();
void show();

int total = 0; /*总人数*/

struct person { /*结点的数据结构*/
long ID; /*准考证号*/
float fen; /*考分*/
struct person* next; /*指向下一个结点*/
};

struct person Head = {0L,0.0,NULL};//赋初值
struct person *p,*q,*old;//声明结构体指针型p和q
void bubble(struct person*);//声明函数,函数参数为结构体指针型person
long ID;
float fen;
int roop;
long wuy;

void main() {
while(1) {
int menu;
system("cls");
printf("- 1 创建链表增加节点 -\n");
printf("- 2 删除节点 -\n");
printf("- 3 改正节点 -\n");
printf("- 4 查找节点 -\n");
printf("- 5 链表排序 -\n");
printf("- 6 输出链表 -\n");
printf("- 7 退出系统 -\n");
printf("- It is %d data in this program~",total);
scanf("%d",&menu);
switch(menu) {
case 1: establish(); break;//创建链表
case 2: eliminate(); break;//删除节点
case 3: ; break;//改正节点
case 4: search(); break;//查找节点
case 5: bubble(&Head); break;//链表排序
case 6: show(); break;//输出链表信息
case 7: exit(0); break;//退出系统
}
}
}

void establish() {
int stunum;
if(Head->next != NULL) { // 增加一段判断语句
q = Head->next;
while(q != NULL) q = q->next; // 移动指针到链表末尾
}
else q = &Head;//结构体指针q指向head
printf("how many student took the exam : ");
scanf("%d",&stunum);
if(stunum < 1 || stunum > 50) {
printf("please input correct number!,please try again : ");
scanf("%d",&stunum);
}
for(;stunum != 0;stunum--) {
printf("No.%d: ID,score = ",total + 1);
scanf("%ld%,%f",&ID,&fen);
p = (struct person*)malloc(sizeof(struct person));
q->next = p;
p->next = NULL;
p->ID = ID;
p->fen = fen;
total++;
q = p;
}
printf("the information you entered has been confirmed");
system("pause");
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-02-13
这个问题这么久了,还没有解决啊,系统把它推荐给我了,那我就继续给它回答吧,说一下我的理解,你是想让这个输入函数在第二次调用时可以继续追加数据,而不是完全的新建表?
我按自己的理解,改造了你的这个函数,代码如下:
void establish()
{ int stunum;
q=&Head;//结构体指针q指向head
printf("how many student took the exam?");
//scanf("%d",&stunum);
//if(stunum<1||stunum>50)
//{
// printf("please input correct number!,please try again");
// scanf("%d",&stunum);
//}//注释掉的这一段用下面的do while循环替代,保证输入有效的数据 这段只是优化,不是必需改的
do
{
int flag = 0;
if(flag)
{
printf("please input correct number!,please try again");
fflush(stdin);//如果输入有误,让重输入时清除键盘输入缓存区,确保开始重新输入!
}
scanf("%d",&stunum);
flag = 1;
}while(stunum<1||stunum>50);

if(q->next)while(q->next)q=q->next; //<<<***添加了这一行,以便保留之前输入的数据
//你想要的可能就是上面这一行,
//要求在删除函数中,删空时,要保证置Head.next=NULL,否则程序有崩溃的危险

for(;stunum!=0;stunum--)
{
printf("No.%d: ID,score=",total+1);
scanf("%ld%*c%f",&ID,&fen);
p=(struct person*)malloc(sizeof(struct person));
q->next=p;
p->next=NULL;
p->ID=ID;
p->fen=fen;
total++;
q=p;
}
printf("the information you entered has been confirmed");
system("pause");
}
第2个回答  2012-02-09
struct person*p,*q,*old;//声明结构体指针型p和q
为什么一定要把它们声明为全局变量呢?
没有这个if(Head->next != NULL) { // 增加一段判断语句
q = Head->next;
while(q != NULL) q = q->next; // 移动指针到链表末尾
}
你的程序会采取覆盖添加节点,原来的节点当然会没啦。

我比较关心的是你这个程序怎么管理内存呢,你new出来的内存什么时候释放呢应该,只是把数据写进内存的话不太合理的吧应该。
相似回答