Problem with inserting element to empty list?

Hi!

I've decided to practice linked lists, creating nodes from different kind of data structures, arrays and stuff. My current task is creating a linked list from a structure array, that contains some countries with their names and population. I would like to make an ordered list with the insert() function. However, my list just doesn't want to change the head part from nullptr to the first new_node and I don't know what I can be doing wrong. Could you help me out here, please?

The code:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<string>

struct country_t{
	
	std::string name;
	double quantity;
	
};

struct Node{
	
	std::string name;
	double num;
	Node* next;
	
};

Node* insert(country_t element);

int main(){
	
	country_t arr[] = {
		
		{"Hungary", 10},
		{"Serbia", 6.95},
		{"Czeh Republic", 10.65},
		{"Romania", 19.41},
		{"England", 55.98},
		{"Austria", 8.86}
		
	};
	
	int n = sizeof(arr) / sizeof(arr[0]);
	
	for(int i = 0; i < n; i++){
		insert(arr[i]);
	}
	
	return 0;
}

Node* createNode(country_t element){
	
	Node* new_node = new Node;
	if(new_node){
		new_node->name = element.name;
		new_node->num = element.quantity;
		new_node->next = nullptr; 
	}
	
	return new_node;
}

Node* insert(country_t element){
	
	Node* head = nullptr;
	Node* prev;
	Node* new_node = createNode(element);
	
	
	if(head == nullptr){
		head = new_node;
	}else if(head != nullptr){
		for(Node* i = head; i->num < new_node->num; i++){
			prev = i;
		}
		new_node->next = prev->next;
		prev->next = new_node;
		
	}

	return head; 
}


Last edited on
Look at lines 19, 37, 55 and 73.

Tell us what you think is happening to the nodes you create.
Well, uhm... I dunno... they are passed in a wrong way?
I mean I know there is something wrong, but I don't know what to change... it'd be handy if you could just tell me what to change
You're ignoring the return result.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<string>

struct country_t{
	
	std::string name;
	double quantity;
	
};

struct Node{
	
	std::string name;
	double num;
	Node* next;
	
};

//!! insert needs to know a list to insert into.
Node* insert(Node* head, country_t element);

int main(){
	
	country_t arr[] = {
		
		{"Hungary", 10},
		{"Serbia", 6.95},
		{"Czeh Republic", 10.65},
		{"Romania", 19.41},
		{"England", 55.98},
		{"Austria", 8.86}
		
	};
	
	int n = sizeof(arr) / sizeof(arr[0]);

	//!! main starts off with an empty list.
	Node* head = nullptr;

	for(int i = 0; i < n; i++){
		head = insert(head, arr[i]);
	}
	
	return 0;
}

Node* createNode(country_t element){
	
	Node* new_node = new Node;
	if(new_node){
		new_node->name = element.name;
		new_node->num = element.quantity;
		new_node->next = nullptr; 
	}
	
	return new_node;
}

//!! pass in the current head
Node* insert(Node* head, country_t element){
	
	Node* prev;
	Node* new_node = createNode(element);
	
	
	if(head == nullptr){
		head = new_node;
	}else if(head != nullptr){
		for(Node* i = head; i->num < new_node->num; i++){
			prev = i;
		}
		new_node->next = prev->next;
		prev->next = new_node;
		
	}

	return head; 
}

Ohh I see... and is it possible to make the head in the function? As if I couldn't reach int main(), cause it's hidden in front of me?
Cause I've seen tasks where I have to write only the insert() function but I'm unable to do modifications in main
Have you covered class with methods/functions yet? A linked list would usually be implemented as such.

Also, the insert code has issues and allocated memory isn't freed. Consider:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <iterator>

struct country_t {
	std::string name;
	double quantity {};
};

struct Node {
	country_t data {};
	Node* next {};
};

Node* insert(Node* head, const country_t& element);

int main() {
	const country_t arr[] {
		{"Hungary", 10},
		{"Serbia", 6.95},
		{"Czeh Republic", 10.65},
		{"Romania", 19.41},
		{"England", 55.98},
		{"Austria", 8.86}
	};

	Node* head {};

	for (const auto& d : arr)
		head = insert(head, d);

	for (auto i = head; i; i = i->next)
		std::cout << i->data.name << "  " << i->data.quantity << '\n';

	std::cout << '\n';

	while (head) {
		const auto nxt {head->next};

		delete head;
		head = nxt;
	}
}

Node* createNode(const country_t& element) {
	const auto new_node {new Node{}};

	new_node->data = element;
	return new_node;
}

Node* insert(Node* head, const country_t& element) {
	const auto new_node {createNode(element)};

	if (head == nullptr)
		head = new_node;
	else {
		Node* prev {};

		for (auto i = head; i && (i->data.quantity < new_node->data.quantity); i = i->next)
			prev = i;

		if (prev) {
			new_node->next = prev->next;
			prev->next = new_node;
		} else {
			new_node->next = head;
			head = new_node;
		}
	}

	return head;
}



Serbia  6.95
Austria  8.86
Hungary  10
Czeh Republic  10.65
Romania  19.41
England  55.98

Last edited on
As a class, consider:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
#include <iterator>
#include <iomanip>

struct Country_t {
	std::string name;
	double quantity {};

	friend std::ostream& operator<<(std::ostream& os, const Country_t& t) {
		return os << std::left << std::setw(20) << t.name << std::right << std::setw(8) << std::fixed << std::setprecision(2) << t.quantity;
	}
};

class MyList {
public:
	MyList() {}

	~MyList() {
		while (head) {
			const auto nxt {head->next};

			delete head;
			head = nxt;
		}
	}

	MyList(const MyList&) = delete;
	MyList& operator=(const MyList&) = delete;

	void insert(const Country_t& element) {
		const auto new_node {new Node(element)};

		if (head == nullptr)
			head = new_node;
		else {
			Node* prev {};

			for (auto i = head; i && (i->data.quantity < new_node->data.quantity); i = i->next)
				prev = i;

			if (prev) {
				new_node->next = prev->next;
				prev->next = new_node;
			} else {
				new_node->next = head;
				head = new_node;
			}
		}
	}

private:
	struct Node {
		Country_t data {};
		Node* next {};

		Node(const Country_t& c) : data(c) {}
	};

	Node* head {};

	friend std::ostream& operator<<(std::ostream& os, const MyList& ml) {
		for (auto i = ml.head; i; i = i->next)
			os << i->data << '\n';

		return os;
	}
};

int main() {
	const Country_t arr[] {
		{"Hungary", 10},
		{"Serbia", 6.95},
		{"Czeh Republic", 10.65},
		{"Romania", 19.41},
		{"England", 55.98},
		{"Austria", 8.86}
	};

	MyList ml;

	for (const auto& d : arr)
		ml.insert(d);

	std::cout << ml << '\n';
}



Serbia                  6.95
Austria                 8.86
Hungary                10.00
Czeh Republic          10.65
Romania                19.41
England                55.98

Last edited on
@seeplus I haven't learned object oriented programming, so I tried out your first method ^^ It ran down perfectly, except one thing - that it left out my first element, aka Hungary from the sorting ^^'
Update: I solved it :D But thanks for the help, I greatly appreciate it!!
It ran down perfectly, except one thing - that it left out my first element, aka Hungary from the sorting ^^'


Not from my code as provided above. The output includes Hungary as shown in the post. Pleased you're solved your issue.
Topic archived. No new replies allowed.