递归结构? [英] Recursive structure?

查看:62
本文介绍了递归结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来了一个声明以下数据的程序

结构。


typedef struct node {

struct node * next;

} node;


它看起来像一个递归数据结构,但我遇到了麻烦

了解它的用途吗?有人可以解释一下吗?


当结构实例化时,编译器将分配多少内存?


以下结构使用了以前的结构。


typedef struct queue {

node * head,* tail;

}队列;


祝你好运!

解决方案

----- BEGIN PGP SIGNED MESSAGE -----

哈希:SHA1

dspfun写道:


我已经来一个声明以下数据的程序

结构。

typedef struct node {

struct node * next;

}节点;


它看起来像一个递归数据结构



不是真的,但是你/是/关闭。首先,引用的文本不是

结构,它是一个typedef。 typedef定义一个与简单或复杂存储类型相关联的名称

。在这种情况下,与名称node相关联的类型

恰好是结构定义

称为node。


但是我遇到了麻烦

了解它的使用情况?有人可以解释一下吗?



当然。


假设你担心


struct node {

struct node * next;

};



typedef的一部分,然后结构定义称为node

包含/ pointer /(类型为指针)结构节点)。


当结构实例化时,编译器将分配多少内存?



首先,实例化"不是用的术语。但是,我知道你的b $ b意味着什么。


多少内存?好吧,它将是

sizeof(结构节点)

字节长。


什么是真实的价值,你问?我们不能告诉你,除了说

它至少足以容纳一个指向结构的指针。

结构/可能/是分配了隐形填充/对齐

字节,使结构大于其组件的总和,但

由编译器决定。此外,根据您的
编译器,指向struct节点的指针可能比任何其他指针更大,更小或相同

大小。再次,这取决于实现。


[snip]

HTH


- -

Lew Pitcher


-----开始PGP签名-----

版本:GnuPG v1.4.3(MingW32) - WinPT 0.11.12


iD8DBQFE2fYCagVFX4UWr64RAsvqAKDoqBMdCETJhrKiFqbcTj 1h7mwKBwCg4MMf

esqUVrmtlZg6juKjuZpIHV8 =

= ERDL

-----结束PGP签名-----




Lew Pitcher写道:


-----开始PGP签名消息-----

哈希:SHA1


dspfun写道:
< blockquote class =post_quotes>
我来了一个声明以下数据的程序

结构。


typedef struct node {

struct node * next;

} node;


它看起来像一个递归数据结构



不是,但你/是/关闭。首先,引用的文本不是

结构,它是一个typedef。 typedef定义一个与简单或复杂存储类型相关联的名称

。在这种情况下,与名称node相关联的类型

恰好是结构定义

称为node。


但是我遇到了麻烦

了解它的使用情况?有人可以解释一下吗?



当然。


假设你担心


struct node {

struct node * next;

};



typedef的一部分,然后结构定义称为node

包含/ pointer /(类型为指针)结构节点)。



是的,这正是我不明白的,怎么能结构节点

包含类型指向结构节点的指针的指针?似乎

" struct node"是不是很容易宣布?编译器如何知道

要做什么?对我而言,似乎类似于说x具有值x。


编译器将分配多少内存结构是实例化的?



首先,实例化"不是用的术语。但是,我知道你的b $ b意味着什么。


多少内存?好吧,它将是

sizeof(结构节点)

字节长。


什么是真实的价值,你问?我们不能告诉你,除了说

它至少足以容纳一个指向结构的指针。

结构/可能/是分配了隐形填充/对齐

字节,使结构大于其组件的总和,但

由编译器决定。此外,根据您的
编译器,指向struct节点的指针可能比任何其他指针更大,更小或相同

大小。再次,这取决于实现。


[snip]


HTH


- -

Lew Pitcher


-----开始PGP签名-----

版本:GnuPG v1.4.3( MingW32) - WinPT 0.11.12

iD8DBQFE2fYCagVFX4UWr64RAsvqAKDoqBMdCETJhrKiFqbcTj 1h7mwKBwCg4MMf

esqUVrmtlZg6juKjuZpIHV8 =

= ERDL

- ----结束PGP SIGNATURE -----




dspfun写道:


是的,这正是我不明白的,结构节点怎么可能包含一个类型为指向结构节点的指针的指针?似乎

" struct node"是不是很容易宣布?编译器如何知道

要做什么?对我来说,似乎类似于说x具有值x。



啊,对你非常敏感。但它确实有效,因为:


(1)包含的是同一类型变量的指针。


( 2)在我能想到的大多数系统中,除了可能有些奇怪的好处之外,一个指针只是一个地址。任何东西的地址,

无论物体的大小,形状或类型如何,地址都是固定的位数,通常为16,32, 48或64位。


(3)所以当编译器看到:struct foo {foo * nextfoo;它去了

好吧,foo还没有被完全宣布,事实上,我甚至可能不知道有一个foo被声明,但是指向foo什么是

只有32位,我知道如何分配


(4)最终foo将会结束,一切都会好的。 br $>

----

它可以更抽象,你可以提前定义:

typedef foo * PtrToFoo;


这将被接受,因为编译器知道你可能打算

做同样的事情。当然最终基本类型还有

来定义。


I''ve come a across a program that declares the following data
structure.

typedef struct node {
struct node *next;
} node;

It looks like a recursive data structure but I''m having trouble
understanding the use of it? Can somebody explain this?

How much memory will be allocated by the compiler when the structure is
instantiated?

The following structure makes use of the previous structure.

typedef struct queue {
node *head, *tail;
} queue;

Best regards!

解决方案

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
dspfun wrote:

I''ve come a across a program that declares the following data
structure.

typedef struct node {
struct node *next;
} node;

It looks like a recursive data structure

Not really, but you /are/ close. First off, the quoted text isn''t a
structure, it is a typedef. A typedef defines a name that will be
associated to a simple or complex storage type. In this case, the type
associated to the name "node'' just happens to be a structure definition
called "node".

but I''m having trouble
understanding the use of it? Can somebody explain this?

Sure.

Assuming that you are concerned about the

struct node {
struct node *next;
};

portion of the typedef, then the structure definition called "node"
contains a /pointer/ (of the type "pointer to struct node").

How much memory will be allocated by the compiler when the structure is
instantiated?

First off, "instantiation" isn''t the term to use. But, I know what you
mean.

How much memory?? Well, it will be
sizeof(struct node)
bytes long.

What''s the "real" value, you ask? We can''t tell you, other than to say
that it will be at least big enough to hold a pointer to a structure.
The structure /may/ be allocated with invisible padding/alignment
bytes, making the structure bigger than the sum of it''s components, but
that''s up to the compiler to determine. Also, depending on your
compiler, a pointer to struct node may be larger, smaller, or the same
size as any other pointer. Again, this is implementation dependant.

[snip]
HTH

- --
Lew Pitcher

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFE2fYCagVFX4UWr64RAsvqAKDoqBMdCETJhrKiFqbcTj 1h7mwKBwCg4MMf
esqUVrmtlZg6juKjuZpIHV8=
=ERDL
-----END PGP SIGNATURE-----



Lew Pitcher wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
dspfun wrote:

I''ve come a across a program that declares the following data
structure.

typedef struct node {
struct node *next;
} node;

It looks like a recursive data structure


Not really, but you /are/ close. First off, the quoted text isn''t a
structure, it is a typedef. A typedef defines a name that will be
associated to a simple or complex storage type. In this case, the type
associated to the name "node'' just happens to be a structure definition
called "node".

but I''m having trouble
understanding the use of it? Can somebody explain this?


Sure.

Assuming that you are concerned about the

struct node {
struct node *next;
};

portion of the typedef, then the structure definition called "node"
contains a /pointer/ (of the type "pointer to struct node").

Yes, this is exactly what I don''t understand, how can "structure node"
contain a pointer of the type "pointer to struct node"? It seems that
"struct node" is not declared readily? How can the compiler know what
to do? To me it seems analogous to saying "x has the value x".

How much memory will be allocated by the compiler when the structure is
instantiated?


First off, "instantiation" isn''t the term to use. But, I know what you
mean.

How much memory?? Well, it will be
sizeof(struct node)
bytes long.

What''s the "real" value, you ask? We can''t tell you, other than to say
that it will be at least big enough to hold a pointer to a structure.
The structure /may/ be allocated with invisible padding/alignment
bytes, making the structure bigger than the sum of it''s components, but
that''s up to the compiler to determine. Also, depending on your
compiler, a pointer to struct node may be larger, smaller, or the same
size as any other pointer. Again, this is implementation dependant.

[snip]
HTH

- --
Lew Pitcher

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFE2fYCagVFX4UWr64RAsvqAKDoqBMdCETJhrKiFqbcTj 1h7mwKBwCg4MMf
esqUVrmtlZg6juKjuZpIHV8=
=ERDL
-----END PGP SIGNATURE-----



dspfun wrote:

Yes, this is exactly what I don''t understand, how can "structure node"
contain a pointer of the type "pointer to struct node"? It seems that
"struct node" is not declared readily? How can the compiler know what
to do? To me it seems analogous to saying "x has the value x".

Ah, quite perceptive of you. But it does work because:

(1) What''s included is a POINTER to a variable of the same type.

(2) On most every system I can think of, except perhaps some weird ol
Burroughs, a POINTER is just an address. And the address of anything,
no matter what the size, shape or type of the thing, the address is
just a fixed number of bits, often 16, 32, 48, or 64 bits.

(3) So when the compiler sees: struct foo{ foo * nextfoo; it goes
"okay, foo hasnt been fully declared yet, in fact, I may not even be
aware that there is a foo being declared, but a pointer to anything is
just 32 bits, I know how to allocate that"

(4) Eventually foo will come to an end and everything will be okay.

----

It can get even a bit more abstract, you can define ahead of time:

typedef foo * PtrToFoo;

and that will be accepted, as the compiler "knows" you may be intending
to do the same thing as above. of course eventually the base type has
to be defined somewheere.


这篇关于递归结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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