链表看错哪里了啊?
麻烦说下错哪里了.顺便改正一下.谢了.
#include <stdio.h>
#include <stdlib.h>
#define S struct worker
#define LEN sizeof(struct worker)
struct worker
{
int num;
char name[10];
int pay;
S *next;
};
/******删除函数******/
S *del(S *head,int n)
{
S *p,*q;
p=q=head;
while(n!=p-> num&&p-> next!=0)
{
q=p; p=p-> next;
}
if(n==p-> num)
{
if(p==head) head=p-> next;
else q-> next=p-> next;
}
free(p);
return head;
}
/******输入******/
S *input(void)
{
S *p,*q,*head;
p=q=head=(S*)malloc(LEN);
scanf( "%d ",&p-> num);
getch();
scanf( "%s ",p-> name);
getch();
scanf( "%d ",&p-> pay);
while(1)
{
p=(S*)malloc(LEN);
scanf( "%d ",&p-> num);
if(p-> num==0) break;
getch();
scanf( "%s ",p-> name);
getch();
scanf( "%d ",&p-> pay);
q=p;
q-> next=p;
}
return head;
}
/******输出******/
void out(S *p)
{
while(p!=0)
{
printf( "%d %s %d\n ",p-> num,p-> name,p-> pay);
p=p-> next;
}
}
/******主函数******/
main()
{
int n;
S *head;
head=input();
printf( "input n ");
scanf( "%d ",&n);
head=del(head,n);
out(head);
}
[解决办法]
给三个建议
1.链表的头节点不要用来存储数据
2 scanf( "%d ",&p-> num);改为scanf( "%d ",&(p-> num));其他类似
3.函数命名最好多加点内容
[解决办法]
好像没有getch(),应该是getchar()吧,
另,程序的用意不太明白
[解决办法]
把输入的 q=p; q-> next=p;顺序换一下 q-> next=p;q=p;就好拉