简体   繁体   English

打印双向链表-无结果

[英]print doubly linked list - no result

I am new to c++ and programming in general. 我是C ++和一般编程的新手。 I am trying to implement a doubly linked list. 我正在尝试实现双向链表。 I think the list is created successfully, but I am having trouble printing the list out entirely. 我认为该列表创建成功,但是我无法完全打印出该列表。 Can you please let me know what's wrong with my printListForward method below? 您能否让我知道下面的printListForward方法出了什么问题? My code is not complete yet. 我的代码尚未完成。 Would really appreciate any tips and suggestions as well. 真的很感谢任何提示和建议。

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

}

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

Just a few comments. 只是一些评论。 First you want to use proper indents. 首先,您要使用适当的缩进。 For beginner it is important to learn to write a simple Makefile. 对于初学者来说,学习编写简单的Makefile很重要。 In your case, I wrote one for you. 就您而言,我为您写了一封。

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)

For your source code I did simple editing and named your file: doublelink.cpp: 对于您的源代码,我进行了简单的编辑,并将文件命名为: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;
}

With both file in the directory, you type 将两个文件都放在目录中,然后键入

make 使

a binary file doublelink will appear in your directory. 二进制文件双链接将出现在您的目录中。
you run this program by typing its name: 您通过键入其名称来运行该程序:

$ doublelink 1 2 3 4 5 6 7 8 9 9 $ doublelink 1 2 3 4 5 6 7 8 9 9

But with all these efforts. 但是经过所有这些努力。 You should never need to implement double linked list. 您永远不需要实现双链表。 You should use the C++ standard library and customize the data type for your purpose. 您应该使用C ++标准库并根据需要自定义数据类型。 The std::list is implemented as double linked list. std :: list被实现为双链表。 Please read the document at http://www.cplusplus.com/reference/list/list/ . 请阅读http://www.cplusplus.com/reference/list/list/上的文档。 You should create your structure of interest by 您应该通过以下方式创建您感兴趣的结构

list<MagicSquare> myfancySquareList;

myfancySquareList.push_back(MagicSquare(somevalue));

Your double linked list is also missing the destructor, and you are having memory leak. 您的双链表也缺少析构函数,并且内存泄漏。 There are many other missing things from your implementation which is usually covered by a text book of several hundred pages. 您的实现中还有许多其他遗漏的内容,通常包含数百页的教科书。 Hope this get you started. 希望这可以帮助您入门。 When you have trouble, you can run your program in debug mode: gdb doublelink. 遇到问题时,可以在调试模式下运行程序:gdb doublelink。 You can step through it and figure out where is your problem. 您可以逐步解决问题,并找出问题所在。 Your initial problem is a segmentation fault. 您最初的问题是细分错误。 Try to run your original program and see where it terminate. 尝试运行您的原始程序,并查看其终止位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM