Basic linked list not working

Hi again guys,

I am trying to get my head around a basic linked list. All the nodes will be a simple int data value. I am not sure if I implemented the 2 classes correctly as when I run my code I should be getting 5 7 3 in the console but it's currently showing 0 0 0

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

//ListOFInts.h
#ifndef LISTOFINTS_H
#define LISTOFINTS_H
#include "NodeOfInt.h"

class ListOfInts
{
	friend class NodeOfInt;
public:

	ListOfInts();
	~ListOfInts();

	void insertInt(int);
	void displayList();

private:

	NodeOfInt* head;
};

typedef NodeOfInt* ListNodePtr;

#endif

  //ListOfInts.cpp

#include "pch.h"
#include <iostream>
#include "ListOfInts.h"
using namespace std;

ListOfInts::ListOfInts()
{
	head = NULL;
}

ListOfInts::~ListOfInts()
{

}
void ListOfInts::insertInt(int x)
{
	NodeOfInt* newNode = new NodeOfInt(x);
	newNode->next = head;
	head = newNode;
}

void ListOfInts::displayList() 
{
	ListNodePtr tempPtr = head;
	while (tempPtr != NULL)
	{
		cout << tempPtr->data << " ";
		tempPtr = tempPtr->next;
	}
}

//NodeOfInt.h

#ifndef NODEOFINT_H
#define NODEOFINT_H

class NodeOfInt
{
	friend class ListOfInts;
public:
	NodeOfInt(int);

private:
	int data;
	NodeOfInt* next;
};
typedef NodeOfInt* ListNodePtr;

//NodeOfInt.cpp
#include "pch.h"
#include <iostream>
#include "NodeOfInt.h"
using namespace std;

NodeOfInt::NodeOfInt(int d)
{

}


//main
#include "pch.h"
#include <iostream>
#include "ListOfInts.h"
#include "NodeOfInt.h"
using namespace std;

int main()
{
	ListOfInts int1;
	int1.insertInt(5);
	int1.insertInt(7);
	int1.insertInt(3);


	int1.displayList();
}



Last edited on
You don't set the data in each node. You pass in a number (e.g. 5) to the constructor, but the constructor does nothing with that number.
Thank you mate! Could you please help me with a function to delete the most recent added int (first node in the linked list).

I get an error when i try run this:

1
2
3
4
5
6
int ListOfInts::deleteMostRecent()
{
	ListNodePtr tempPtr = head;
	delete tempPtr;
	return tempPtr->data;
}
1
2
delete tempPtr;
return tempPtr->data;


Does that seem reasonable to you? Calling delete on a pointer, and then after that trying to do something with that pointer? Anything seem odd about that?
true. i tried

1
2
3
4
5
void ListOfInts::deleteMostRecent()
{
	ListNodePtr tempPtr = head;
	delete tempPtr;
}


and i get an exception thrown in the function ListOfInts::displayList() :

cout << tempPtr->data << " "; ** read access violation on this line
You need to stop guessing and think about what you're trying to do; think about what is actually being done when you're trying to delete the first node in the list.

First, you need to make head point to the SECOND node in the list. Then, you delete the first node.
Last edited on
thanks for the reply. i only started looking at linked lists today and they're confusing me! :P



i understand your point, this code works:

1
2
3
4
5
6
void ListOfInts::deleteMostRecent()
{
	ListNodePtr tempPtr = head;
	head = head->next;
	delete tempPtr;
}



but why do i need to make it point to the 2nd node in the list if i am trying to delete the first? so i can understand the concept better

cheers mate
why do i need to make it point to the 2nd node in the list if i am trying to delete the first?


Your list object, ListOfInts , is basically just a pointer to the first node in the list. It calls this pointer "head". Each node points to the next node in the list, so the ListOfInts object just needs to always know what the first node is.

So if ListOfInts ever stops pointing at the first node in the list, how can you ever get back any of the data? You can't.

So when you remove the first node from the list, what becomes the new first node in the list? What should ListOfInts be pointing at?
Last edited on
of course, the next node in the list! thanks man, very helpful
Topic archived. No new replies allowed.