我目前正在学习如何使用链表,特别是双向链表,当我尝试向后打印时,我的程序遇到了问题。
以下是我需要帮助的代码部分:
#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 函数中,我正在修改开始和结束,但我只能返回一个或另一个。这意味着无论我不返回哪个,在 main 函数中仍然是 NULL,因此不会指向任何内容。我尝试将开始/当前/结束设置为不等于 NULL,但如果我这样做,createList 将无法正常工作。
有没有人对我如何修改两者有任何想法?需要明确的是,列表必须在函数中创建,只需在主要中初始化它就非常容易了。
谢谢特里斯坦
你的问题是你正在复制指针,而你应该通过引用传递它们,即使用指针到指针或引用到指针,而不仅仅是复制指针最初指向的值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, ¤t, &end);
并做最后的分配,begin
createList
的功能内部,而不是main
。