Nov 16, 2011 at 6:47pm UTC
Hi im trying to create a circular linked list in which after the first entries all subsequent entries are inputed after the first. Compiler errors are occuring on the lines where im trying to create a new Node (ie Node::Circlist). Am i doing this wrong? can i create more objects from the nested class within one object of the primary class?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
class Circlist
{ public :
Circlist(); // default constructor
void addnode(string sx); // adds name sx to the circle
string select(int n); // removes every nth, returns winner
void display( ) const ; // displays contents of list
private :
class Node;
Node* start;
};
class Circlist::Node
{ public :
Node(string sx) : s(sx), link(NULL) { }
string s;
Node* link;
};
void Circlist::addnode(string name)
{
Node* p;
Node* temp;
if (start == NULL)
{start = new Node;
start -> s = name;
start -> link = start;
}
else if (start -> link == start)
{
start -> link = new Node;
p = start -> link;
p -> sx = name;
p -> link = start;
}
else
temp = start -> link;
start -> link = new Node;
p = start -> link;
p -> link = temp;
p -> sx = name;
}
Last edited on Nov 16, 2011 at 8:21pm UTC
Nov 16, 2011 at 9:26pm UTC
Your constructor requires a string param.
1 2 3 4 5 6 7 8 9 10
void Circlist::addnode(string name)
{
Node* p;
Node* temp;
if (start == NULL)
{start = new Node(name); // pass name to constructor
start -> link = start;
}
...
Last edited on Nov 16, 2011 at 9:31pm UTC
Nov 16, 2011 at 9:28pm UTC
changed the
start = new Node;
to
start = new Node(sx);
the whole way through. Don't really know why that compiled? Is it because the constructor of Node required a string argument i.e sx?
Nov 16, 2011 at 9:57pm UTC
cheers andy. Much appreiciated.