指向列表中元素的指针 VS 元素本身
Pointer to the element in the list VS element itself
我在 Internet 上找到了这段代码,需要一些帮助。
这是代码:
#include<iostream>
using namespace std;
/* Linked list structure */
struct list {
struct list *prev;
int data;
struct list *next;
} *node = NULL, *first = NULL, *last = NULL, *node1 = NULL, *node2 = NULL;
class linkedlist {
public:
/* Function for create/insert node at the beginning of Linked list */
void insert_beginning() {
list *addBeg = new list;
cout << "Enter value for the node:" << endl;
cin >> addBeg->data;
if(first == NULL) {
addBeg->prev = NULL;
addBeg->next = NULL;
first = addBeg;
last = addBeg;
cout << "Linked list Created!" << endl;
}
else {
addBeg->prev = NULL;
first->prev = addBeg;
addBeg->next = first;
first = addBeg;
cout << "Data Inserted at the beginning of the Linked list!" << endl;
}
}
我不明白的是,当他创建新的 Node 对象时(在本例中为 addBeg),他在其前面放置了一个指针运算符。以及我现在的看法,不应该在对象名称和数据之前创建没有“*”的对象,指向下一个的指针和指向前一个的指针,这与指向列表中节点的指针不同应该只包含节点的地址,没有任何其他数据?如果不是这种情况,那么指向列表中节点的指针与节点本身的区别是什么?
代码中的做法是正确的。您理解错误,无法通过指向该节点的指针访问该节点的数据。
如果addBeg
是指向new list
返回的节点的指针,那么可以使用运算符->
:
访问该节点的数据
list.data
等同于 addBeg->data
.
If that's not the case, then what is the thing that differs the pointer to the node in the list from the node itself ?
=> addBeg
是指向对象的指针,该对象由 new List
返回。
您没有正确解释此 C++ 声明...代码的含义
list *addBeg = new list;
是
list* addBeg;
addBeg = new list;
list*
换句话说就是addBeg
的类型。
请注意,规则确实很奇怪,因为虽然 *
在逻辑上附加到第一个 list
以形成类型,但
的含义
list *a, b;
将声明 a
为 a "pointer to list" 而 b 为 "instance of list" (因此含义附加到 list
但星号本身附加到a
).
一些程序员在这方面走得很远,并且:
- 他们总是把星号附加到类型上(在左边)
- 他们从不在同一个结构中声明两个变量
根据我的经验,在编写足够多的代码后,这个解释问题将会消失,甚至 C/C++ 声明的疯狂语法规则也将变得合理且易于阅读(至少在简单的情况下)。
我在 Internet 上找到了这段代码,需要一些帮助。 这是代码:
#include<iostream>
using namespace std;
/* Linked list structure */
struct list {
struct list *prev;
int data;
struct list *next;
} *node = NULL, *first = NULL, *last = NULL, *node1 = NULL, *node2 = NULL;
class linkedlist {
public:
/* Function for create/insert node at the beginning of Linked list */
void insert_beginning() {
list *addBeg = new list;
cout << "Enter value for the node:" << endl;
cin >> addBeg->data;
if(first == NULL) {
addBeg->prev = NULL;
addBeg->next = NULL;
first = addBeg;
last = addBeg;
cout << "Linked list Created!" << endl;
}
else {
addBeg->prev = NULL;
first->prev = addBeg;
addBeg->next = first;
first = addBeg;
cout << "Data Inserted at the beginning of the Linked list!" << endl;
}
}
我不明白的是,当他创建新的 Node 对象时(在本例中为 addBeg),他在其前面放置了一个指针运算符。以及我现在的看法,不应该在对象名称和数据之前创建没有“*”的对象,指向下一个的指针和指向前一个的指针,这与指向列表中节点的指针不同应该只包含节点的地址,没有任何其他数据?如果不是这种情况,那么指向列表中节点的指针与节点本身的区别是什么?
代码中的做法是正确的。您理解错误,无法通过指向该节点的指针访问该节点的数据。
如果addBeg
是指向new list
返回的节点的指针,那么可以使用运算符->
:
list.data
等同于 addBeg->data
.
If that's not the case, then what is the thing that differs the pointer to the node in the list from the node itself ?
=> addBeg
是指向对象的指针,该对象由 new List
返回。
您没有正确解释此 C++ 声明...代码的含义
list *addBeg = new list;
是
list* addBeg;
addBeg = new list;
list*
换句话说就是addBeg
的类型。
请注意,规则确实很奇怪,因为虽然 *
在逻辑上附加到第一个 list
以形成类型,但
list *a, b;
将声明 a
为 a "pointer to list" 而 b 为 "instance of list" (因此含义附加到 list
但星号本身附加到a
).
一些程序员在这方面走得很远,并且:
- 他们总是把星号附加到类型上(在左边)
- 他们从不在同一个结构中声明两个变量
根据我的经验,在编写足够多的代码后,这个解释问题将会消失,甚至 C/C++ 声明的疯狂语法规则也将变得合理且易于阅读(至少在简单的情况下)。