链接列表,新尝试(是:链接列表,没有输出,帮助) [英] Linked list, New try (was:Linked list, no out put,help)

查看:59
本文介绍了链接列表,新尝试(是:链接列表,没有输出,帮助)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的小组,


感谢所有以前帮助过的人。


现在我打印了一些价值。这个列表的价值是打印出来的吗?我是否真的污染了清单?


上一个主题链接(对不起谷歌)



#include< stdio.h>

#include< stdlib.h>

struct node

{

int data;

struct node * next;

};


struct node * add_node(struct node * p,int funct_data)

{

struct node * t = p;

/ *由值传递。所以我将参数值保存在一个

* temp变量中。我是否正确?* /


t = malloc(sizeof * t);

if(!t)

{

printf(" mem error");

退出(EXIT_FAILURE);

}

t-> data = funct_data;

返回t;

}

int main(无效)

{

struct node * p,* q;

int i;

q = malloc(sizeof * q);

if(!q)

退出(EXIT_FAILURE);

for(i = 0; i< 6; i ++)

{

q = add_node(p,i);

printf("%d \ n" ;,q->数据);

}

返回0;

}

Dear group,

Thanks for all those who helped previously.

Now I get some value printed. Is that the value of the list getting
printed? Am I
really polluted the list?

Previous thread link (sorry for google)
http://groups.google.com/group/comp....8ac16a2ed4ed35

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

struct node *add_node(struct node *p, int funct_data)
{
struct node *t = p;
/* passed by values. So I save the argument value in a
* temp variable.Am I correct?*/

t = malloc(sizeof *t);
if(!t)
{
printf("mem error");
exit(EXIT_FAILURE);
}
t->data = funct_data;
return t;
}

int main(void)
{
struct node *p,*q;
int i;
q=malloc(sizeof *q);
if(!q)
exit(EXIT_FAILURE);
for(i=0 ; i< 6 ; i++)
{
q = add_node(p, i);
printf("%d\n",q->data);
}
return 0;
}

推荐答案

傻瓜schrieb:
亲爱的小组,

感谢所有以前帮助过的人。
现在我打印了一些价值。打印清单的价值是什么?我真的污染了清单吗?


对不起,我不明白这个问题。

你真的没有列表,只有一组已分配的列表节点。

无法打印列表的值。

上一个主题链接(对不起谷歌)
http://groups.google.com/group/comp....8ac16a2ed4ed35

请将结果加起来。

#include< stdio.h>
#include< stdlib.h>
struct node
{
int data;
struct node * next;
};

struct node * add_node(struct node * p,int funct_data)
{
struct node * t = p;
/ *由值传递。所以我将参数值保存在一个
* temp变量中。我是否正确?* /


是的。请注意,不需要保存参数值。

t = malloc(sizeof * t);


现在你覆盖临时变量如果(!t)
{
printf(&mem error);


考虑将错误消息写入stderr而不是stdout。

退出(EXIT_FAILURE);
}
t-> data = funct_data;


你忘了链接t和p,例如

t-> next = p;

return t;
}

{struct struct * p,* q;
int i;
q = malloc(sizeof * q);


您为q赋值。

p未初始化。

如果(!q)
退出( EXIT_FAILURE);
for(i = 0; i< 6; i ++)
{
q = add_node(p,i);


你覆盖q的值并使用p uninitialised。

这意味着你有内存泄漏并且所有赌注都已关闭,

分别。

printf("%d \ n",q->数据);
}
返回0;
}
Dear group,

Thanks for all those who helped previously.

Now I get some value printed. Is that the value of the list getting
printed? Am I
really polluted the list?
Sorry, I do not understand the question.
You do not really have a list but a set of allocated list nodes.
The value of the list cannot be printed.

Previous thread link (sorry for google)
http://groups.google.com/group/comp....8ac16a2ed4ed35
Sum the results up, please.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

struct node *add_node(struct node *p, int funct_data)
{
struct node *t = p;
/* passed by values. So I save the argument value in a
* temp variable.Am I correct?*/
Yes. Note that the argument value does not need to be saved.

t = malloc(sizeof *t);
Now you overwrite the "temp variable" to which you saved p.
if(!t)
{
printf("mem error");
Consider writing error messages to stderr instead of stdout.
exit(EXIT_FAILURE);
}
t->data = funct_data;
You forgot to link t and p, e.g.
t->next = p;
return t;
}

int main(void)
{
struct node *p,*q;
int i;
q=malloc(sizeof *q);
You assign a value to q.
p is not initialised.
if(!q)
exit(EXIT_FAILURE);
for(i=0 ; i< 6 ; i++)
{
q = add_node(p, i);
You overwrite the value of q and use p uninitialised.
This means you have a memory leak and "all bets are off",
respectively.
printf("%d\n",q->data);
}
return 0;
}




考虑这个更正版本(未经过大量测试):


#include< stdio.h>

#include< stdlib.h>

struct node {

int data;

struct node * next;

};


struct node * prepend_node(struct node * pSuccessor,int new_data)

{

struct node * pNew = malloc(sizeof * pNew);

if(NULL == pNew)

{

fprintf(stderr," mem error \\ \\ n");

退出(EXIT_FAILURE);

}

pNew-> data = new_data;

pNew-> next = pSuccessor;


返回pNew;

}


void print_list(const struct节点* pFirst)

{

const struct node * pNode;


printf(" Contents:");

for (pNode = pFirst; NULL!= pNode; pNode = pNode-> next){

printf(" \t%d",pNode-> data);

}

printf(" \ n");

}


void free_list(struct node * pFirst)

{

struct node * pNode,* pNext;


if(pFirst){

for(pNode = pFirst; NULL!= pNode; pNode = pNext){

pNext = pNode-> next;

free(pNode);

}

}

}


int main(无效)

{

struct node * list = NULL;

int i;


for(i = 0; i< 6; i ++)

{

list = prepend_node(list,i);

print_list(list);

}

free_list(list);


返回0;

}

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



Consider this corrected version (not largely tested):

#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};

struct node *prepend_node (struct node *pSuccessor, int new_data)
{
struct node *pNew = malloc(sizeof *pNew);
if(NULL == pNew)
{
fprintf(stderr, "mem error\n");
exit(EXIT_FAILURE);
}
pNew->data = new_data;
pNew->next = pSuccessor;

return pNew;
}

void print_list (const struct node *pFirst)
{
const struct node *pNode;

printf("Contents:");
for (pNode = pFirst; NULL != pNode; pNode = pNode->next) {
printf("\t%d", pNode->data);
}
printf("\n");
}

void free_list (struct node *pFirst)
{
struct node *pNode, *pNext;

if (pFirst) {
for (pNode = pFirst; NULL != pNode; pNode = pNext) {
pNext = pNode->next;
free(pNode);
}
}
}

int main (void)
{
struct node *list = NULL;
int i;

for(i=0 ; i< 6 ; i++)
{
list = prepend_node(list, i);
print_list(list);
}
free_list(list);

return 0;
}
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


fool写道:
亲爱的小组,

感谢所有人我以前帮过过。

现在我得到了一些价值。打印清单的价值是什么?我是否真的污染了清单?

以前的主题链接(对不起google)
http://groups.google.com/group/comp....8ac16a2ed4ed35

#include< stdio.h>
#include< stdlib.h>
struct node
{
int data;
struct node * next;
};

struct node * add_node(struct node * p,int funct_data)
{struct node * t = p;


为什么这个作业? p只包含一个不确定的值。这将导致未定义的行为导致


/ *通过值传递。所以我将参数值保存在一个
* temp变量中。我是否正确?* /

t = malloc(sizeof * t);
if(!t)
{
printf(" mem error");


添加换行符或调用fflush(stdout)。否则,上述声明将无法显示在控制台上。

退出(EXIT_FAILURE);


你已经在main()中分配了内存。如果只是退出(),系统可能会丢失该内存

。你应该返回main()表示

错误,free()内存然后退出()。

}
t-> data = funct_data ;
返回t;
}
int main(void)
{struct node * p,* q;
int i;
q = malloc(sizeof * q);


add_node()分配struct node的实例并返回。这里

你已经设置q指向另一个struct node实例。然后在下面

你覆盖q以包含在

add_node()中分配的节点的地址,从而失去对原始节点的访问并导致

内存泄漏。


在main()或add_node()中分配结构,而不是在两者中分配。

if(!q)
exit(EXIT_FAILURE);


再次,你正在退出内存中的free()'。这对于

演示来说并不重要,但除非你现在养成了免费()的习惯,否则你将会在晚些时候搞砸b $ b。严肃的代码。

for(i = 0; i< 6; i ++)
{
q = add_node(p,i);


这里用你在add_node()中分配的一个

来覆盖上面分配的节点。删除main()中的分配,然后简单地让

add_node()这样做。


顺便说一下,因为add_node()分配了一个struct node的新副本,

每次调用它时你都要保存指针的副本

每次返回。否则你只是无法访问之前的

malloc()'''结构,再次是内存泄漏。

printf("%d \ nn,q- >数据);
}
返回0;
}
Dear group,

Thanks for all those who helped previously.

Now I get some value printed. Is that the value of the list getting
printed? Am I
really polluted the list?

Previous thread link (sorry for google)
http://groups.google.com/group/comp....8ac16a2ed4ed35

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

struct node *add_node(struct node *p, int funct_data)
{
struct node *t = p;
Why this assignment? p just contains an indeterminate value. This will
lead to undefined behaviour.
/* passed by values. So I save the argument value in a
* temp variable.Am I correct?*/

t = malloc(sizeof *t);
if(!t)
{
printf("mem error");
Add a newline or call fflush(stdout). Otherwise the above statement is
not garenteed to appear on the console.
exit(EXIT_FAILURE);
You''ve allocated memory in main(). If you simply exit(), that memory
might be lost to the system. You should return to main() indicating an
error, free() the memory and then exit().
}
t->data = funct_data;
return t;
}

int main(void)
{
struct node *p,*q;
int i;
q=malloc(sizeof *q);
add_node() allocates an instance of struct node and returns. Here
you''ve set q to point to another instance of struct node. Then below
you overwrite q to contain the address of the node allocated in
add_node(), thus losing access to the original node and causing a
memory leak.

Either allocate the structure in main() or in add_node(), not in both.
if(!q)
exit(EXIT_FAILURE);
Again you''re exiting with free()''ing the memory. This won''t matter for
demos, but unless you get into the habit of free()''ing now, you''ll
mess-up later on in serious code.
for(i=0 ; i< 6 ; i++)
{
q = add_node(p, i);
Here''s where you overwrite the node allocated above with the one
allocated in add_node(). Remove the allocation in main() and simply let
add_node() do it.

Incidentally, since add_node() allocates a fresh copy of struct node,
each time it''s called you''ll have to save a copy of the pointer it
returns each time. Otherwise you simply lose access to the previously
malloc()''ed structures, again a memory leak.
printf("%d\n",q->data);
}
return 0;
}




我建议完全重写,基于pete'' s代码早先发布了

或者像K& R2这样的好书中给出的示例实现。你需要了解内存分配和deacllocation更好的
。你会

还需要考虑链表的整体结构

程序。


如上所述,上面的代码是脆弱的,并且是极端错误的。



I suggest a complete rewrite, based on either pete''s code posted
earlier or sample implementations given in a good book like K&R2. You
need to understand memory allocation and deacllocation better. You''ll
also need to think about the overall structure of your linked list
program.

As written, the code above is fragile and erroneous to the extreme.


" santosh" < SA ********* @ gmail.com>写道:
"santosh" <sa*********@gmail.com> writes:
fool写道:
亲爱的小组,

感谢所有以前帮助过的人。

以前的主题链接(对不起google)
http://groups.google.com/group/comp....8ac16a2ed4ed35

#include< stdio.h>
#include< stdlib.h>
struct node
{
int data;
struct node * next;
};

struct node * add_node(struct node * p,int funct_data)
{struct struct * t = p;
为什么这个任务? p只包含一个不确定的值。这将导致未定义的行为。
Dear group,

Thanks for all those who helped previously.

Now I get some value printed. Is that the value of the list getting
printed? Am I
really polluted the list?

Previous thread link (sorry for google)
http://groups.google.com/group/comp....8ac16a2ed4ed35

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

struct node *add_node(struct node *p, int funct_data)
{
struct node *t = p;
Why this assignment? p just contains an indeterminate value. This will
lead to undefined behaviour.




到op:看看来自main()的调用。你没有设置p。



to the op : see the call from main(). You didnt set p.

/ *通过值传递。所以我将参数值保存在一个
* temp变量中。我是否正确?* /


是的,但你没有把任何东西传递给p。

t = malloc(sizeof * t);
如果(!t)
{
printf(mem error);
添加换行符或调用fflush(stdout) )。否则上述声明不会出现在控制台上。
/* passed by values. So I save the argument value in a
* temp variable.Am I correct?*/
yes, but you havent passed anything into p.

t = malloc(sizeof *t);
if(!t)
{
printf("mem error");
Add a newline or call fflush(stdout). Otherwise the above statement is
not garenteed to appear on the console.
退出(EXIT_FAILURE);
exit(EXIT_FAILURE);



你有在main()中分配了内存。如果你只是退出(),该内存可能会丢失到系统中。你应该返回main()表示
错误,free()内存然后退出()。



You''ve allocated memory in main(). If you simply exit(), that memory
might be lost to the system. You should return to main() indicating an
error, free() the memory and then exit().




出于好奇和OT,什么系统/ OS在程序崩溃或exit()调用的

事件中不替换malloc?



Out of curiosity and OT , what systems/OSs dont replace mallocs in the
event of a program crash or exit() call?

}
t-> data = funct_data ;
返回t;
}
int main(void)
{struct node * p,* q;
int i;
q = malloc(sizeof * q);
}
t->data = funct_data;
return t;
}

int main(void)
{
struct node *p,*q;
int i;
q=malloc(sizeof *q);



add_node()分配struct node的实例并返回。这里你已经设置q指向struct node的另一个实例。然后在下面
覆盖q以包含在
add_node()中分配的节点的地址,从而失去对原始节点的访问并导致内存泄漏。
在main()或add_node()中分配结构,而不是在两者中分配。



add_node() allocates an instance of struct node and returns. Here
you''ve set q to point to another instance of struct node. Then below
you overwrite q to contain the address of the node allocated in
add_node(), thus losing access to the original node and causing a
memory leak.

Either allocate the structure in main() or in add_node(), not in both.

if(!q)
exit(EXIT_FAILURE);
if(!q)
exit(EXIT_FAILURE);



再次,你正在退出内存中的free()'。这不是重要的



Again you''re exiting with free()''ing the memory. This won''t matter for




不,他不是。内存没有分配。在这种情况下..


到OP:别忘了你的下一个指针。查看你从$ main调用add_node的参数。使用调试器或printfs来检查传递给add_node函数的数据 - 你会看到

" p"没有初始化和下一步不习惯保留清单。


祝你好运!



No he''s not. The memory wasnt allocated. In this case..

To the OP : dont forget your "next" pointer. Look at the parameters you
are calling add_node with from main. Use a debugger or printfs to
examine the data being passed to your add_node function - you will see
"p" is not initialised and "next" is not used to keep the list.

best of luck!


这篇关于链接列表,新尝试(是:链接列表,没有输出,帮助)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆