打印双向链表 - 没有结果

时间:2016-09-29 03:59:50

标签: c++ doubly-linked-list magic-square

我是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;

}

2 个答案:

答案 0 :(得分:1)

你需要设置头部。

  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;
    }

答案 1 :(得分: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;
}

将两个文件都放在目录中,然后键入

二进制文件双链接将出现在您的目录中 您通过键入其名称来运行此程序:

$ doublelink 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。您可以单步执行它并找出问题所在。您最初的问题是分段错误。尝试运行原始程序并查看它终止的位置。