回复 10楼 phrankmoon
我只是想让更多人 想进来看看是什么东西
周末没上这个论坛。周一看到的,写了代码,没调好就有同学找我出去了,然后没完成。
今天完成了,也通过,不过你已经知道怎么做了。所以就这样了。
但是还是我的代码贴出来了,毕竟是我的成果:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int StuNum;
char StuName[20];
int CourseC;
int CourseMath;
int CourseEng;
int CourseLinearM;
struct node *pnext;
}node_t;
node_t *pcurr = NULL;
//输出结构体里的成员变量值
int PrintData(node_t *tmp) //参数是指针
{
node_t *tmp1 = tmp;
printf("tmp->StuNum = %d\n",tmp1->StuNum);
printf("tmp->StuName: %s\n",tmp1->StuName);
printf("tmp->CourseC = %d\n",tmp1->CourseC);
printf("tmp->CourseMath = %d\n",tmp1->CourseMath);
printf("tmp->CourseEng = %d\n",tmp1->CourseEng);
printf("tmp->CourseLinearM = %d\n",tmp1->CourseLinearM);
return 0;
}
//输出链表中每个结点的值
int PrintList(node_t *head)
{
node_t *pcurrent = head;
while(pcurrent != NULL)
{
if(PrintData(pcurrent))
{
printf("Error:In PrintList!\n");
return -1;
}
printf("\n");
pcurrent = pcurrent->pnext;
}
return 0;
}
//在链表中插入结点
node_t *insertList(node_t *head , node_t *tmp)
{
node_t *pnew = NULL;
pnew = (node_t *)malloc(sizeof(node_t));
if(NULL == pnew)
{
printf("Error: In insertlist,NULL == pnew!\n");
exit(-1);
}
pnew->StuNum = tmp->StuNum;
pnew->CourseC = tmp->CourseC;
pnew->CourseEng = tmp->CourseEng;
pnew->CourseMath = tmp->CourseMath;
pnew->CourseLinearM = tmp->CourseLinearM;
strcpy(pnew->StuName,tmp->StuName);
pnew->pnext = NULL;
if (NULL == head)
{
head = pnew;
}
else
{
pcurr->pnext = pnew;
}
pcurr = pnew;
return(head);
}
//从文件中获取数据,并且存入链表中
int ListGetData(FILE *fp , node_t *tmp, node_t **head) //必须是二级指针,不然在返回的时候head == NULL
//int ListGetData(FILE *fp, node_t *tmp, node_t *head)
{
int length = 0;
char str[128];
node_t *tmp1 = tmp;
memset(str, 0, sizeof(char)*128);
while(fgets(str,128,fp) != NULL)
{
printf("\n%s\n",str);
if(sscanf(str,"%1d %s %2d %2d %2d %2d",&tmp1->StuNum, tmp1->StuName, &tmp1->CourseC, &tmp1->CourseMath, &tmp1->CourseEng, &tmp1->CourseLinearM)!=6)
{
printf("Error: while in sscanf!\n");
return -1;
}
length = strlen(tmp1->StuName);
tmp1->StuName[length+1] = '\0';
tmp1->pnext = NULL;
printf("In ListGetData!\n");
if(PrintData(tmp1)) //输出经过sscanf函数之后得到的数据
{
printf("Error : In PrintData!\n");
return -1;
}
*head = insertList(*head, tmp);
// head = insertList( head, tmp);
if(NULL == *head)
// if(NULL == head)
{
printf("Error : In inserList,NULL==head!\n");
return -1;
}
}
return 0;
}
//删除链表,释放内存
int DeleteList(node_t *head)
{
node_t *pfree = NULL;
while(NULL != head)
{
pfree = head;
head = pfree->pnext;
free(pfree);
}
return 0;
}
int main()
{
FILE *fp = NULL;
node_t tmp;
char str[128];
node_t *head = NULL;
memset(str,0,sizeof(char)*128);
fp = fopen("E:\\test.txt","r");
if(fp == NULL)
{
printf("can't open file test.txt!\n");
exit(-1);
}
fgets(str,128,fp);
if(ListGetData(fp,&tmp,&head))
// if(ListGetData(fp,&tmp,head))
{
printf("Error : In GetData!\n");
return -1;
}
if(NULL == head)
{
printf("Error:In main , NULL ==head!\n");
return -1;
}
printf("\n\nIn main : After ListGetData:\n");
if(PrintList(head))
{
printf("Error : In main,while PrintList!\n");
return -1;
}
if(DeleteList( head))
{
printf("Error: In main,while DeleteList!\n");
return -1;
}
pcurr = NULL;
printf("Bye!\n");
return 0;
}