Linked list questions in c++

Started by
13 comments, last by the_edd 13 years, 1 month ago

user p ;
p.name = "ted";
p.id =12;
node<user> *head_ptr;
node<user> *tail_ptr;

node<user> r;

head_ptr = NULL;
tail_ptr = NULL;

I have tryed
head_ptr = p;

head_ptr is of type node<user>* and p is of type user so this assignment won't work as the types are really quite unrelated. You need to create a node<user>* in order to initialize head_ptr.

Something like:


template<typename T>
struct node
{
node(const T &data) : data(data), next(0) { }
T data;
node *next;
};

struct user
{
user(const std::string &name, int id) : name(name), id(id) { }
std::string name;
int id;
};

// ...

node<user> *p = new node<user>(user("ted", 12));


This is reasonably elementary-level stuff. If you're struggling with it, it might be worth picking up a beginners C++ text or reading through some tutorials.
Advertisement
I am in my second year of collage I already did the c++ calss doing data structures
did the queue and stack assinment. Working on the linked list one but I want to do more then what the min requirements of the assinment is.

So its not that easy to creat what I need from scratch without using the stl.

It has to be all from scratch.

The linked list works as long as I set it to the data I want and I can set it to 2 data types bit to do that I would have to change the class every
time I wanted to make a change which is not what I want. The professor dont care so he wont help with making it better.


template <class Item> //edit for got this part

class node
{
public:
typedef Item value_type;

node(const Item& init_data = Item(),const node* init_link = NULL)
{
data_field = init_data; link_field = init_link;
}
void set_data(const Item& new_data){data_field = new_data;}
void set_link(node* new_link) {link_field = new_link;}

const Item& data() const{return data_field;}

const node* link() const{return link_field;}
node* link() {return link_field;}
protected:
private:
Item data_field;
node *link_field;
};


This is my header file for my linked list class
But as you can see it only takes one data type and I could change it to take two data types but I rather use a struct to contain the data needed then pass it to the list.
You should learn about templates. They allow you to make code for any type of data. You can change the data type of Item without having to alter the code, and have lists which can contain only their kind of data to prevent errors.

You should learn about templates. They allow you to make code for any type of data. You can change the data type of Item without having to alter the code, and have lists which can contain only their kind of data to prevent errors.


the thing is I am trying to use a template but I cant figure out the syntax to make the call
from the noce class using the user struct as the data.

the thing is I am trying to use a template but I cant figure out the syntax to make the call
from the noce class using the user struct as the data.

Does my previous post not illustrate how you might do that? If not can you be more specific about where you're having trouble?

This topic is closed to new replies.

Advertisement