问题描述
- 总是出现这类问题无法解决,求助
-
#include
#include
#include
#include
#include
int Student=0;//总人数
int N;
int Mark[100];
int Mark1[100];
struct student *Head;
struct student
{
char Name[20];
char Snum[20];
char Sex[4];
};
typedef struct node
{
char name[20];
char snum[20];
char sex[4];
struct node *pnext;
}node,*pnode;//类型声明
struct student *GetData();//文件导入
pnode create_list();//创建链表
void traverse_list(pnode phead);//遍历链表输入
int length_list(pnode phead);//链表长度
void sort_list(pnode phead);//学号正排序
void inverse_list(pnode phead);//学号反排序
void delete_list(pnode phead);//链表删除
int insert_list(pnode phead);//链表插入
void main()
{
srand(time(NULL));//随机函数初始化
pnode head=NULL;//建空链表
head=create_list();//链表头指针地址返回head
printf("n");
traverse_list(head);//原链表正排序
sort_list(head);
printf("1.按照学号大小正排序后:n");
traverse_list(head);
int pos;
pos=rand()%N+1;////随即删除
delete_list(head,pos);
printf("2.系统随机删除的同学第%d个同学nn删除后按学号反序输出",pos);
inverse_list(head);//反排序
traverse_list(head);
printf("3.系统已从剩余学生中抽取了一位nn");
printf("插入该生序号为%dnn",insert_list(head));
traverse_list(head);//遍历重新反排序链表输出
}
pnode create_list()//创建链表并随机产生N函数
{
int m=0,i,j,r;
Head=GetData();
printf("n");
for(i=0;i<Student;i++)
Mark[i]=i;
for(i=0;i<Student;i++)
{
r=rand()%(Student-i);
Mark1[m++]=Mark[r];
for(j=r;j<Student;j++)
Mark[j]=Mark[j+1];} m=0; pnode head=(pnode)malloc(sizeof(node)); pnode tail=head; tail->pnext=NULL; printf("请输入你想抽取学生人数N:"); scanf("%d",&N); for(i=0;i<N;i++) { pnode pnew=(pnode)malloc(sizeof(node));//申请空间存储节点 strcpy(pnew->name,Head[Mark1[m]].Name); strcpy(pnew->snum,Head[Mark1[m++]].Snum); strcpy(pnew->sex,Head[Mark1[m++]].Sex); tail->pnext=pnew;//末尾指向新元素 pnew->pnext=NULL; tail=pnew;//新元素地址变成新末尾 } return head;
}
void traverse_list(pnode phead)
{
pnode p=phead->pnext;//将头结点指针给指针p
printf("学生信息为:nn");
while(p!=NULL)
{
printf("t%st%st%st",p->name,p->snum,p->sex);
p=p->pnext;
}
printf("n");
}
int length_list(pnode phead)//链表长度
{
int len=0;
pnode p=phead->pnext;
while(p!=NULL)
{
len++;
p=p->pnext;
}
return len;
}
void sort_list(pnode phead)//冒泡法排序,按正排序复制
{
int i,j,len;
char name[20],num[20],sex[4];///
pnode q;
len=length_list(phead);
for(i=0;i
{
for(j=0,q=phead->pnext;jpnext,j++)if(strcmp(q->snum,q->pnext->snum)>0) { strcpy(name,q->name); strcpy(q->name,q->pnext->name); strcpy(q->pnext->name,name); strcpy(num,q->snum); strcpy(q->snum,q->pnext->snum); strcpy(q->pnext->snum,num); strcpy(sex,q->sex); strcpy(q->sex,q->pnext->sex); strcpy(q->pnext->sex,sex); } }
}
void inverse_list(pnode phead)
{
int i,j,len;
char name[20],num[20],sex[4];
pnode q;
len=length_list(phead);
for(i=len-1;i>0;i--)
{
for(j=len-1,q=phead->pnext;j>0;q=q->pnext,j--)
if(strcmp(q->snum,q->pnext->snum)
{
strcpy(name,q->name);
strcpy(q->name,q->pnext->name);
strcpy(q->pnext->name,name);strcpy(num,q->snum); strcpy(q->snum,q->pnext->snum); strcpy(q->pnext->snum,num); strcpy(sex,q->sex); strcpy(q->sex,q->pnext->sex); strcpy(q->pnext->sex,sex); } }
}
void delete_list(pnode phead,int pos)//删除元素
{pnode p=phead;
int i=0;
while(p->pnext!=NULL&&i
{
p=p->pnext;
i++;
}//寻找指定位置
pnode q=p->pnext;
p->pnext=p->pnext->pnext;
free(q);//释放空间
q=NULL;
}
int insert_list(pnode phead)
{
int m=rand()%(Student-N)+N;//产生N到student随机数
pnode p=phead;
pnode q=(pnode)malloc(sizeof(node));
strcpy(q->name,Head[Mark1[m]].Name);
strcpy(q->snum,Head[Mark1[m]].Snum);
strcpy(q->sex,Head[Mark1[m]].Sex);
q->pnext=p->pnext;
p->pnext=q;///
inverse_list(phead);
pnode pt=phead;
int location=0;
pnode s=(pnode)malloc(sizeof(node));
strcpy(s->snum,Head[Mark1[m]].Snum);
while(pt!=NULL)
{
pt=pt->pnext;
location++;
if(strcmp(s->snum,pt->snum)==0)
break;
}
return location;
}
struct student *GetData()//导入文件函数
{
int i;
struct student students[100];
FILE *fp;
fp=fopen("a.txt","r");
printf("导入学生数据:nn");
for(i=0;!feof(fp);i++)
{
fscanf(fp,"%s%s%s",students[i].Name,students[i].Snum,students[i].Sex);
printf("t%st%st%stn",students[i].Name,students[i].Snum,students[i].Sex);
}////
Student=i;
return students;
}
'delete_list' : function does not take 2 parameters
解决方案
void delete_list(pnode phead);//链表删除
这个函数定义修改为
void delete_list(pnode phead,int pos)