0

I'm trying to store a list of names in alphabetical order, while implementing this linked list, but I've bumped into some errors. I have my problems (like 'node' no appropriate default constructor, and 'first' undeclared identifier). This is what I have:

Node.h
class node
{public:
node(node* , node* , node* , node* , node* , node* , node* , node* , node* , node*);

private:
string data;node* previous;node* next;
friend class list;
friend class iterator;};

List.h
# include <string>
# include "iterator.h"
# include<iostream>
using std::string;
class list
{public:
list();void push_back();
iterator begin();
iterator end();

private:
node* first; node* last;};

# include "node.h"
using std::string;
node::node(node* , node* , node* , node* , node* , node* , node* , node* , node* , node*)
{node* t1 = new node();
t1->previous = NULL;
t1->data = "mark";
t1->next = NULL;
first = t1;

node *t2 = new node();
t2->previous = t1;
t2->data = "ray";
t2->next = NULL;
t1->next = t2;

node *t3 = new node();
t3->previous = t2;
t3->data = "cst";
t3->next = NULL;
t2->next = t3;

node *t4 = new node();
t4->previous = t3;
t4->data = "rat";
t4->next = NULL;
t3->next = t4;

node *t5 = new node();
t5->previous = t4;
t5->data = "pig";
t5->next = NULL;
t4->next = t5;

node *t6 = new node();
t6->previous = t5;
t6->data = "rex";
t6->next = NULL;
t5->next = t6;

node *t7 = new node();
t7->previous = t6;
t7->data = "pen";
t7->next = NULL;
t6->next = t7;

node *t8 = new node();
t8->previous = t7;
t8->data = "sheep";
t8->next = NULL;
t7->next = t8;

node *t9 = new node();
t9->previous = t8;
t9->data = "sun";
t9->next = NULL;
t8->next = t9;

node *t10 = new node();
t10->previous = t9;
t10->data = "Dog";
t10->next = NULL;
t9->next = t10;
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by NicAx64
0
# include "node.h"
using std::string;
node::node(node* , node* , node* , node* , node* , node* , node* , node* , node* , node*)
{node* t1 = new node();
t1->previous = NULL;
t1->data = "mark";
t1->next = NULL;
first = t1;

node *t2 = new node();
t2->previous = t1;
t2->data = "ray";
t2->next = NULL;
t1->next = t2;

node *t3 = new node();
t3->previous = t2;
t3->data = "cst";
t3->next = NULL;
t2->next = t3;

node *t4 = new node();
t4->previous = t3;
t4->data = "rat";
t4->next = NULL;
t3->next = t4;

node *t5 = new node();
t5->previous = t4;
t5->data = "pig";
t5->next = NULL;
t4->next = t5;

node *t6 = new node();
t6->previous = t5;
t6->data = "rex";
t6->next = NULL;
t5->next = t6;

node *t7 = new node();
t7->previous = t6;
t7->data = "pen";
t7->next = NULL;
t6->next = t7;

node *t8 = new node();
t8->previous = t7;
t8->data = "sheep";
t8->next = NULL;
t7->next = t8;

node *t9 = new node();
t9->previous = t8;
t9->data = "sun";
t9->next = NULL;
t8->next = t9;

node *t10 = new node();
t10->previous = t9;
t10->data = "Dog";
t10->next = NULL;
t9->next = t10;
}

you just add the elements one by one.

make the Node constructor just default Node().
and another suggestion read the C++ coding standards a bit.
( specially the naming standards of variables , how to write beautiful
code , node is C style use Node ).

and you'r node class only contains a pointer to the next node. if that
node is the last node then that pointer will be 0 or NULL.

implement it like this.

class node
{public:

Node* previous;
Node* next;
       Node(Node * ptrPreviousNode  )
       {
             previous = ptrPreviousNode ;
             next = NULL ;
       }
       
       void InsertNode ( Node * ptrNext)
       {
            if ( next == NULL )
                 next = ptrNext ;
            else 
                 next->InsertNode ( ptrNext);
       }        
private:
string data;

friend class list;
friend class iterator;
};

so the above red marked code will insert an element to the last
. Give it a try and modify it to check and add according to the
alphabetical order.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.