链接的对象列表 [英] Linked list of object

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

问题描述

假设我定义了以下类:


class example_class {


public:

example_class( );

void funtion_1();

void function_2();


受保护:

struct st {

example_class * next_element;

example_class * prev_element;

} s1;


int variable_1;

double variable_2;


private:

int * variable_3;

}


然后我创建了四个对象:

example_class * A;

example_class * B;

example_class * C;

example_class * D;


并将它们链接到链接列表:ABCD。

然后我创建一个新对象E并希望在C之前插入E.

以下是我的工作:


example_class * E;


E-> s1.next_element = C;

E-> s1.prev_element = B;

C-> s1.prev_element =&( E-> s1.next_elemet);

B-> s1.next_element =&(E-> s1.next_element);


以上操作是否正确?对于最后两句,我也可以

写:

C-> s1.prev_element = E;

B-> s1 .next_element = E;

我认为&(E-> s1.next_elemet)= E,我是对的吗?


----- ---------------------------------

以下是另一种情况:

我将example_class的定义修改为:


class example_class {


public:

example_class();

void funtion_1();

void function_2();


double new_variable1; //我添加两个变量

int new_variable2; //在strust st。

受保护的前面:

struct st {

example_class * next_element;

example_class * prev_element;

} s1;


int variable_1;

double variable_2;


private:

int * variable_3;

}


我在struct st前添加两个新变量后,我仍然可以像上面的例子那样操作

吗?那就是:


使用链表:ABCD与上面相同。

然后我创建一个新对象E并希望在C之前插入E. />
以下是我的工作:


example_class * E;


E-> s1.next_element = C;

E-> s1.prev_element = B;

C-> s1.prev_element =&(E-> s1.next_elemet);

B-> s1.next_element =&(E-> s1.next_element):


以上操作是否正确?

在struct st前面添加了两个新变量,我还能说b&b;&(E-> s1.next_elemet)= E?

只要有在结构的前面是可变的,我不能

说&(E-> s1.next_elemet)= E?

如果可能的话,请给出你的更正。


非常感谢。


杰克

解决方案

为什么不只是使用std :: list。它为您提供了一个已编写的通用

双向链表。


C ++ fan写道:

假设我定义了以下类:

class example_class {

public:
example_class();
void funtion_1();
void function_2();

protected:
struct st {
example_class * next_element;
example_class * prev_element;
} s1;

int variable_1;
double variable_2;

private:
int * variable_3;
}
然后我创建了四个对象:
example_class * A;
example_class * B;
example_class * C;
example_class * D;

并将它们链接到链接列表:ABCD 。
然后我创建了一个新的对象E并希望在C之前插入E.
以下是我的工作:

example_class * E;

E-> s1.next_element = C;
E-> s1.prev_element = B;
C-> s1.prev_eleme nt =&(E-> s1.next_elemet);
B-> s1.next_element =&(E-> s1.next_element);

以上操作是否正确?


否。除了拼写错误之外,你还要指定一个指针指向

example_class的指针。到指向example_class的指针。你的编译器应该是

告诉你这个。

对于最后两句,我也可以写:
C-> s1。 prev_element = E;
B-> s1.next_element = E;


那要好得多。

我认为&(E-> s1.next_elemet)= E,我是对的吗?


不,这些指针有不同的类型。

---------------------- ----------------
以下是另一种情况:
我将example_class的定义修改为:

class example_class {

public:
example_class();
void funtion_1();
void function_2();

double new_variable1; //我添加了两个变量
int new_variable2; //在strust st。
protected:
struct st {
example_class * next_element;
example_class * prev_element;
} s1;

int variable_1;
double variable_2;

私有:
int * variable_3;
}

我添加两个新变量后在struct st前面,我还可以做与上例相同的操作吗?


不,你还是不能这样做。

即:

链表:ABCD与上面。
然后我创建一个新的对象E,并希望在C之前插入E.
以下是我的工作:

example_class * E;
E-> s1.next_element = C;
E-> s1.prev_element = B;
C-> s1.prev_element =&(E-> s1.next_elemet); < br-> B-> s1.next_element =&(E-> s1.next_element):

以上操作是否正确?


No.

在struct st前面添加了两个新变量,我还能说&(E-> s1 .next_elemet)= E?


No.

只要结构面前有变数,我就不能说&(E-> s1。 next_elemet)= E?


不,即使这些指针包含相同的地址(我不确定是否需要b $ b),你还有类型不匹配。顺便说一句,

这是你第五次犯同样的错误......我错过了什么?

如果可能,请给你更正。

非常感谢。

杰克




Jeff Schwab< je ******@comcast.net>在消息新闻中写道:< yO ******************** @ comcast.com> ...

C ++ fan写道:

假设我定义了以下类:

class example_class {

public:
example_class();
void funtion_1();
void function_2();

protected:
struct st {
example_class * next_element;
example_class * prev_element;
} s1;

int variable_1;
double variable_2;

私人:
int * variable_3;
}
example_class * A;
example_class * B;
example_class * C;
example_class * D;

和链接他们将其作为链接列表:ABCD。
然后我创建一个新对象E并想在C之前插入E.
以下是我的工作:

example_class * E;

E-> s1.next_element = C;
E-> s1.prev_element = B;
C-> s1.prev_element =&a mp;(E-> s1.next_elemet);
B-> s1.next_element =&(E-> s1.next_element);

以上操作是否正确?<除了拼写错误之外,你还要指定一个指针指向
example_class的指针。到指向example_class的指针。你的编译器应该告诉你这个。

对于最后两句,我也可以写:
C-> s1。 prev_element = E;
B-> s1.next_element = E;



那就好多了。

I think&(E-> s1.next_elemet)= E,我是对吗?



不,这些指针有不同的类型。



感谢您的回复。

我想知道的是对象E的入口地址。

因为E-> s1.next_element是第一个成员变量,是E-> s1.next_element的地址与E的地址相同?
或者是E-> s1.next_element位于同一个内存中位置为E?

我可以说(example_class *)(&(E-> s1.next_element))== E?

- -------------------------------------
以下是另一种情况:
我修改了example_clas的定义如下:

class example_class {

public:
example_class();
void funtion_1();
void function_2() ;

double new_variable1; //我添加了两个变量
int new_variable2; //在strust st。
protected:
struct st {
example_class * next_element;
example_class * prev_element;
} s1;

int variable_1;
double variable_2;

私有:
int * variable_3;
}

我添加两个新变量后在struct st的前面,我还可以做与上面例子相同的操作吗?



不,你还是不能这样做。

那就是:

链接列表:ABCD与上面相同。
然后我创建一个新对象E并想在C之前插入E.
以下是我的工作:

example_class * E;

E-> s1.next_element = C;
E-> s1.prev_element = B;
C-> s1.prev_element =&(E-> s1.next_elemet);
B-> s1.next_element =&(E-> s1.next_element):



没有。

有两个新的变量在结构st前添加的ables,我还能说&(E-> s1.next_elemet)= E?



No.
< blockquote class =post_quotes>只要struct st的变量面前,我就不能说&(E-> s1.next_elemet)= E?



不,即使这些指针包含相同的地址(我不确定是否需要),你的类型不匹配。顺便说一句,这是你第五次犯同样的错误......我错过了什么?




加两个在struct st前面的变量,对象E的入口地址

被更改了吗?对象E的入口地址是E-> s1.next_element的

地址或new_variable1的地址?


以下两个表达式:

(example_class *)(&(E-> s1.next_element))== E

(example_class *)(& new_variable1)== E


哪一个是正确的?


非常感谢。


杰克


Suppose that I define the following class:

class example_class{

public:
example_class();
void funtion_1();
void function_2();

protected:
struct st {
example_class *next_element;
example_class *prev_element;
} s1;

int variable_1;
double variable_2;

private:
int *variable_3;
}

Then I create four objects:
example_class *A;
example_class *B;
example_class *C;
example_class *D;

And link them togother as a linked list: A-B-C-D.
Then I create a new object E and want to insert E before C.
Below is what I do:

example_class *E;

E->s1.next_element = C;
E->s1.prev_element = B;
C->s1.prev_element = &(E->s1.next_elemet);
B->s1.next_element = &(E->s1.next_element);

Are above operation correct? For the last two sentences, I can also
write:
C->s1.prev_element = E;
B->s1.next_element = E;
I think &(E->s1.next_elemet)= E, am I right?

--------------------------------------
Below is another situation:
I modify the defination of example_class to be:

class example_class{

public:
example_class();
void funtion_1();
void function_2();

double new_variable1; //I add two variables
int new_variable2; // in front of the strust st.
protected:
struct st {
example_class *next_element;
example_class *prev_element;
} s1;

int variable_1;
double variable_2;

private:
int *variable_3;
}

After I add two new variables in front of struct st, can I still do
the same operation as above example? That is:

With a linked list: A-B-C-D same as above.
Then I create a new object E and want to insert E before C.
Below is what I do:

example_class *E;

E->s1.next_element = C;
E->s1.prev_element = B;
C->s1.prev_element = &(E->s1.next_elemet);
B->s1.next_element = &(E->s1.next_element):

Are the above operation correct?
With two new variables added in front of struct st, can I still
say that &(E->s1.next_elemet)=E ?
As long as there is variable infront of struct st, I can not
say &(E->s1.next_elemet)=E ?
If possible, please give your correction.

Thanks a lot.

Jack

解决方案

Why not just use std::list. It gives you an already written, generic
doubly-linked list.


C++fan wrote:

Suppose that I define the following class:

class example_class{

public:
example_class();
void funtion_1();
void function_2();

protected:
struct st {
example_class *next_element;
example_class *prev_element;
} s1;

int variable_1;
double variable_2;

private:
int *variable_3;
}

Then I create four objects:
example_class *A;
example_class *B;
example_class *C;
example_class *D;

And link them togother as a linked list: A-B-C-D.
Then I create a new object E and want to insert E before C.
Below is what I do:

example_class *E;

E->s1.next_element = C;
E->s1.prev_element = B;
C->s1.prev_element = &(E->s1.next_elemet);
B->s1.next_element = &(E->s1.next_element);

Are above operation correct?
No. Aside from the typo, you''re assigning a "pointer to pointer to
example_class" to a "pointer to example_class." Your compiler should be
telling you about this.
For the last two sentences, I can also
write:
C->s1.prev_element = E;
B->s1.next_element = E;
That''s much better.
I think &(E->s1.next_elemet)= E, am I right?
No, those pointers have different types.
--------------------------------------
Below is another situation:
I modify the defination of example_class to be:

class example_class{

public:
example_class();
void funtion_1();
void function_2();

double new_variable1; //I add two variables
int new_variable2; // in front of the strust st.
protected:
struct st {
example_class *next_element;
example_class *prev_element;
} s1;

int variable_1;
double variable_2;

private:
int *variable_3;
}

After I add two new variables in front of struct st, can I still do
the same operation as above example?
No, you still can''t do it.
That is:

With a linked list: A-B-C-D same as above.
Then I create a new object E and want to insert E before C.
Below is what I do:

example_class *E;

E->s1.next_element = C;
E->s1.prev_element = B;
C->s1.prev_element = &(E->s1.next_elemet);
B->s1.next_element = &(E->s1.next_element):

Are the above operation orrect?
No.
With two new variables added in front of struct st, can I still
say that &(E->s1.next_elemet)=E ?
No.
As long as there is variable infront of struct st, I can not
say &(E->s1.next_elemet)=E ?
No, even if those pointers contain the same address (I''m not sure
whether that''s required), you''ve got a type mismatch. By the way,
that''s the fifth time you''ve made the same typo... Am I missing something?
If possible, please give your correction.

Thanks a lot.

Jack




Jeff Schwab <je******@comcast.net> wrote in message news:<yO********************@comcast.com>...

C++fan wrote:

Suppose that I define the following class:

class example_class{

public:
example_class();
void funtion_1();
void function_2();

protected:
struct st {
example_class *next_element;
example_class *prev_element;
} s1;

int variable_1;
double variable_2;

private:
int *variable_3;
}

Then I create four objects:
example_class *A;
example_class *B;
example_class *C;
example_class *D;

And link them togother as a linked list: A-B-C-D.
Then I create a new object E and want to insert E before C.
Below is what I do:

example_class *E;

E->s1.next_element = C;
E->s1.prev_element = B;
C->s1.prev_element = &(E->s1.next_elemet);
B->s1.next_element = &(E->s1.next_element);

Are above operation correct?
No. Aside from the typo, you''re assigning a "pointer to pointer to
example_class" to a "pointer to example_class." Your compiler should be
telling you about this.

For the last two sentences, I can also
write:
C->s1.prev_element = E;
B->s1.next_element = E;



That''s much better.

I think &(E->s1.next_elemet)= E, am I right?



No, those pointers have different types.



Thanks for responding.
What I want to know is the entry address of object E.
Since E->s1.next_element is the first memeber variable, is the
address of E->s1.next_element same as the address of E?
Or is E->s1.next_element located in the same memory location as E?
Can I say that (example_class *)(&(E->s1.next_element))== E ?

--------------------------------------
Below is another situation:
I modify the defination of example_class to be:

class example_class{

public:
example_class();
void funtion_1();
void function_2();

double new_variable1; //I add two variables
int new_variable2; // in front of the strust st.
protected:
struct st {
example_class *next_element;
example_class *prev_element;
} s1;

int variable_1;
double variable_2;

private:
int *variable_3;
}

After I add two new variables in front of struct st, can I still do
the same operation as above example?



No, you still can''t do it.

That is:

With a linked list: A-B-C-D same as above.
Then I create a new object E and want to insert E before C.
Below is what I do:

example_class *E;

E->s1.next_element = C;
E->s1.prev_element = B;
C->s1.prev_element = &(E->s1.next_elemet);
B->s1.next_element = &(E->s1.next_element):

Are the above operation orrect?



No.

With two new variables added in front of struct st, can I still
say that &(E->s1.next_elemet)=E ?



No.

As long as there is variable infront of struct st, I can not
say &(E->s1.next_elemet)=E ?



No, even if those pointers contain the same address (I''m not sure
whether that''s required), you''ve got a type mismatch. By the way,
that''s the fifth time you''ve made the same typo... Am I missing something?



After adding two variable in front of struct st, has the entry address
of object E been changed? The entry address of object E is the
address of E->s1.next_element or the address of new_variable1?

The following two expression:
(example_class *)(&(E->s1.next_element))== E
(example_class *)(&new_variable1)== E

which one is correct?

Thanks a lot.

Jack


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

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