大家说下怎么把链表里面的数据放到一个数组里面

xsdzx4 2012-02-15 10:49:15
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
int nDate;
struct node *pstnext;
}Node;

//链表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
printf("分配内存失败\r\n");
return NULL;
}
head->pstnext = NULL;

p = head;
while(cycle)
{
printf("请输入数据且当输入数据为0时结束输入\r\n");
scanf("%d", &Date);
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
printf("分配内存失败\r\n");
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}

//链表输出
int output(Node *head)//获取数据元素个数n 为存放到数组做好准备
{
int n=0;
Node *p = head;
while(NULL != p)
{
n++;
printf("%d ", p->nDate);
p = p->pstnext;
}
printf("\r\n");
return n;
}
int *lookup(Node *head)//把链表的数据放到数组a[k]里面去
{
int k=output(head);
Node *p=head;
int a[k];
for(int i=0;i<k;i++)
{
a[i]=p->nDate;
p=p->pstnext;
}
return a;
}
int main()
{
int *a;

Node *head=creat();
int k=output(head);
//creat();
a=lookup(head);
for(int i=0;i<k;i++)
{
printf("%d ",a[i]);
}
return 0;

}
我的源代码 有错误如下 大家帮我看看
sd.c
C:\Documents and Settings\郑奇文\sd.c(67) : error C2057: expected constant expression
C:\Documents and Settings\郑奇文\sd.c(67) : error C2466: cannot allocate an array of constant size 0
C:\Documents and Settings\郑奇文\sd.c(67) : error C2133: 'a' : unknown size
C:\Documents and Settings\郑奇文\sd.c(68) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\郑奇文\sd.c(68) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\郑奇文\sd.c(68) : error C2143: syntax error : missing ')' before 'type'
C:\Documents and Settings\郑奇文\sd.c(68) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\郑奇文\sd.c(68) : error C2065: 'i' : undeclared identifier
C:\Documents and Settings\郑奇文\sd.c(68) : warning C4552: '<' : operator has no effect; expected operator with side-effect
C:\Documents and Settings\郑奇文\sd.c(68) : error C2059: syntax error : ')'
C:\Documents and Settings\郑奇文\sd.c(69) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\郑奇文\sd.c(73) : warning C4172: returning address of local variable or temporary
C:\Documents and Settings\郑奇文\sd.c(83) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\郑奇文\sd.c(83) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\郑奇文\sd.c(83) : error C2143: syntax error : missing ')' before 'type'
C:\Documents and Settings\郑奇文\sd.c(83) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\郑奇文\sd.c(83) : warning C4552: '<' : operator has no effect; expected operator with side-effect
C:\Documents and Settings\郑奇文\sd.c(83) : error C2059: syntax error : ')'
C:\Documents and Settings\郑奇文\sd.c(84) : error C2143: syntax error : missing ';' before '{'
执行 cl.exe 时出错.

sd.obj - 1 error(s), 0 warning(s)

...全文
1739 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
面包大师 2012-02-15
  • 打赏
  • 举报
回复
for(int i=0;i<k;i++)
在纯C中,不能这样用,我用的VS 2005的编译器,所以没发现。。。。要先定义,后使用即改成
int i;
for(i=0;i<k;i++)
DyanWang 2012-02-15
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
int nDate;
struct node *pstnext;
}Node;

//链表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
printf("分配内存失败\r\n");
return NULL;
}
head->pstnext = NULL;

p = head;
while(cycle)
{
printf("请输入数据且当输入数据为0时结束输入\r\n");
scanf("%d", &Date);
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
printf("分配内存失败\r\n");
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}

//链表输出
int output(Node *head)//获取数据元素个数n 为存放到数组做好准备
{
int n=0;
Node *p = head;
while(NULL != p)
{
n++;
printf("%d ", p->nDate);
p = p->pstnext;
}
printf("\r\n");
return n;
}
int *lookup(Node *head)//把链表的数据放到数组a[k]里面去
{
int k=output(head);
int i;
Node *p=head;
int *a = (int *)malloc(sizeof(int) * k);
for(i=0;i<k;i++)
{
a[i]=p->nDate;
p=p->pstnext;
}
return a;
}

int main()
{
int *a;
int i;

Node *head=creat();
int k=output(head);
//creat();
a=lookup(head);
for(i=0;i<k;i++)
{
printf("%d ",a[i]);
}
free(a);
free(head);

return 0;
}
justkk 2012-02-15
  • 打赏
  • 举报
回复
for(int i=0;i<k;i++)
像这种都是c++的风格,68行、84行一样

你可以先定义变量i,然后在for循环中使用
xsdzx4 2012-02-15
  • 打赏
  • 举报
回复
这是修改后的代码
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
int nDate;
struct node *pstnext;
}Node;

//链表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
printf("分配内存失败\r\n");
return NULL;
}
head->pstnext = NULL;

p = head;
while(cycle)
{
printf("请输入数据且当输入数据为0时结束输入\r\n");
scanf("%d", &Date);
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
printf("分配内存失败\r\n");
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}

//链表输出
int output(Node *head)//获取数据元素个数n 为存放到数组做好准备
{
int n=0;
Node *p = head;
while(NULL != p)
{
n++;
printf("%d ", p->nDate);
p = p->pstnext;
}
printf("\r\n");
return n;
}
int *lookup(Node *head)//把链表的数据放到数组a[k]里面去
{
int k=output(head);
Node *p=head;
int *a=(int *)malloc(k*sizeof(int));
for(int i=0;i<k;i++)
{
a[i]=p->nDate;
p=p->pstnext;
}
return a;
}
int main()
{
int *a;

Node *head=creat();
int k=output(head);
//creat();
a=lookup(head);
for(int i=0;i<k;i++)
{
printf("%d ",a[i]);
}
return 0;

}
面包大师 2012-02-15
  • 打赏
  • 举报
回复
先清除,再重新变一下。。。如果还出错,你把你改后的代码重新发一份,用[ code=C/C++][/code]这个括起来,好看些、、、
xsdzx4 2012-02-15
  • 打赏
  • 举报
回复
按你这样改了还是有错误!!
:\Documents and Settings\郑奇文\sd.c(68) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\郑奇文\sd.c(68) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\郑奇文\sd.c(68) : error C2143: syntax error : missing ')' before 'type'
C:\Documents and Settings\郑奇文\sd.c(68) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\郑奇文\sd.c(68) : error C2065: 'i' : undeclared identifier
C:\Documents and Settings\郑奇文\sd.c(68) : warning C4552: '<' : operator has no effect; expected operator with side-effect
C:\Documents and Settings\郑奇文\sd.c(68) : error C2059: syntax error : ')'
C:\Documents and Settings\郑奇文\sd.c(69) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\郑奇文\sd.c(83) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\郑奇文\sd.c(83) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\郑奇文\sd.c(83) : error C2143: syntax error : missing ')' before 'type'
C:\Documents and Settings\郑奇文\sd.c(83) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\郑奇文\sd.c(83) : warning C4552: '<' : operator has no effect; expected operator with side-effect
C:\Documents and Settings\郑奇文\sd.c(83) : error C2059: syntax error : ')'
C:\Documents and Settings\郑奇文\sd.c(84) : error C2143: syntax error : missing ';' before '{'
执行 cl.exe 时出错.
面包大师 2012-02-15
  • 打赏
  • 举报
回复
把上面那段代码改成我的那个样子就行,在数组长度未知或者要用变量当长度的时候,你要动态分配内存
面包大师 2012-02-15
  • 打赏
  • 举报
回复
int *lookup(Node *head)//把链表的数据放到数组a[k]里面去
{
int k=output(head);
Node *p=head;
int *a = (int *)malloc(k*sizeof(int));
for(int i=0;i<k;i++)
{
a[i]=p->nDate;
p=p->pstnext;
}
return a;
}
justkk 2012-02-15
  • 打赏
  • 举报
回复
int k=output(head);
Node *p=head;
int a[k];

不能采用c的方式编译,用C++的方式是可以编译通过的
另外,lookup()函数的最后 return a 是不对的,因为a是一个局部变量,在函数返回后其地址不再有效

建议把a定义为一个指针,动态分配空间
xsdzx4 2012-02-15
  • 打赏
  • 举报
回复
恩 牛逼啊!

70,021

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧