我的链表代码有什么问题? [英] what's wrong with my code of linked list?

查看:63
本文介绍了我的链表代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试链接列表的问题。

我做了很多次:创建一个列表,然后释放它。


## ###########################################

#include< stdio.h>

#include< stdlib.h>


struct element {

int index ;

struct element * next;

};


void free_list(struct element * first){

struct element * current,* next;

current = first;

next = current-> next;

while(next != NULL){

printf(释放%d元素\ n,当前 - >索引);

免费(当前);

current = next;

next = current-> next;

}

free(current);

}


void print_list(struct element * first){

struct element * current;

current = first;

while(current!= NULL){

printf("%d th element visited\\\
,current-> index); < b r />
current = current-> next;

}

}

struct element * create_list(void){

struct element * temp,* leading,* previous;

int i;

leading = malloc(sizeof(struct element));

leading-> index = 0;

previous = leading;


for(i = 1; i< 1000; i ++) {

temp = malloc(sizeof(struct element));

temp-> next == NULL;

temp-> index = i;

previous-> next = temp;

previous = temp;

}

返回领先;

}


int main(){

int i;

struct element * dummy ;

for(i = 0; i< 2; i ++){

dummy = create_list();

print_list(dummy); < br $>
free_list(假);

}

返回0;

}


********************************************* *** ****

这段代码怎么了?我用(i = 0; i< 1; i ++)

main()时很好。


谢谢

解决方案

un **** ********@hotmail.com 写道......

temp-> next == NULL;




"问题?" <未************ @ hotmail.com>写道:

我正在测试链表的问题。
我只是做了很多次:创建一个列表,然后释放它。

### ##########################################
#include< stdio.h>
#include< stdlib.h>
[snip]
*************************************** *********** ****
这段代码怎么了?当我在
main()中使用(i = 0; i< 1; i ++)时它很好。




你将会有给我们一个提示。什么程序应该是什么?b $ b要做什么,它实际上做了什么?


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


" questions?" <未************ @ hotmail.com>写道:

我正在测试链表的问题。
我只是做了很多次:创建一个列表,然后释放它。
############################################# $


[Snip链表代码]

**************************** ********************** ****
这段代码怎么了?我在
main()中使用(i = 0; i< 1; i ++)时很好。




你的问题是什么?它?它对我来说运行良好,乍看之下我不会发现它有任何问题。我唯一能选择的就是

你称之为领先的东西。通常被称为头节点,但

这不是一个实际错误,这是一个风格问题。


Richard


I am testing a problem with linked list.
I just do a lot of times: create a list, then free it.

#############################################
# include <stdio.h>
# include <stdlib.h>

struct element {
int index;
struct element *next;
};

void free_list(struct element * first){
struct element *current,*next;
current=first;
next=current->next;
while(next !=NULL){
printf("free the %d the element\n",current->index);
free(current);
current=next;
next=current->next;
}
free(current);
}

void print_list(struct element *first){
struct element *current;
current=first;
while(current !=NULL){
printf("the %d th element visited\n",current->index);
current=current->next;
}
}
struct element *create_list(void){
struct element *temp,*leading,*previous;
int i;
leading=malloc(sizeof(struct element));
leading->index=0;
previous=leading;

for(i=1;i<1000;i++){
temp=malloc(sizeof(struct element));
temp->next==NULL;
temp->index=i;
previous->next=temp;
previous=temp;
}
return leading;
}

int main(){
int i;
struct element *dummy;
for(i=0;i<2;i++){
dummy=create_list();
print_list(dummy);
free_list(dummy);
}
return 0;
}

************************************************** ****
What''s wrong this this code? it is fine when I use for(i=0;i<1;i++) in
main().

Thanks

解决方案

un************@hotmail.com wrote...

temp->next==NULL;




"questions?" <un************@hotmail.com> writes:

I am testing a problem with linked list.
I just do a lot of times: create a list, then free it.

#############################################
# include <stdio.h>
# include <stdlib.h> [snip]
************************************************** ****
What''s wrong this this code? it is fine when I use for(i=0;i<1;i++) in
main().



You''re going to have to give us a hint. What is the program supposed
to do, and what does it actually do?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


"questions?" <un************@hotmail.com> wrote:

I am testing a problem with linked list.
I just do a lot of times: create a list, then free it.

#############################################
[ Snip linked list code ]
************************************************** ****
What''s wrong this this code? it is fine when I use for(i=0;i<1;i++) in
main().



What''s your problem with it? It runs fine for me, and at first glance I
can''t find anything wrong with it. The only nit I would pick is that
what you call "leading" is conventionally called the head node, but
that''s not an actual error, it''s a style issue.

Richard


这篇关于我的链表代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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