C++ 打印双链接列表

C++ 打印双链接列表,c++,list,linked-list,C++,List,Linked List,我目前正在学习如何使用链表,特别是双链表,当我试图反向打印程序时,遇到了一个问题 以下是我需要帮助的代码部分: #include <iostream> using namespace std; struct node { int data; //int to store data in the list node *next; //pointer to next value in list node *prev; //pointer to previous

我目前正在学习如何使用链表,特别是双链表,当我试图反向打印程序时,遇到了一个问题

以下是我需要帮助的代码部分:

#include <iostream>

using namespace std;

struct node
{
    int data; //int to store data in the list
    node *next; //pointer to next value in list
    node *prev; //pointer to previous value in list
};

node *appendList(node *current, int newData) //Function to create new nodes in the list
{
    node *newNode; //create a new node
    newNode = new node;
    newNode->data = newData; //Assign data to it
    newNode->next = NULL; //At end of list so it points to NULL
    newNode->prev = current; //Link new node to the previous value
    current->next = newNode; //Link current to the new node
    return newNode; //return the new node
}

node *createList(int maxLoop, node *begin, node *current, node *end) //Function to create list
{
    //Allocate the starting node
    current = new node;
    current -> data = 1; //First data value is 1
    current -> next = NULL; //next value is NULL
    current -> prev = NULL; //previous value is NULL
    begin = current; //This is the beginning of the list

    for (int count = 2; count <= maxLoop; count++) //Loop to fill the list
    {
        current = appendList(current, count*count); //Create new nodes and fill with square numbers
    }
    end = current; //Now we are at the end of the list
    return begin; //Return begin, this is the problem; I can't return end as well
}

void printForward (node *p) //Function to print the list forwards
{
    node *curr = p; //current is the beginning value of the list
    while (curr != NULL) //Continue while current is not at the end of the list
    {
        cout << curr->data << " "; //Print out the data of current
        curr = curr->next; //Move one value along in the list
    }
}

void printBackward (node *p) //Function to print the list backwards
{
    node *curr = p; //current is the end value of the list
    while (curr != NULL) //Continue while current is not at the beginning of the list
    {
        cout << curr->data << " "; //Print out the data of current
        curr = curr->prev; //Move one value back in the list
    }
}

int main()
{
    //Initialize current, begin, and end
    node *current = NULL; 
    node *begin = NULL;
    node *end = NULL;
    int maxLoop = 10; //The number of items in the list

    cout << "The list has now been created." << endl;
    begin = createList(maxLoop, begin, current, end); //function to create the list
    cout << "Printed forwards, this list is: ";
    printForward(begin); //Function to print the list forwards
    cout << endl;
    cout << "Printed backwards, this list is: ";
    printBackward(end); //Function to print the list backwards
    cout << endl;
    return 0;
}
#包括
使用名称空间std;
结构节点
{
int data;//在列表中存储数据的int
node*next;//指向列表中下一个值的指针
node*prev;//指向列表中上一个值的指针
};
node*appendList(node*current,int newData)//用于在列表中创建新节点的函数
{
node*newNode;//创建一个新节点
newNode=新节点;
newNode->data=newData;//为其分配数据
newNode->next=NULL;//位于列表末尾,因此它指向NULL
newNode->prev=current;//将新节点链接到上一个值
当前->下一步=新节点;//将当前链接到新节点
return newNode;//返回新节点
}
node*createList(int-maxLoop,node*begin,node*current,node*end)//创建列表的函数
{
//分配起始节点
当前=新节点;
当前->数据=1;//第一个数据值为1
当前->下一个=空;//下一个值为空
当前->上一个=NULL;//上一个值为NULL
begin=current;//这是列表的开头

对于(int count=2;count您的问题是您正在复制指针,而您应该通过引用传递它们,即,使用指向指针的指针或指向指针的引用,而不仅仅是复制
main
中指针最初指向的值。您所做的是无法修改原始指针变量that是在
main
中声明的。通过引用传递将允许您执行此操作,同时将所有列表设置代码保留在您的函数中

比如说,改变

node* createList(int maxLoop, node *begin, node *current, node *end)

然后确保在函数体中考虑额外的解引用

最后,您可以这样称呼它:

createList(maxLoop, &begin, &current, &end);

createList
的函数体中,而不是在
main
中,将最后一个赋值给
begin

您可以通过引用获取指针(node*&begin)。我理解您所说的函数中额外的解引用是什么意思,但我该如何去做呢?特别是对于“current=new node;”我会说“*current=new node;”还是“current=new*node;”或者完全是别的什么?它应该是
*current=new node;
,因为
current
现在是指向调用方堆栈上的原始指针的指针,指向
createList
函数
createList(maxLoop, &begin, &current, &end);