我如何循环双链表? [英] How do I circular doubly linked list?

查看:69
本文介绍了我如何循环双链表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个循环的双向链表。这是我的代码。它不打印任何东西。我认为If,Else语句在insertAfterLast函数中有一些问题。我不知道两个小时。我应该使用while循环来搜索最后一个节点吗?



我尝试了什么:



I tried to make a circular doubly linked list.Here is my code.It doesn't print anything. I think "If, Else" statement, which is in insertAfterLast function, have some problem. I don't figure out for two hours. should I use "while" loop for searching last node?

What I have tried:

#ifndef CIRCULARDLL_H
#define CIRCULARDLL_H
#include <iostream>
#include "Node.h"
class CircularDLL
{
private:
    NodePtr  top;

public:
    void insertDataFromFile();
    void print();
    void insertAfterLast(int id, string name, string email, int age);
};





----------------------- ------------------------

(circularDLL.cpp)



-----------------------------------------------
(circularDLL.cpp)

 #include "CircularDLL.h"

 void Circular::insertAfterLast(int id, string name, string email, int age)
{
    NodePtr *newNode= new Node;
    newNode->stId = id;
    newNode->stName = name;
    newNode->stEmail = email;
    newNode->stAge = age;

        if (top == NULL)
        {
            top = newNode;
            return;
        }


        if (top->before == top) // Only one node in the list
        {
            top->next = newNode;
            top->before = newNode;
            newNode->next = top;
            newNode->before = top;
        }
        else
        {
            newNode->before= top->before;
                     top->before->next = before;
                    top->before= before;
                    before->next = top;
        }
}



--------------------- --------------------------


-----------------------------------------------

#ifndef NODE_H
#define NODE_H

typedef Node* NodePtr;

class Node
{
    friend class CircularDLL;

private:
    int stId;
    string stName;
    string stEmail;
    int stAge;
    NodePtr next;
    NodePtr before;

};

推荐答案

你错过了一些初始化的东西。而且,对我来说,节点插入的代码看起来不太正确。试试



You missed some initialization stuff. Moreover, your code for node insertion doesn't look quite correct, to me. Try

// (circ.hpp)
#ifndef CIRCULARDLL_H
#define CIRCULARDLL_H
#include <string>

class Node
{
  friend class CircularDLL;

private:

  void print();
  int stId;
  std::string stName;
  std::string stEmail;
  int stAge;
  Node * next;
  Node * prev;
};

typedef Node* NodePtr;

class CircularDLL
{
private:
  NodePtr  top;

public:
  CircularDLL():top(nullptr){}
//  void insertDataFromFile();
  void print();
  void printBackward();
  void insertAfterLast(int id, const std::string & name, const std::string & email, int age);
};
#endif // CIRCULARDLL_H




// (circ.cpp)
#include "circ.hpp"
#include <iostream>

void Node::print()
{
  std::cout << "{" << stId << ", " << stName << ", " << stEmail << ", " << stAge << "}\n";
}

void CircularDLL::insertAfterLast(int id, const std::string & name, const std::string & email, int age)
{
  NodePtr newNode = new Node;
  newNode->stId = id;
  newNode->stName = name;
  newNode->stEmail = email;
  newNode->stAge = age;

  if ( ! top )
  {
    top = newNode;
    top->next = top;
    top->prev = top;
  }
  else
  {
    NodePtr np = top;
    while ( np->next != top)
    {
      np = np->next;
    }
    np->next = newNode;
    newNode->prev = np;
    newNode->next = top;
    top->prev = newNode;
  }
}

void CircularDLL::print()
{
  if ( ! top )
  {
    std::cout << "(empty)\n";
    return;
  }

  NodePtr np = top;
  do
  {
    np->print();
    np = np->next;
  } while (np != top);
}

void CircularDLL::printBackward()
{
  if ( ! top )
  {
    std::cout << "(empty)\n";
    return;
  }
  NodePtr np = top->prev;
  do
  {
    np->print();
    np = np->next;
  } while ( np != top->prev);
}

int main()
{
  CircularDLL cd;
  cd.insertAfterLast( 1, "foo", "foo@foo.com", 42 );
  cd.insertAfterLast( 3, "bar", "bar@bar.com", 24 );
  cd.insertAfterLast( 11, "goo", "goo@goo.com", 2448 );

  std::cout << "printing with forward iteration\n";
  cd.print();
  std::cout << "printing with backward iteration\n";
  cd.printBackward();
  std::cout << std::endl;
}


这篇关于我如何循环双链表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆