problem with printing linked list data

Hello!
I made a simple program to practice linked list basics with the current goal being to be able to insert a new node in front of head or before head if i choose to do so.

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
  #include<iostream>

using std::cout;
using std::cin;
using std::endl;


struct node {
	int data;
	node *next;
};

class list {
private:
	node *head;
	node *temp;
	int length;

public:
	list();
	list(int data);

	~list();
	
	void insertTop(int data);
	void insertBottom(int data);
	void printList();
};

list::list() {
	head = new node;
	head->data = 0;
	head->next = NULL;
	temp = head;
	length = 0;
}//constructor end

list::list(int data) {
	head = new node;
	head->data = data;
	head->next = NULL;
	temp = head;
	length = 0;
}//constructor end

list::~list() {
	node* prev = head;
	node* pnext = head;
	while (pnext) {
		prev = pnext;
		pnext = prev->next;
		if (pnext) delete prev;
	}//while end
}//destructor end

void list::insertTop(int data) {
	node *newNode;
	newNode = new node;
	newNode->data = data;
	newNode->next = NULL;
	temp->next = newNode;
	temp = temp->next;
	length++;
}//insertTop function end

void list::insertBottom(int data) {
	node* newNode;
	newNode = new node;
	newNode->data = data;
	newNode->next = head;
	head = newNode;
	length++;
}//insertBottom function end

void list::printList() {
	for (int i=0; i < length; i++) {
		temp = head;
		cout << "Data at location " << i << " is: " << temp->data << endl;
		temp = temp->next;
	}//for end
	cout << "Data at location " << length << " is:" << temp->data;
}//printList function end

int main() {
	list a;
	a.insertTop(51);
	a.insertTop(22);
	a.insertTop(31);
	a.printList();
	return 0;
}


the problem is that the last 2 inserts show that the data is 0 and the first insert shows as if it was the last one with the correct data.
Program output:
Data at location 0 is: 0 //<--the reason behind this one is the constructor(not a problem)
Data at location 1 is: 0
data at location 2 is: 0
data at location 3 is: 51

I suspect the printList function but cant pinpoint where the problem occurs.
Any help will be much appreciated.
in front of head or before head

reading from left to right, what is the difference b/w these two?
Sorry for the vague explanation i'l try to explain it more graphically:

in front:

head: new node
_______ ______
| data | |data |
|_____| |_____|
|next | |next |
|_____|---->|_____|

Regular insertion.

before:
newNode : head:
_______ ______
| data | |data |
|_____| |_____|
|next | |next |
|_____|---->|_____|

Here we insert newNode before head making it the new head.
when you insert into a singly linked list you'd have to check first whether or not the list is empty
node *temp; - a data-member is usually a representation of some characteristics for the datatype (e.g class Person would have name, age, etc as data-members). so a full-fledged data-member of the list is not necessary just to iterate through the list, you can declare local variables for that purpose
in front: ... head: new node
1
2
3
4
5
6
7
8
9
10
11
12
13
void insertAfterHead(const int& data)
{
    node* temp = new node(data);
    if(head)
    {
        temp -> next = head -> next;//head assigns ownership of it's next to temp
        head -> next = temp;//then head's next is reassigned to temp
    }
    else //list is empty so temp becomes head;
    {
        head = temp;
    }
}

before: ... newNode : head:
1
2
3
4
5
6
7
8
9
void insertBeforeHead(const int& data)//const qualify data
{
    node* temp = new node(data);//overloaded ctor
    if(head)//if list is not empty
    {
        temp -> next = head;
    }
    head = temp;
}


your list also needs a destructor as the objects allocated with new are not automatically released
finally, this link has a discussion on singly linked lists with a working print()/display() method:
http://www.cplusplus.com/forum/beginner/210637/
Thanks for the response!


when you insert into a singly linked list you'd have to check first whether or not the list is empty
node *temp; - a data-member is usually a representation of some characteristics for the datatype (e.g class Person would have name, age, etc as data-members). so a full-fledged data-member of the list is not necessary just to iterate through the list, you can declare local variables for that purpose


I understand your solution but i still cant understand why mine doesn't work. I know my way is far from perfect but my goal was to learn only the logic behind the list and try to build it myself before i watch a proper guide on how to make one.


your list also needs a destructor as the objects allocated with new are not automatically released


I know that they don't release automatically and i did make a destructor:
1
2
3
4
5
6
7
8
9
list::~list() {
	node* prev = head;
	node* pnext = head;
	while (pnext) {
		prev = pnext;
		pnext = prev->next;
		if (pnext) delete prev;
	}//while end
}//destructor end 


Though i don't know if it is supost to be done that way
Topic archived. No new replies allowed.