c语言链表问题:为什么输出不了 100
#include<stdio.h>#include<stdlib.h>structagency{intdata;structagency*next;};main(){in...
#include<stdio.h>
#include<stdlib.h>
struct agency{
int data;
struct agency *next;
};
main()
{
int x,i;
int v;
int num;
struct agency *p,*head,*p1,*p2,*p3;
int size=sizeof(struct agency);
head=NULL;
printf("X:");
scanf("%d",&x);
for(i=1;i<=x;i++){
p=(struct agency *)malloc(size);
scanf("%d",&p->data);
if(head==NULL){
head=p;
p1=head;
}
else {
p1->next=p;
p1=p;
}
}
p1->next=NULL;
p2=head;
printf("请输入要删除的元素:");
scanf("%d",&num);
while(p2!=NULL&&p2->data!=num){
p3=p2;
p2=p2->next;
}
if(p2!=NULL){//找到了元素
p3->next=p2->next;
free(p2);
for(p=head;p!=NULL;p=p->next)
printf("%d",p->data);
}
else printf("没找到\n");
} 展开
#include<stdlib.h>
struct agency{
int data;
struct agency *next;
};
main()
{
int x,i;
int v;
int num;
struct agency *p,*head,*p1,*p2,*p3;
int size=sizeof(struct agency);
head=NULL;
printf("X:");
scanf("%d",&x);
for(i=1;i<=x;i++){
p=(struct agency *)malloc(size);
scanf("%d",&p->data);
if(head==NULL){
head=p;
p1=head;
}
else {
p1->next=p;
p1=p;
}
}
p1->next=NULL;
p2=head;
printf("请输入要删除的元素:");
scanf("%d",&num);
while(p2!=NULL&&p2->data!=num){
p3=p2;
p2=p2->next;
}
if(p2!=NULL){//找到了元素
p3->next=p2->next;
free(p2);
for(p=head;p!=NULL;p=p->next)
printf("%d",p->data);
}
else printf("没找到\n");
} 展开
1个回答
展开全部
123456789101112131415161718192021222324252627在creat_linklist()函数里,返回的链表是llist。在for循环之前,llist=NULL;进入for循环中,当i=0时,让llist=q,但是q->info是没有赋过值的。当i>0时,q重新创建了一个Node,虽然你给q->info赋值了,但是q并没有追加到llist上去。 Linklist creat_linklist(){ int i; Pnode q = NULL; Pnode lastNode = NULL; //最后产生的node Linklist llist = NULL; for(i=0; i<10; i++) { //创建新Node q=(Pnode)malloc(sizeof(struct Node)); q->info=i; q->link = NULL; if(llist==NULL) llist=q; else lastNode->link = q; lastNode = q; } return llist;}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询