c语言 链表问题求解,链表为什么创建不了哪里有错? 30
#include<stdio.h>#include<stdlib.h>//usingnamespacestd;structnode{intdata;structnode*...
#include <stdio.h>
#include <stdlib.h>
//using namespace std;
struct node
{
int data;
struct node *next;
};
struct node *create();
//void show(struct node *head);
int main(){
struct node *head;
struct node *red;
head=create();
red=head;
printf("%d",head->data);
//show(head);
return 0;
}
struct node *create()
{
struct node *head=NULL;
struct node *ped=head;
struct node *ps;
int num;
//cout<<"please input some datas end with '\n'"<<endl;
//cin>>num;
while(1)
{
printf("please input some datas end with 0\n");
scanf("%d",&num);
if(num==0)
break;
ps=(struct node*)malloc(sizeof(struct node));// ps=struct node;
ps->data=num;
ps->next=NULL;
if(head==NULL)
head->next=ps;
else
{
ped->next=ps;
}
ped=ps;
}
return head;
}
有点乱,求告知链表创建函数的错误就好,,,,其实只要答这个就好head->next=ps改成head=ps 展开
#include <stdlib.h>
//using namespace std;
struct node
{
int data;
struct node *next;
};
struct node *create();
//void show(struct node *head);
int main(){
struct node *head;
struct node *red;
head=create();
red=head;
printf("%d",head->data);
//show(head);
return 0;
}
struct node *create()
{
struct node *head=NULL;
struct node *ped=head;
struct node *ps;
int num;
//cout<<"please input some datas end with '\n'"<<endl;
//cin>>num;
while(1)
{
printf("please input some datas end with 0\n");
scanf("%d",&num);
if(num==0)
break;
ps=(struct node*)malloc(sizeof(struct node));// ps=struct node;
ps->data=num;
ps->next=NULL;
if(head==NULL)
head->next=ps;
else
{
ped->next=ps;
}
ped=ps;
}
return head;
}
有点乱,求告知链表创建函数的错误就好,,,,其实只要答这个就好head->next=ps改成head=ps 展开
展开全部
#include <stdio.h>
#include <stdlib.h>
//using namespace std;
struct node
{
int data;
struct node *next;
};
struct node *create();
//void show(struct node *head);
int main(){
struct node *head;
struct node *red;
head=create();
red=head;
printf("%d",head->next->data);//输出的只是头指针指向的内存的下一个地址里面的内存数据内容//(即为第一个ps里的数据)
//show(head);
return 0;
}
struct node *create()
{
struct node *head=(struct node*)malloc(sizeof(struct node));//您没有为head申请内存
struct node *ped=head;
struct node *ps;
int num;
head->next=NULL;
//cout<<"please input some datas end with '\n'"<<endl;
//cin>>num;
while(1)
{
printf("please input some datas end with 0\n");
scanf("%d",&num);
if(num==0)
break;
ps=(struct node*)malloc(sizeof(struct node));// ps=struct node;
ps->data=num;
ps->next=NULL;
if(head->next==NULL)
head->next=ps;//这边应该这样改
else
{
ped->next=ps;
}
ped=ps;
}
return head;
}
#include <stdlib.h>
//using namespace std;
struct node
{
int data;
struct node *next;
};
struct node *create();
//void show(struct node *head);
int main(){
struct node *head;
struct node *red;
head=create();
red=head;
printf("%d",head->next->data);//输出的只是头指针指向的内存的下一个地址里面的内存数据内容//(即为第一个ps里的数据)
//show(head);
return 0;
}
struct node *create()
{
struct node *head=(struct node*)malloc(sizeof(struct node));//您没有为head申请内存
struct node *ped=head;
struct node *ps;
int num;
head->next=NULL;
//cout<<"please input some datas end with '\n'"<<endl;
//cin>>num;
while(1)
{
printf("please input some datas end with 0\n");
scanf("%d",&num);
if(num==0)
break;
ps=(struct node*)malloc(sizeof(struct node));// ps=struct node;
ps->data=num;
ps->next=NULL;
if(head->next==NULL)
head->next=ps;//这边应该这样改
else
{
ped->next=ps;
}
ped=ps;
}
return head;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *create();
void show( struct node *head )
{
struct node *p=head;
while( p )
{
printf("%d->", p->data );
p=p->next ;
}
printf("NULL\n");
}
int main(){
struct node *head;
head=create();
show(head);
return 0;
}
struct node *create()
{
struct node *head=NULL;
struct node *ped;
struct node *ps;
int num;
while(1)
{
printf("please input some datas end with 0\n");
scanf("%d",&num);
if(num==0)
break;
ps=(struct node*)malloc(sizeof(struct node));
ps->data=num;
ps->next=NULL;
if(head==NULL)
head=ps;
else
ped->next=ps;
ped=ps;
}
return head;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if(head==NULL)
{
head=ps;
ped=ps;
}
试一下,没运行过
{
head=ps;
ped=ps;
}
试一下,没运行过
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询