Problem with list

G

Gaijinco

I'm doing a template class to make an ordered list.

I created a class Person to make an agenda.

When I create a new person, there's no problem but when I try to add
it to the list it throws a lot of errors.

Can someboy help me! Thanks!

#include <iostream>
#include <string>

template <typename T>
class List
{
class Node
{
T data;
Node* next;
public:
Node(T data)
{
this->data = data;
next = (Node*)NULL;
}
T getData() const
{
return data;
}
Node* getNext() const
{
return next;
}
void setData(T data)
{
this->data = data;
return;
}
void setNext(Node* next)
{
this->next = next;
return;
}
};

Node* head;
size_t nodes;

public:
List()
{
head = (Node*)NULL;
nodes = 0;
}

void insert(T data)
{
Node* p = new Node(data);

if(p!=(Node*)NULL)
{
if(nodes==0)
head = p;
else if(head->data > data)
p->setNext(head);
else
{
bool done=false;
Node* q = head;

while(head!=(Node*)NULL && !done)
if(head->getNext()->getData() < data)
done = true;
else
head = head->getNext();

if(done)
q->setNext(p);
else
p->setNext(q);
}
++nodes;
}
}
size_t size() const
{
return nodes;
}
};

class Person
{
std::string name;
std::string lastname;
std::string telephone;
public:
Person(std::string n, std::string l, std::string
t):name(n),lastname(l),telephone(t){};
std::string getName() const { return name; }
std::string getLastname() const { return lastname; }
std::string getTelephone() const { return telephone; }
void setName(std::string name) { this->name = name;
return; }
void setLastname(std::string name) { this->lastname =
lastname; return; }
void setTelephone(std::string name) { this->telephone =
telephone; return; }
friend bool operator<(Person,Person);
friend bool operator>(Person,Person);
};

bool operator<(Person a, Person b)
{
return a.lastname < b.lastname;
}

bool operator>(Person a, Person b)
{
return a.lastname > b.lastname;
}

int main()
{
List<Person> agenda;

Person p("John","Doe","555-123456");

agenda.insert(p);

return 0;
}
 
O

Old Wolf

void insert(T data)
{
Node* p = new Node(data);

if(p!=(Node*)NULL)
{
if(nodes==0)
head = p;
else if(head->data > data)
p->setNext(head);

I think you're missing a "head = p" here,
after this statement you have not actually
added p to the list.
else
{
bool done=false;
Node* q = head;

while(head!=(Node*)NULL && !done)
if(head->getNext()->getData() < data)

Here's your problem: when you only have one
node in the list, head->getNext() is NULL,
so you have just dereferenced a null pointer.
I think you meant the loop condition to be:

while (head->getNext() != NULL && !done)

Note that your "(Node*)NULL" casts are useless
and obfuscatory; remove them.
done = true;
else
head = head->getNext();

if(done)
q->setNext(p);
else
p->setNext(q);
}

This loop trashes 'head' . It no longer points
to the head of the list after this.
++nodes;
}

I think this is correct (untested though),
note that i think you had the comparison
the wrong way around too:
{
Node* q;

/* go through until data is less than next entry */
for (q = head; q->getNext() != NULL; q = q->getNext() )
if ( data < q->getNext()->getData() )
break;

/* insert p after the spot we stopped (note that
this code still works if we stopped on the final node) */
p->setNext( q->getNext() );
q->setNext( p );
}
 
S

Salt_Peter

I'm doing a template class to make an ordered list.

I created a class Person to make an agenda.

When I create a new person, there's no problem but when I try to add
it to the list it throws a lot of errors.

Can someboy help me! Thanks!

#include <iostream>
#include <string>

template <typename T>
class List
{
class Node
{
T data;
Node* next;
public:
Node(T data)
{
this->data = data;
next = (Node*)NULL;
}
T getData() const
{
return data;
}
Node* getNext() const
{
return next;
}
void setData(T data)
{
this->data = data;
return;
}
void setNext(Node* next)
{
this->next = next;
return;
}
};

Node* head;
size_t nodes;

public:
List()
{
head = (Node*)NULL;
nodes = 0;
}

void insert(T data)
{
Node* p = new Node(data);

if(p!=(Node*)NULL)
{
if(nodes==0)
head = p;
else if(head->data > data)
p->setNext(head);
else
{
bool done=false;
Node* q = head;

while(head!=(Node*)NULL && !done)
if(head->getNext()->getData() < data)
done = true;
else
head = head->getNext();

if(done)
q->setNext(p);
else
p->setNext(q);
}
++nodes;
}
}
size_t size() const
{
return nodes;
}

};

class Person
{
std::string name;
std::string lastname;
std::string telephone;
public:
Person(std::string n, std::string l, std::string
t):name(n),lastname(l),telephone(t){};
std::string getName() const { return name; }
std::string getLastname() const { return lastname; }
std::string getTelephone() const { return telephone; }
void setName(std::string name) { this->name = name;
return; }
void setLastname(std::string name) { this->lastname =
lastname; return; }
void setTelephone(std::string name) { this->telephone =
telephone; return; }
friend bool operator<(Person,Person);
friend bool operator>(Person,Person);

};

bool operator<(Person a, Person b)
{
return a.lastname < b.lastname;

}

bool operator>(Person a, Person b)
{
return a.lastname > b.lastname;

}

int main()
{
List<Person> agenda;

Person p("John","Doe","555-123456");

agenda.insert(p);

return 0;

}


The List's embedded Node class doesn't need encapsulation (since the
List itself encapsulates Node). Why don't you just make Node a struct
with perhaps just a ctor?
When passing parameters, these should always be by const reference
unless there is a reason to do otherwise (const ref should be the
default). Otherwise you'll be passing via expensive copy ctors.

ie:
void insert(const T& data) { ... }

I'ld suggest designing an unordered list first.
 
J

Jon Harrop

Gaijinco said:
I'm doing a template class to make an ordered list.

I created a class Person to make an agenda.

When I create a new person, there's no problem but when I try to add
it to the list it throws a lot of errors.

Can someboy help me! Thanks!

You might be interested to see your example program written in OCaml using a
recursive "insert" function and the built-in list data structure:

let rec insert x = function
| y::_ as ys when x<=y -> x::ys
| y::ys -> y::insert x ys
| [] -> [x]

type person = {name: string; lastname: string; phone: string}

let list = insert {name="John"; lastname="Doe"; phone="555-123456"} []

The three rules of the "insert" function are all that is needed to implement
insertion into an ordered list. The first rule inserts an element when the
correct place is found. The second rule propagates insertion to the rest of
the list when necessary. The last rule handles insertion into the empty
list [].
 
T

tragomaskhalos

Gaijinco said:
I'm doing a template class to make an ordered list. [snip]
Can someboy help me! Thanks!

You might be interested to see your example program written in OCaml using a
recursive "insert" function and the built-in list data structure:
[snip]

I am certainly interested in how you think your frequent and
irrelevant plugs for your company on this NG can possibly assist the
OP with his C++ coding problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top