Click here to Skip to main content
15,891,940 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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)
C++
 #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;
        }
}

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

};
Posted
Updated 24-Nov-18 11:03am
v3

1 solution

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

C++
// (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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900