打印双链表

时间:2014-04-02 03:25:32

标签: 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 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;
}

此程序的目的是创建一个列表,向前打印,向后打印,插入元素,擦除元素,然后销毁列表。我把它砍成了创建,向前打印和向后打印功能。

我遇到的问题是在createList函数中我修改了开始和结束但我只能返回一个或另一个。这意味着我在主函数中返回的任何一个仍然是NULL,因此不指向任何东西。我已经尝试将begin / current / end设置为不等于NULL但是如果我这样做,createList将无法工作。

有没有人对如何修改两者有任何想法?为了清楚起见,在函数中创建了 HAS TO 列表,在main中初始化它将非常容易。

谢谢, 特里斯坦

1 个答案:

答案 0 :(得分:1)

你的问题是你正在复制指针,当你应该通过引用传递它们时,即使用指针指针或引用指针而不是仅仅复制指针{{ 1}}最初指向。根据您正在执行的操作,您无法修改main中声明的原始指针变量...传递引用将允许您执行此操作同时保留所有列表在你的功能中设置代码。

例如,改变

main

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

然后确保在功能正文中考虑额外的解除引用

最后,你会这样称呼:

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

最后分配到createList(maxLoop, &begin, &current, &end); 函数正文中的begin,而不是createList