Hi im creating a linked list with these data types

etc

string firstname
string surname
int age

would anyone give a simple instruction to put these in a new node as i have tried everything but still doesnt work so far i have done this

nodeType first,newNode, *last;

cout<<"Name: ";
cin>>name;
cout<<"Surname: ";
cin>>surname;
cout<<"Age: ";
cin>>age;


setVideoInfo(name,surname,age);



  first=NULL;


     newNode = new nodeType;     // create new Node

    newNode->info =(name,surname,age) ;

*This is not all of it but i know the problem lies in the lines above any help would be much appreciated

Thankyou*

Recommended Answers

All 3 Replies

It's kind of hard to see what the problem is without knowing how you Node type looks, but this is how I would do it.

typedef unsigned int uint;

class Node {
public:
    Node(uint a, std::string n, std::string s) :
    age(a), name(n), surename(s), next(NULL) {}



private:
    uint age;
    std::string name;
    std::string surename;
    Node* next;
};

A node is a simple struct or class that contains elements and a pointer to the next node like K0ns3rv did with Node * next. In this case it's better to use a struct because you don't need to make private your variables, it will be harder if you work like that. You could try making a struct like this:

struct Node
{
    string name;
    string surname;
    int age;
    Node* next;//a pointer to the next node from the list.
    //A constructor to create easily a "Node" struct
    Node(string name,string surname,int age):
    name(name),surname(surname),age(age)
    {
        //You don't need to use this space. You initialized the variables after the ':'
    }
};

Don't forget to include the iostream and string libraries and "using namespace std;"

the easiest way to fix this problem is create a Node class. A Node is like a block of data in the list which contains a link to the node before and after it. This Node can pretty much contain anything you want really assuming you have enough memory and all that, but in the Node class you can just create numerous variables to store the data you need
So here's how I would do it:

struct Node
{
    string name;
    string surname;
    int age;
    Node* next;

    Node(string n,string surn,int a){
    name = n;
    surname = surn;
    age = a;
    next = null;//assigned this null because you will update it's next when inserting
            //it into your list with an insert function. This will just create an
            //independent node
    }
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.