链接列表问题。 [英] Linked List problem.

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

问题描述




我是C的新手,我正在尝试为我的程序设置一个简单的链表工作

。每个链表的结构存储char

*数据和*接下来引用下一个链接。我得到的问题是

,我试图链接我已经定义的结构和拒绝

链接。我已经尝试将我的结构转换为char *但是尝试将
转换回其原始结构以仅访问其内容。

fault。

我会尝试将链表中的数据类型更改为我的

结构,但这似乎不对。

任何建议是赞赏。


谢谢

Ben

Hi,

I am a newbie with C and am trying to get a simple linked list working
for my program. The structure of each linked list stores the char
*data and *next referencing to the next link. The problem I get is
that I am trying to link a struct that I have defined and its refusing
to link. I have tried casting my struct into char * but attempts to
cast it back to its original struct to access its contents only seg
faults.

I''d attempt to change the type of data in the linked list to that my
struct but that just doesnt seem right.
Any advice is appreciated.

Thanks
Ben

推荐答案



Ben <是****** @ hotmail.com>在消息中写道

news:87 ************************** @ posting.google.c om ...

"Ben" <be******@hotmail.com> wrote in message
news:87**************************@posting.google.c om...


我是C的新手,我正在尝试为我的程序设置一个简单的链接列表。每个链表的结构存储char
* data和* next,引用下一个链接。我得到的问题是,我试图链接我已定义的结构和拒绝链接的结构。我已经尝试将我的结构转换为char *,但是尝试将其转换回原始结构以仅访问其内容中的错误。

我会尝试更改链接列表中的数据类型到我的
结构但是看起来不对。
任何建议都表示赞赏。
Hi,

I am a newbie with C and am trying to get a simple linked list working
for my program. The structure of each linked list stores the char
*data and *next referencing to the next link. The problem I get is
that I am trying to link a struct that I have defined and its refusing
to link. I have tried casting my struct into char * but attempts to
cast it back to its original struct to access its contents only seg
faults.

I''d attempt to change the type of data in the linked list to that my
struct but that just doesnt seem right.
Any advice is appreciated.




您的错误在第42行。


-Mike



Your bug is on line 42.

-Mike


是****** @ hotmail.com (Be​​n)写道:
be******@hotmail.com (Ben) writes:
我是C的新手,我试图得到一个简单的链接列表工作
为我的程序。每个链表的结构存储char
* data和* next,引用下一个链接。我得到的问题是,我试图链接我已定义的结构和拒绝链接的结构。我已经尝试将我的结构转换为char *,但是尝试将其转换回原始结构以仅访问其内容中的错误。

我会尝试更改链接列表中的数据类型到我的
结构但是看起来不对。
任何建议都表示赞赏。
I am a newbie with C and am trying to get a simple linked list working
for my program. The structure of each linked list stores the char
*data and *next referencing to the next link. The problem I get is
that I am trying to link a struct that I have defined and its refusing
to link. I have tried casting my struct into char * but attempts to
cast it back to its original struct to access its contents only seg
faults.

I''d attempt to change the type of data in the linked list to that my
struct but that just doesnt seem right.
Any advice is appreciated.



首先,不要做任何演员。编码

简单链接列表是不必要的,如果不需要投射,通常最好

来避免它。

二,开始定义类型,可能是这样的:


typedef struct node_struct_tag * List,ListElement;


struct node_struct_tag {

char * data;

下一个列表;

};

现在,一个简单的功能:


unsigned int

list_length(List head_of_list){

列出剩余; unsigned int result;


余数= head_of_list;

result = 0;


while(余数!= 0 ){

结果++;

余数=余数 - >下一个;

}


返回结果;

}

列表形成功能:


列表

lisp_style_cons(char *值,列表尾部){

列出结果;


result = malloc(sizeof(* result));

断言(结果!= 0);


result-> data = value;

result-> next = tail;


返回结果;

}

更典型的C风格操作功能:


void
splice_one_element_into_list(列表元素,列表* list_address){

断言(list_address!= 0);

断言(元素!= 0);


element-> next = * list_address;

* list_address = element;

}

该功能可能会用到插入

a排序列表的函数:


void

insert_into_sorted_list(List element,List * sorted_list_address){

List * insertion_point;


断言(元素!= 0);

断言(sorted_list_address!= 0);


insertion_point = sorted_list_address;

while(* insertion_point!= 0){

if(strcmp((* insertion_point) - > data,element - >数据)> 0){

休息;

}

insertion_point =& (* insertion_point) - >下一个;

}


splice_one_element_into_list(element,insertion_point);

}

在尝试编译此代码之前,请仔细阅读并确保

您了解您认为它正在做什么以及为什么。


如果你想尝试编译,首先找到适当的标题为

assert(),malloc()和strcmp(),并#include它们。


上面的函数几乎肯定不是你想要的,但如果你能理解这些,你应该能够写出你想要的函数

。 />

免责声明:上面的代码尚未编译,甚至语法

已检查(除非我自己手动)。我很有信心NG的许多读者会慷慨地报告任何傻瓜错误

我已经做了。 :)


First, don''t do any casting. Casting is unnecessary for coding a
simple linked list, and if casting is unnecessary it usually is best
to avoid it.
Second, start with defining the type, perhaps something like this:

typedef struct node_struct_tag *List, ListElement;

struct node_struct_tag {
char *data;
List next;
};
Now, a simple function:

unsigned int
list_length( List head_of_list ){
List remainder; unsigned int result;

remainder = head_of_list;
result = 0;

while( remainder != 0 ){
result++;
remainder = remainder->next;
}

return result;
}
A function for list formation:

List
lisp_style_cons( char *value, List tail ){
List result;

result = malloc( sizeof( *result ) );
assert( result != 0 );

result->data = value;
result->next = tail;

return result;
}
A function for more typical C-style manipulation:

void
splice_one_element_into_list( List element, List *list_address ){
assert( list_address != 0 );
assert( element != 0 );

element->next = *list_address;
*list_address = element;
}
That function might be used in a function that inserts into
a sorted list:

void
insert_into_sorted_list( List element, List *sorted_list_address ){
List *insertion_point;

assert( element != 0 );
assert( sorted_list_address != 0 );

insertion_point = sorted_list_address;
while( *insertion_point != 0 ){
if( strcmp( (*insertion_point)->data, element->data ) > 0 ){
break;
}
insertion_point = & (*insertion_point)->next;
}

splice_one_element_into_list( element, insertion_point );
}
Before trying to compile this code, read through it and make sure
you understand what you think it''s doing and why.

If you want to try compiling, first find the appropriate headers for
assert(), malloc() and strcmp(), and #include them.

The functions above almost certainly aren''t what you want, but if
you can understand these you should be able to write the functions
that you do want.

Disclaimer: the code above has not been compiled or even syntax
checked (except manually by myself). I''m confident the many
readers of the NG will graciously report any bonehead errors
I''ve made. :)




Tim Rentsch写道:


Tim Rentsch wrote:
是****** @ hotmail.com (Be​​n)写道:

be******@hotmail.com (Ben) writes:

我是一个C的新手,我正试图为我的程序获得一个简单的链接列表。每个链表的结构存储char
* data和* next,引用下一个链接。我得到的问题是,我试图链接我已定义的结构和拒绝链接的结构。我已经尝试将我的结构转换为char *,但是尝试将其转换回原始结构以仅访问其内容中的错误。

我会尝试更改链接列表中的数据类型与我的
结构相似,但似乎没有。
任何建议都表示赞赏。
I am a newbie with C and am trying to get a simple linked list working
for my program. The structure of each linked list stores the char
*data and *next referencing to the next link. The problem I get is
that I am trying to link a struct that I have defined and its refusing
to link. I have tried casting my struct into char * but attempts to
cast it back to its original struct to access its contents only seg
faults.

I''d attempt to change the type of data in the linked list to that my
struct but that just doesnt seem right.
Any advice is appreciated.



typedef struct node_struct_tag * List, ListElement;

struct node_struct_tag {
char * data;
列表下一个;
};



.... snip ....

List
lisp_style_cons(char * value,List tail){
列出结果;

结果= malloc (sizeof(* result));
断言(结果!= 0);

结果 - > data = value;
result-> next = tail;

.... snip ...

免责声明:上面的代码还没有编译甚至语法
检查(manua除外)我自己)。我很有信心NG的许多读者会慷慨地报告我已经做过的任何傻瓜错误。 :)



typedef struct node_struct_tag *List, ListElement;

struct node_struct_tag {
char *data;
List next;
};


....snip....
List
lisp_style_cons( char *value, List tail ){
List result;

result = malloc( sizeof( *result ) );
assert( result != 0 );

result->data = value;
result->next = tail;

return result;
}

....snip...

Disclaimer: the code above has not been compiled or even syntax
checked (except manually by myself). I''m confident the many
readers of the NG will graciously report any bonehead errors
I''ve made. :)




我在骨头错误中没有注意到,但上面列出的函数lisp_style_cons,

让我害怕。该函数要求''value''

指向其他地方分配的存储空间。我更喜欢在创建节点的函数中封装

分配和赋值。

以下示例:


#include< ; stdlib.h>

#include< string.h>

#include< stdio.h>


typedef struct NODE

{

char * data;

struct NODE * next;

} NODE;


unsigned NodesInList(NODE * p);

int AddListHead(NODE ** p,const char * s);

int AddListTail( NODE ** p,const char * s);

void PrintList(NODE * p);

void FreeList(NODE ** p);


int main(无效)

{

NODE * head = NULL;


AddListTail(& head,Hello World);

AddListTail(& head,Goodbye World);

AddListHead(& head," list head" );

printf(列表中有%u个节点。\ n,

NodesInList(头部));

的printList(头);

FreeList(& head);

printf(" \ nFreed the list。现在列表中有%u

节点。\ nnn,NodesInList(head));

返回0;

}


unsigned NodesInList(NODE * p)

{

unsigned nelem;


for(nelem = 0; p; p = p-> next,nelem ++);

return nelem;

}

int AddListHead(NODE ** p,const char * s)

{

NODE * new;


if((new = malloc(sizeof * new))== NULL)返回0;

if((new-> data = malloc(strlen(s)+1))== NULL )

{

免费(新);

返回0;

}

strcpy(new-> data,s);

new-> next = * p;

* p = new;

返回1;

}


int AddListTail(NODE ** p,const char * s)

{

NODE ** tmp,* new;


if((new = malloc(sizeof * new))== NULL)return 0;

if((new-> data = malloc(strlen(s)+1))== NULL)

{

free(new);

返回0;

}

strcpy(new-> data,s);

new-> next = NULL;

for(tmp = p; * tmp!= NULL; tmp =&(* tmp) - > next);

* tmp = new;

返回1;

}


无效PrintList(NODE * p)

{

未签名我;


for(i = 1; p; p = p-> next,i ++)

printf(" Node%u:Data is \"%s \" \ n,i,p->数据);

返回;

}


void FreeList(NODE * * p)

{

NODE * tmp;


for(; * p; * p = tmp)
{

tmp =(* p) - > next;

免费((* p) - >数据);

免费(* p);

}

返回;

}


- -

Al Bowers

Tampa,Fl USA

mailto: xa ****** @ myrapidsys.com (删除x发送电子邮件)
http://www.geocities.com/abowers822/



I haven''t noticed in bonehead errors but function lisp_style_cons,
listed above, scares me. The function requires that ''value''
point to storage allocated somewhere else. I prefer to encapsulate
the allocation and assignment in the function that creates the node.
Examples below:

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

typedef struct NODE
{
char *data;
struct NODE *next;
} NODE;

unsigned NodesInList( NODE *p );
int AddListHead( NODE **p, const char *s );
int AddListTail( NODE **p, const char *s );
void PrintList( NODE *p );
void FreeList( NODE **p );

int main(void)
{
NODE *head = NULL;

AddListTail(&head,"Hello World");
AddListTail(&head,"Goodbye World");
AddListHead(&head,"The list head");
printf("There are %u Nodes in the list.\n",
NodesInList(head));
PrintList(head);
FreeList(&head);
printf("\nFreed the list. Now there are %u "
"nodes in the list.\n",NodesInList(head));
return 0;
}

unsigned NodesInList( NODE *p )
{
unsigned nelem;

for(nelem = 0; p; p = p->next,nelem++) ;
return nelem;
}

int AddListHead( NODE **p, const char *s )
{
NODE *new;

if((new = malloc(sizeof *new)) == NULL) return 0;
if((new->data = malloc(strlen(s)+1)) == NULL)
{
free(new);
return 0;
}
strcpy(new->data,s);
new->next = *p;
*p = new;
return 1;
}

int AddListTail( NODE **p, const char *s )
{
NODE **tmp, *new;

if((new = malloc(sizeof *new)) == NULL) return 0;
if((new->data = malloc(strlen(s)+1)) == NULL)
{
free(new);
return 0;
}
strcpy(new->data,s);
new->next = NULL;
for(tmp = p; *tmp != NULL;tmp = &(*tmp)->next) ;
*tmp= new;
return 1;
}

void PrintList( NODE *p )
{
unsigned i;

for(i = 1 ; p; p = p->next,i++)
printf("Node %u: Data is \"%s\"\n",i,p->data);
return;
}

void FreeList( NODE **p )
{
NODE *tmp;

for( ; *p; *p = tmp)
{
tmp = (*p)->next;
free((*p)->data);
free(*p);
}
return;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


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

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