0

我是 C++ 和一般编程的新手。我正在尝试实现一个双向链表。我认为该列表已成功创建,但我无法完全打印该列表。你能告诉我下面的 printListForward 方法有什么问题吗?我的代码还没有完成。也非常感谢任何提示和建议。

#include "MagicSquare.hpp"
#include <iostream>

class MagicSquaresList{

private:

    struct MagicSquaresNode{

        int nodeIndex;
        MagicSquaresNode *pleft;
        MagicSquaresNode *pright;
        MagicSquaresNode *pup;
        MagicSquaresNode *pdown;
    };

    MagicSquaresNode *head;
    MagicSquaresNode *tail;

public:
    MagicSquaresList (){ 
        head = NULL;
        tail = NULL;
    }

    int getListLength(){
        int length = 1;
        MagicSquaresNode *temp = new MagicSquaresNode;
        temp = head;

        if(isEmpty()){
            return 0;
        }else{
            while(temp != tail){
                length++;
                temp = temp->pright;
            }
        }
        return length;
    }

    bool isEmpty(){
        return head == NULL;
    }

    void appendToEnd(int val){
        MagicSquaresNode *newNode = new MagicSquaresNode;
        newNode->nodeIndex = val;

        if(isEmpty()){
            tail = newNode;
        } else {
            tail->pright = newNode;
            newNode->pleft = tail;
        }

        tail = newNode;
    }

    void printListForward() {
        MagicSquaresNode *ptr = head;

        while(ptr != tail){
            std::cout << ptr->nodeIndex << " ";
            ptr = ptr->pright;
        }

        std::cout << std::endl;
    }

};


int main(){

    /*********** temporary *****************/
    int matrixSize, listSize;
    matrixSize = 3;
    listSize = matrixSize * matrixSize;
    /****************************************/

    MagicSquaresList list1;

    for (int i = 1; i <= listSize; i++){
        list1.appendToEnd(i);
    }

    list1.printListForward();
    std::cout << list1.getListLength() << std::endl;
    return 0;

}
4

2 回答 2

1

You need to set the head.

  void appendToEnd(int val){
    MagicSquaresNode *newNode = new MagicSquaresNode;
    newNode->nodeIndex = val;

    if(isEmpty()){
        tail = newNode;
        head = newNode;
    } else {
        tail->pright = newNode;
        newNode->pleft = tail;
    }

    tail = newNode;
    }
于 2016-09-29T04:20:11.977 回答
0

只是一些评论。首先,您要使用适当的缩进。对于初学者来说,学习编写一个简单的 Makefile 很重要。在你的情况下,我为你写了一个。

生成文件:

  1 bin_PROGRAMS=doublelink
  2 GCCLIBDIR= /usr/local/lib64
  3 CXXFLAGS=-g -std=c++11
  4 CC=g++   
  5 LDFLAGS=-L$(GCCLIBDIR)
  6          
  7 all : $(bin_PROGRAMS)
  8 
  9 doublelink : doublelink.o
 10    $(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

对于您的源代码,我进行了简单的编辑并将您的文件命名为:doublelink.cpp:

//#include "MagicSquare.hpp"
#include <iostream>

using namespace std;

class MagicSquaresList{
   private:
      struct MagicSquaresNode {
         MagicSquaresNode(int ni) : nodeIndex(ni), pleft(0), pright(0), pup(0), pdown(0) { }
         int nodeIndex;
         MagicSquaresNode *pleft;
         MagicSquaresNode *pright;
         MagicSquaresNode *pup;
         MagicSquaresNode *pdown;
      };

      MagicSquaresNode *head;
      MagicSquaresNode *tail;

   public:
      MagicSquaresList () { 
         head = 0;
         tail = 0;
      }

      int getListLength(){
         MagicSquaresNode *temp = head;
         if (temp == 0) {
            return 0;
         }
         int length = 0;
         while (temp != 0) {
            ++length;
            temp = temp->pright;
         }
         return length;
      }

      bool isEmpty(){
         return head == 0;
      }

      void appendToEnd(int val){
         MagicSquaresNode *newNode = new MagicSquaresNode(val);
         if (tail == 0) {
            head = newNode;
         } 
         else {
            tail->pright = newNode;
            newNode->pleft = tail;
         }
         tail = newNode;
      }

      void printListForward() {
         MagicSquaresNode *ptr = head;
         while (ptr != 0) {
            //cout << ptr << endl;
            std::cout << ptr->nodeIndex << " ";
            ptr = ptr->pright;
         }
         std::cout << std::endl;
      }

};


int main(){
    /*********** temporary *****************/
    int matrixSize, listSize;
    matrixSize = 3;
    listSize = matrixSize * matrixSize;
    /****************************************/

    MagicSquaresList list1;

    for (int i = 1; i <= listSize; i++){
        list1.appendToEnd(i);
    }

    list1.printListForward();
    std::cout << list1.getListLength() << std::endl;
    return 0;
}

在目录中的两个文件中,您键入

制作

一个二进制文件双链接将出现在您的目录中。
你通过输入它的名字来运行这个程序:

$双链接 1 2 3 4 5 6 7 8 9 9

但是通过所有这些努力。您永远不需要实现双链表。您应该使用 C++ 标准库并根据您的目的自定义数据类型。std::list 被实现为双链表。请阅读http://www.cplusplus.com/reference/list/list/上的文档。您应该通过以下方式创建您感兴趣的结构

list<MagicSquare> myfancySquareList;

myfancySquareList.push_back(MagicSquare(somevalue));

您的双链表也缺少析构函数,并且您有内存泄漏。您的实现中还有许多其他缺失的东西,通常包含在数百页的教科书中。希望这能让你开始。当你遇到问题时,你可以在调试模式下运行你的程序:gdb doublelink。您可以逐步解决问题并找出问题所在。您最初的问题是分段错误。尝试运行您的原始程序并查看它终止的位置。

于 2016-09-29T05:39:13.163 回答