这个链表的删除函数有错,要怎么改啊??
时间:2008-06-12 13:11:18
来源:论坛整理 作者: 编辑:chinaitzhe
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student
{
int num;
int score;
struct student *next;
};
int N=1;
struct student *creat(void)
{
struct student *p1, *p2, *head;
p1=p2=(struct student *)malloc(LEN);
head=p1;
printf("the %d one:\n",N);
scanf("%d%d",&p1->num, &p1->score);
while(p1->num!=0)
{
N ;
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
printf("the %d one:\n",N);
scanf("%d%d",&p1->num, &p1->score);
}
p2->next=NULL;
free(p1);
free(p2);
return head;
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%d\t%d\n",p->num, p->score);
p=p->next;
}
}
void find(struct student *head, int num)
{
struct student *p;
p=head;
for(;p!=NULL;)
{
if(p->score==num)
break;
else
p=p->next;
}
if(p->score==num)
{
printf("%d\t%d\n",p->num, p->score);
}
else
{
printf("not found!!\n");
}
}
void find1(struct student *head)
{
int num;
printf("wanna find?? input the number you want first:\n");
scanf("%d",&num);
find(head, num);
}
struct student *del1(struct student *head)
{
int num;
struct student *pointer;
printf("wanna del?? input the number you want first:\n");
scanf("%d",&num);
pointer=del(head, num);
return pointer;
}
struct student *del(struct student *p, int num)
{
struct student *p1, *p2;
p1=p; /*p1前,p2后*/
for(;p1->num!=num && p1->next!=NULL;) /*先找*/
{
p2=p1;
p1=p1->next;
}
if(p1->score==num)
{
printf("del:%d\t%d\n",p1->num, p1->score);
p2->next=p1->next;
N--;
}
else
{
printf("not found!!\n");
}
return p;
}
int main(void)
{
struct student *p;
p=creat();
print(p);
getch();
find1(p);
getch();
del(p);
print(p);
getch();
return 0;
}
网友回复:struct student *del1(struct student *head)
{
int num;
struct student *pointer;
printf("wanna del?? input the number you want first:\n");
scanf("%d",&num);
pointer=del(head, num);
return pointer;
}
struct student *del(struct student *p, int num)
{
struct student *p1, *p2;
p1=p; /*p1前,p2后*/
for(;p1->num!=num && p1->next!=NULL;) /*先找*/
{
p2=p1;
p1=p1->next;
}
if(p1->score==num)
{
printf("del:%d\t%d\n",p1->num, p1->score);
p2->next=p1->next;
N--;
}
else
{
printf("not found!!\n");
}
return p;
}
换个位置
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <stdio.h> #include <stdlib.h> #define LEN sizeof(struct student) struct student { int num; int score; struct student *next; }; int N=1; struct student *creat(void) { struct student *p1, *p2, *head; p1=p2=(struct student *)malloc(LEN); head=p1; printf("the %d one:\n",N); scanf("%d%d",&p1->num, &p1->score); while(p1->num!=0) { N ; p2->next=p1; p2=p1; p1=(struct student *)malloc(LEN); printf("the %d one:\n",N); scanf("%d%d",&p1->num, &p1->score); } p2->next=NULL; free(p1); return head; } void print(struct student *head) { struct student *p; p=head; while(p!=NULL) { printf("%d\t%d\n",p->num, p->score); p=p->next; } } void find(struct student *head, int num) { struct student *p; p=head; for(;p!=NULL;) { if(p->score==num) break; else p=p->next; } if(p->score==num) { printf("%d\t%d\n",p->num, p->score); } else { printf("not found!!\n"); } } void find1(struct student *head) { int num; printf("wanna find?? input the number you want first:\n"); scanf("%d",&num); find(head, num); } struct student *del(struct student *p, int num) ; struct student *del1(struct student *head) { int num; struct student *pointer; printf("wanna del?? input the number you want first:\n"); scanf("%d",&num); pointer=del(head, num); return pointer; } struct student *del(struct student *p, int num) { struct student *p1, *p2; p1=p; /*p1前,p2后*/ for(;p1->num!=num && p1->next!=NULL;) /*先找*/ { p2=p1; p1=p1->next; } if(p1->score==num) { printf("del:%d\t%d\n",p1->num, p1->score); p2->next=p1->next; N--; } else { printf("not found!!\n"); } return p; } int main(void) { struct student *p; p=creat(); print(p); getch(); find1(p); getch(); del1(p); print(p); getch(); return 0; } the 1 one: 1 1 the 2 one: 2 2 the 3 one: 0 0 1 1 2 2 wanna find?? input the number you want first: 1 1 1 wanna del?? input the number you want first: 2 del:2 2 1 1 Press any key to continue PS:在create里还有bug的,嘻嘻,自己找一下
网友回复:发现飞雪也睡的很晚啊
网友回复:mm...
关键字:链表,删除,函数,有错,
上一篇:新手提问:关于字符串指针问题
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面