Singly linked list of structs

Pages: 12
Hey everyone, I want to make a singly linked list of structs with a custom datatype. Sadly, I dont think I understand either.

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
#include <iostream>
using namespace std;

struct datatype
	{
		string name;
		int id;
		float amt;
	};
struct Node
{
	datatype data;
	Node *next;
}*node;

class List 
{
	Node *head;
public:
			void insert(int);
			void remove();
			List(){head = 0;}
};

void List::insert(int){
	
}

int main ()
{

}


Basically, I cant figure out how to insert a strcut with a string, random ID number, and a random salary amount, into a linked list.

Any suggestions?
In order to access elements of your "datatype" struct using your node struct, you're going to have to use some syntax similar to this:

1
2
3
node->data->name="name";
node->data->id=10023923;
node->data->amt=1200.00;
closed account (D80DSL3A)
You will want void insert(datatype); since you would be inserting that type (not ints).
A constructor for the datatype struct would be helpful:
datatype(string Name, int Id, float Amt): name(Name), id(Id), amt(Amt) {}

Then, generate the values for Name, Id and Amt (random values if you wish) then call
insert( datatype(Name,Id,Amt) ); to add it to the list.
Last edited on
fun2Code, im confused, where would I put the datatype constructor?
closed account (D80DSL3A)
In the definition of the datatype struct, like so:
1
2
3
4
5
6
7
8
struct datatype
	{
		string name;
		int id;
		float amt;
                datatype(string Name, int Id, float Amt): name(Name), id(Id), amt(Amt) {}
                datatype(void): name(""), id(0), amt(0.0f) {}// no argument ctor
	};

I added a no-arg constructor so it is still possible to declare a datatype without supplying any parameters, as in:
datatype a; or datatype arr[5];
Ok, here is my class and insert method, it is giving me error on the variables not being available. I thought everything in a struct was globally available?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class List 
{
	Node *head;
public:
			void insert(datatype);
			void remove();
			List(){head = 0;}
};

void List::insert( datatype(Name,Id,Amt) );
	if (head == NULL){
		
		}
}
closed account (D80DSL3A)
It is, but your code has a few syntax problems.
This may work better, though it isn't inserting anything yet:
1
2
3
4
5
6
7
8
9
10
11
void List::insert( datatype dat )
{
    if( head== NULL )
    {
       // list is empty. Create the head node.
    }
    else
    {
       // add a node to the list
    }
}

Ok, I see how you referenced the variable, now how do you insert the 3 different datatypes of the actual struct?
closed account (D80DSL3A)
It's easiest to insert at the front of the list:

1
2
3
4
5
6
7
void List::insert( datatype dat )
{
    Node* temp = new Node;
    temp->data = dat;// all 3 datatype members will be copied here
    temp->next = head;// the last Node added becomes second in the list
    head = temp;// the newly added Node is now the first one in the list
}

Example of usage in main():
1
2
3
4
5
6
7
int main()
{
    List myList;// declare an empty list
    myList.insert( datatype("Jack", 123, 20.45f) );// insert 1st node

    return 0;
}
1
2
3
4
5
6
7
int main()
{
    List myList;// declare an empty list
    myList.insert( datatype("Jack", 123, 20.45f) );// insert 1st node

    return 0;
}


This was the part I was confused on. But it makes sense now. Thank you so much for taking the time.
When I use the code in my main() function, it tells me that datatype is undefined. Is that because the computer executes the main() code first? How do I declare it since it is a struct?
In the cpp file with main, make sure you included the header that declares datatype.
Is that the only way to make it work? I'm not too good with header files....
1
2
3
4
5
6
#include "MyHeaderFile.hpp"

int main()
{
    //blah
}
so in the main() in the header "MyHeaderFile.hpp" I would declare the struct called datatype?
If you wanted to use headers, yes.

If you look at your first post though, you can just define everything before main, but it is conventional to properly use headers.
Well i thought I did have everything defined before the main(). I have the struct defined first. Without using a header, can you show me what it would look like?
1
2
3
4
5
6
7
8
9
struct MyStruct
{
    //blah
};

int main()
{
    //blah
}


Maybe you should post your entire program so we can see what is wrong?
Last edited on
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
#include <iostream>
using namespace std;

struct datatype
	{
		string name;
		int id;
		float amt;
	}node;
struct Node
{
	datatype data;
	Node *next;
}*node;

class List 
{
	Node *head;
public:
			void insert(datatype dat);
			void remove();
			List(){head = 0;}
};

void List::insert(datatype dat){
	{
    Node* temp = new Node;
    temp->data = dat;
    temp->next = head;
    head = temp;
}

}

int main ()
{
	 List myList;
	 myList.insert(datatype[***Error is here, says this is undefined]("Jack", 123, 20.45f));

    return 0;
}


Here it is. I pointed out the one error I keep getting flagged for.
Last edited on
Remove "node" and "*node" from lines 9 and 14

The problem is that you didn't add the constructor to datatype like fun2code told you to:
http://www.cplusplus.com/forum/general/94267/#msg506159
Pages: 12