Linked List

Working on inserting a node at the front of linked list. First time I ran code worked perfect but now no good.
Thanks

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

class Node
{
public:
	int value;
	Node* Next;
};

void printlist(Node* n)
{
	while(n!= NULL)
	{
		cout << n->value << endl;
		n = n->Next;
	}
}
void insertAtFront(Node** head, int newvalue)
{	
	Node* newNode = new Node();
	newNode->value = newvalue;
	newNode->Next = *head;
	*head = newNode;
}



int main()
{
	Node* head = new Node();
	Node* second = new Node();
	Node* third = new Node();
	Node* fourth = new Node();

	head->value = 1;
	head->Next = second;
	second->value = 2;
	second->Next = third;
	third->value = 3;
	third->Next = fourth;
	fourth->value = 4;
	fourth->Next = NULL;

	printlist(head);
	insertAtFront(&head, -1);
	//insertAtFront(&head, -2);




	system("pause>0");
}
Last edited on
Try this:

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

class Node
{
public:
	int value {};
	Node* Next {};
};

void printlist(const Node* n)
{
	for (; n != nullptr; n = n->Next)
		cout << n->value << '\n';

	cout << '\n';
}

void insertAtFront(Node*& head, int newvalue)
{
	auto newNode {new Node()};

	newNode->value = newvalue;
	newNode->Next = head;
	head = newNode;
}

int main()
{
	Node* head {};

	insertAtFront(head, -1);
	insertAtFront(head, -2);
	insertAtFront(head, -3);
	insertAtFront(head, -4);

	printlist(head);
}


which displays:


-4
-3
-2
-1

Thanks for the code.

Do you see any issues with my code so far? First time I ran code -1 printed in front of 1 but second run nothing printed except 1 - 4.
Any ideas?
Well there's no printlist() after the insertAtFront() ??

Adding that, and slightly changing the display format:

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

class Node
{
public:
	int value;
	Node* Next;
};

void printlist(Node* n)
{
	while (n != NULL)
	{
		cout << n->value << endl;
		n = n->Next;
	}

	cout << '\n';
}

void insertAtFront(Node** head, int newvalue)
{
	Node* newNode = new Node();
	newNode->value = newvalue;
	newNode->Next = *head;
	*head = newNode;
}

int main()
{
	Node* head = new Node();
	Node* second = new Node();
	Node* third = new Node();
	Node* fourth = new Node();

	head->value = 1;
	head->Next = second;
	second->value = 2;
	second->Next = third;
	third->value = 3;
	third->Next = fourth;
	fourth->value = 4;
	fourth->Next = NULL;

	printlist(head);
	insertAtFront(&head, -1);
	insertAtFront(&head, -2);
	printlist(head);

	//system("pause>0");
}



1
2
3
4

-2
-1
1
2
3
4


ah
its usually something that basic that i overlook.
Thanks Seeplus for all your help
Topic archived. No new replies allowed.