主函数里面的*ch类型应该定义成char
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NULL 0
#define LEN sizeof(struct student)
//定义节点结构
struct student
{
char no[7];
int score;
struct student *next;
};
//create()函数:创建一个具有头结点的单链表
//返回值:返回单链表的头指针
struct student *create(void)
{
struct student *head=NULL,*p1,*p2=NULL;
int count=0;
while(1)
{
p1=(struct student *)malloc(LEN);
printf("编号%d:",count+1);
scanf("%6s",p1->no);
if(strcmp(p1->no,"000000")==0) // 比较字符长度
{
free(p1);
break;
}
printf("成绩%d:",count+1);
scanf("%d",&p1->score);
count++;
p1->next=NULL;
if(count==1)
head=p1;
else
p2->next=p1;
p2=p1;
}
return(head);
}
void print(struct student *head)
{
struct student *p;
printf("学生们的成绩是:\n");
p=head;
while(p!=NULL)
{
printf("%s,%d\n",p->no,p->score);
p=p->next;
}
}
struct student *FindNode(struct student *head,int i)//依结点查找
{
struct student *p=head;
int j=1;
while(p->next!=NULL&&i>j)
{
p=p->next;
j++;
}
if(i==j)
return head;
else
return NULL;
}
struct student *Find(struct student *head,char *key)//依值查找
{
struct student *p=head;
while(p!=NULL)
{
if(key != p->no && p->no !=NULL) // 要找到相应的值,当然用strcmp比较长度是不能实现的,
//
p=p->next;
}
if(key == p->no)
strcpy(p->no, key);
return head;
}
void main()
{
char *ch;
struct student *pt;
pt=create();
print(pt);
printf("输入查找的值:");
scanf("%s",&ch); // scanf函数的使用
pt=Find(pt,ch);
printf("%s,%d\n",pt->no,pt->score);
}
唉,指针,内存很难掌握。但是更加要去用它。希望大家能继续努力找错更加多的错误
<div id="post_rate_div_2020679"> |