Adding node to end of linked list

I have a header with inline implementation and I have a node header that returns all the values I need. I know my node header is correct, but when I run my program I am getting a runtime error in my push_back function. This function is supposed to be adding a node to the end of the list. Could anyone tell me what I am doing wrong?
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef LLIST_H
#define LLIST_H
#include "node.h"
#include <string>
#include <iostream>
using namespace std;


class LList{
private:	
	
	typedef string valueType;

	Node* head;
	Node* tail;

public:
	LList();
	LList(const LList&);
	~LList();
	void push_front(const valueType&);
	void push_back(const valueType&);
	void delete_list(Node*&); 
	void operator=(const LList&);
	friend ostream& operator<<(ostream&, const LList&);
	void list_copy(const LList&);

};

inline LList::LList()
{
	head = NULL;
	tail = NULL;
}

inline LList::LList(const LList& source)
{
	//Node* tail_ptr;
	list_copy(source);
}

inline LList::~LList()
{
	cout << endl << "The destructor has been called.\n";
	delete_list(head);
}

inline void LList::delete_list(Node* &head_ptr)
{
	Node* remove_ptr;

	while (head_ptr != NULL)
	{
		remove_ptr = head_ptr;
		head_ptr = head_ptr->next();
		delete remove_ptr;
	}
	
}

inline void LList::push_back(const valueType& entry)
{
	Node* insert_ptr;
	Node* previous_ptr;
	previous_ptr = NULL;
	
	insert_ptr = new Node(entry);
	insert_ptr->str(entry);
	insert_ptr->next(previous_ptr->next());
	previous_ptr->next(insert_ptr);

}

inline void LList::push_front(const valueType& entry)
{
	head = new Node(entry);
}

inline void LList::list_copy(const LList& source)
{
	//Node* temp;
	//temp = new Node;
	///*head_ptr = NULL;
	//tail_ptr = NULL;*/

	//if(source_ptr == NULL)
	//{
	//return;
	//}
	////push_front(source_ptr->str());
	//tail_ptr = head_ptr;

	//source_ptr = source_ptr->next();
	//while(source_ptr != NULL)
	//{
	//	push_back(source_ptr->str());
	//	tail_ptr = tail_ptr->next();
	//	source_ptr = source_ptr->next();
	//}
}

inline void LList::operator=(const LList& source)
{
//	Node* tail_ptr;

	if(this == &source)
	{
		return;
	}

	list_copy(source);

	delete_list(head);

}

inline ostream& operator<<(ostream& out, const LList& L)
{
	Node* p;
	
	for(p = L.head; p != NULL; p=p->next())
	{
		out << p->str()<<endl;
		out << endl;
		
	}
	return out;
}

#endif
What is node::str()? Your push_back method doesn't look right at all. You need to traverse until your pointer (current position) node has a next pointer of null, signalling it's the last in line. Then create a new node, and set the current.next = newNode.

EDIT:
node::str is your data. Anyways, you're also setting previous_ptr to null, then trying to dereference it. That's a pretty big issue. Definitely rework your logic here.
Last edited on
node::str() returns the string, and the line before tht sets the string into the node and sets next to NULL
Okay, tell me if this sounds right. If I have a head and tail pointer and I test them and they are equal to NULL then I can just add the node else I can iterate through the node till I get to tail->NULL and add the node there?
If you have a data member tail that points to the last node in the list, why would you need to iterate through the list?
Topic archived. No new replies allowed.