C++ 指向列表中元素的指针与元素本身

C++ 指向列表中元素的指针与元素本身,c++,doubly-linked-list,C++,Doubly Linked List,我在网上找到了这个代码,我需要一些帮助。 代码如下: #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 link

我在网上找到了这个代码,我需要一些帮助。 代码如下:

#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;
        }
    }
#包括
使用名称空间std;
/*链表结构*/
结构列表{
结构列表*prev;
int数据;
结构列表*下一步;
}*node=NULL、*first=NULL、*last=NULL、*node1=NULL、*node2=NULL;
类链接列表{
公众:
/*用于在链表开头创建/插入节点的函数*/
无效插入_开头(){
list*addBeg=新列表;
无法添加数据;
if(first==NULL){
addBeg->prev=NULL;
addBeg->next=NULL;
第一个=addBeg;
last=addBeg;
cout-prev=addBeg;
addBeg->next=第一;
第一个=addBeg;

cout代码中的操作方式是正确的。您的理解不正确,因为无法通过指向该节点的指针访问该节点的数据

如果
addBeg
是指向
new list
返回的节点的指针,则可以使用操作符
->
访问该节点的数据:

list.data
相当于
addBeg->data

如果不是这样,那么指向列表中节点的指针与节点本身有什么不同?


>>代码> AddiBug <代码>是指向对象的指针,该对象由<代码>新列表返回。

< P>您没有正确解释此C++声明…代码

的含义
 list *addBeg = new list;

list*
换句话说就是
addBeg
的类型

请注意,这些规则确实很奇怪,因为当
*
逻辑上附加到第一个
列表
以形成类型时

list *a, b;
将声明
a
为“指向列表的指针”,而b为“列表的实例”(因此含义附加到
list
,但星形本身附加到
a

一些程序员在这方面做得相当多,并且:

  • 他们总是把星星附在字体上(在左边)
  • 它们从不在同一构造中声明两个变量
  • 根据我的经验,在编写了足够多的代码之后,这个解释问题将消失,甚至C/C++声明的疯狂语法规则也将相当容易阅读(至少在简单的情况下)

    list *a, b;