C语言链表输出问题,为什么会无故多出很多不相干的数据呢?还有我这个程序为什么不可以按姓名查找呢?按专业也不可以,但是按学号可以,不知道
#include "stdio.h "
#include "stdlib.h "
#include "string.h "
struct student
{
int no; /*学号*/
char name[10]; /*姓名*/
char acdamic[20]; /*院系*/
char major[20]; /*专业*/
char province[20]; /*籍贯*/
char address[40]; /*家庭地址*/
long phone; /*联系电话*/
struct student *next;
};
struct student *input();
void print(struct student *h);
struct student *insert(struct student *h);
struct student *del(struct student *h);
void find4(struct student *h);
void find3(struct student *h);
void find2(struct student *h);
void find1(struct student *h);
struct student *head=NULL;
char ch,*menu[]={ "------------------通讯录菜单------------ ",
"1.----------建立学生通讯录--------------- ",
"2.----------输出全部学生通讯录--------------- ",
"3.----------增加的学生的个数--------------- ",
"4.----------删除指定学号的学生-------------- ",
"5.----------按系别查找学生信息------------- ",
"6.----------按专业查找学生信息------------- ",
"7.----------按姓名查找学生信息------------- ",
"8.----------按学号查找学生信息-------------- ",
"9.----------退出通讯录----------------------- "};
struct student *input() /*输入函数*/
{
int n,i;
struct student *h=NULL,*p,*q;
printf( "请输入你要建立的学生通讯录的学生个数:\n ");
scanf( "%d ",&n);
for(i=1;i <=n;i++)
{
printf( "请依次输入第%d个学生的学号,姓名,系别,专业,籍贯,家庭住址和联系电话:\n ",i);
p=(struct student *)malloc(sizeof(struct student));
if(p==NULL)
{
printf( "内存不足!\n ");
exit(0);
}
scanf( "%d,%s,%s,%s,%s,%s,%ld ",&p-> no,p-> name,p-> acdamic,p-> major,p-> province,p-> address,&p-> phone);
if(i==1)h=p;
else q-> next=p;
q=p;
}
q-> next=NULL;
return h;
}
void print(struct student *h) /*输出函数*/
{
struct student *p=h;
while(p)
{
printf( "学生信息:\n%d,%s,%s,%s,%s,%s,%ld\n ",p-> no,p-> name,p-> acdamic,p-> major,p-> province,p-> address,p-> phone);
p=p-> next;
}
}
struct student *insert(struct student *h) /*增加学生信息*/
{
int n,i;
struct student *p,*r;
printf( "请输入你要增加的学生的个数:\n ");
scanf( "%d ",&n);
for(i=1;i <=n;i++)
{
r=(struct student *)malloc(sizeof(struct student));
printf( "请输入第%d个你要插入的学生的信息:\n ",i);
scanf( "%d,%s,%s,%s,%s,%s,%ld ",&r-> no,r-> name,r-> acdamic,r-> major,r-> province,r-> address,&r-> phone);
p=h;
h=r;
r-> next=p;
}
return h;
}
struct student *del(struct student *h) /*按学号进行删除*/
{
int n;
struct student *p,*q;
printf( "请输入要删除的学生的学号:\n ");
scanf( "%d ",&n);
p=h;
while(p-> no!=n&&p-> next)
{q=p;p=p-> next;}
if(p-> no==n)
{
if(h==p)h=p-> next;
else q-> next=p-> next;
}
else printf( "%d is not found!\n ");
return h;
}
void find1(struct student *h) /*按院系查找学生信息*/
{
char s[40];
struct student *p;
printf( "请输入要查找的学生的院系:\n ");
scanf( "%s ",s);
if(h==NULL)printf( "empty list!\n ");
else
{
p=h;
while(p)
{
if(strcmp(p-> acdamic,s)&&p)
printf( "学生信息:\n%d %s %s %s %s %s %ld\n ",p-> no,p-> name,p-> acdamic,p-> major,p-> province,p-> address,p-> phone);
p=p-> next;
}
}
}
void find2(struct student *h) /*按专业查找*/
{
char s[20];
struct student *p;
printf( "请输入要查找的学生的专业名:\n ");
scanf( "%s ",s);
if(h==NULL)printf( "empty list!\n ");
else
{
p=h;
while(p)
{
if(strcmp(p-> major,s)&&p)
printf( "学生信息:\n%d %s %s %s %s %s %ld\n ",p-> no,p-> name,p-> acdamic,p-> major,p-> province,p-> address,p-> phone);
p=p-> next;
}
}
}
void find3(struct student *h) /*按姓名查找*/
{
char ss[10];
struct student *p;
printf( "请输入要查找的学生的姓名:\n ");
scanf( "%s ",ss);
if(h==NULL)printf( "empty list!\n ");
else
{
p=h;
while(p)
{
if(strcmp(p-> name,ss)&&p)
printf( "学生信息:\n%d %s %s %s %s %s %ld\n ",p-> no,p-> name,p-> acdamic,p-> major,p-> province,p-> address,p-> phone);
p=p-> next;
}
}
}
void find4(struct student *h) /*按学号查找*/
{
int num;
struct student *p;
printf( "请输入要查找的学生的学号:\n ");
scanf( "%d ",&num);
if(h==NULL)printf( "empty list!\n ");
else
{
p=h;
while(p)
{
if(p-> no==num)
printf( "学生信息:\n%d %s %s %s %s %s %ld\n ",p-> no,p-> name,p-> acdamic,p-> major,p-> province,p-> address,p-> phone);
p=p-> next;
}
}
}
int menu_select()
{
int i,s;
char c[3];
for(i=0;i <10;i++)
printf( "%s\n ",menu[i]);
do
{
scanf( "%s ",c);
s=atoi(c);
}while(s <0||s> 9);
return s;
}
main()
{
for(;;)
{
switch(menu_select())
{
case 1:head=input();break;
case 2:print(head);break;
case 3:head=insert(head);break;
case 4:head=del(head);break;
case 5:find1(head);break;
case 6:find2(head);break;
case 7:find3(head);break;
case 8:find4(head);break;
case 9:exit(0);
}
}
}
[解决办法]
C语言链表输出问题,为什么会无故多出很多不相干的数据呢?还有我这个程序为什么不可以按姓名查找呢?按专业也不可以,但是按学号可以,不知道为什么,大虾帮忙啊.
--------------------------------------
多出的不相关的数据是什么数据?
char name[10]; /*姓名*/
char acdamic[20]; /*院系*/
char major[20]; /*专业*/
char province[20]; /*籍贯*/
char address[40]; /*家庭地址*/
这里这么多字符数组,最可能的情况是字符数组赋值时超过其所容量,而导致字符数组访问时越界,而产生一些不相关的数据, 再仔细检查一下其容量是否够.
对于查找:比如错误发生在:
void find3(struct student *h) /*按姓名查找*/
{
char ss[10];
struct student *p;
printf( "请输入要查找的学生的姓名:\n ");
scanf( "%s ",ss);
if(h==NULL)printf( "empty list!\n "); // 这里最好修改为if (NULL== h){
printf( "empty list!\n "); }// 加一对{}
else
{
p=h;
while(p)
{
if(strcmp(p-> name,ss)&&p) // 这里字符串比较使用错了方法
// 两个字符串相比,如果相等,返回的值是0,而且那个&&p也没有必要,因为在while循环中,已经确保p是非空指针了,修改为if (strcmp(p-> name, ss) == 0)则可以.
printf( "学生信息:\n%d %s %s %s %s %s %ld\n ",p-> no,p-> name,p-> acdamic,p-> major,p-> province,p-> address,p-> phone);
p=p-> next;
}
}
}
没查找对,主要是你对两个字符串比较的返回值没注意到,如果他们相等,则返回0.