C++双链表反向打印

C++ Double Linked List Reverse Print

本文关键字:打印 链表 C++      更新时间:2023-10-16

我正在尝试编写一个反向打印函数作为双向链表的一部分。 以下是我编写的相关函数:

void PLAYER::AddNode(int addID, std::string addName){
nodePtr n = new node; //creates a new node pointer
n->next = NULL;       //Make next null
n->prev = NULL;      // this will set this to be the ending node
n->ID = addID;      //These two lines pass the information into the node
n->name = addName;  // ID# and Name Information
if(head != NULL){  // This checks to see if a list is set up.
    curr = head;    // Make this point to the head.
    while(curr->next != NULL){ // Loops through until the NULL is found
        curr = curr->next;
    }
    curr->next = n; //Make the currnet node point to N
    n->prev = curr; //Make the previous node connect to curr
    n->next = tail; // connect new node to the tail.
}
else{
    head = n;   //If there is no list, this makes N the first node.
}

下面是对要使用的函数进行原型设计的类。

class PLAYER
{
public:  // Functions go inside PUBLIC
    PLAYER();
    void AddNode(int addID, std::string addName);
    void DeleteNode(int delPlayer);
    void SortNode();
    void PrintList();
    void InsertHead(int AddID, std::string addName);
    void PrintReverse();
private:  //Variables go into here
   typedef struct node{
            // ...
            std::string name;
            int ID;
            node* next;
            node* prev;
    }* nodePtr;

    nodePtr head, curr, temp, prev, test, tail;

};

最后,我尝试创建一个反向遍历函数来向后打印。

void PLAYER::PrintReverse()
{
    curr = head; 
    while(curr->next != NULL) //Get to the end of the list
    {
        curr = curr->next;
    }
    while(curr->prev != NULL)    //Work backward and print out the contents
    {
        std::cout << curr->ID << " " << curr->name << endl;
        curr = curr->prev;
    }
}
我想做的是在 PrintReverse() 函数

中通过尾部指针对其进行初始化,但是我无法弄清楚要添加到 PrintReverse() 和 AddNode() 中的函数,以便让新节点由尾部指向。

这是我在这里发布的第一个问题,我希望我涵盖了我所有的基础。 谢谢你给我找到的任何帮助。

编辑:

感谢您的所有投入。 我正在重新学习数据结构,是的,这是我自己强加的一些家庭作业,以开始让逻辑再次流动。

今晚回家后,我会做出改变。

需要考虑以下更改。

PrintReverse函数不需要前向传递即可获取tail

void PLAYER::PrintReverse()
{
    curr = tail; 
    while(curr != NULL)    //Work backward and print out the contents
    {
        std::cout << curr->ID << " " << curr->name << endl;
        curr = curr->prev;
    }
}

AddNode函数中如何处理尾部存在问题。请参阅注释包含[CHANGED][ADDED]的行:

if(head != NULL){  // This checks to see if a list is set up.
    curr = head;    // Make this point to the head.
    while(curr->next != NULL){ // Loops through until the NULL is found
        curr = curr->next;
    }
    curr->next = n; //Make the currnet node point to N
    n->prev = curr; //Make the previous node connect to curr
    n->next = NULL; // [CHANGED]: we want the last node not to have a successor.
}
else{
    head = n;   //If there is no list, this makes N the first node.
}
tail = n;       // [ADDED]: the last node added is the new tail. 

但是,更简单的解决方案是再次避免向前传递,并从尾部开始。

if(tail != NULL){  // This checks to see if a list is set up.
    tail->next = n; //Make the old tail node point to N
    n->prev = tail; 
    n->next = NULL; 
}
else{
    head = n;   //If there is no list, this makes N the first node.
}
tail = n;       // The last node added is the new tail.