Linked List Outputting Issue

Write your question here.

Hello again, I am having a problem with my linked list outputting zeros instead of inputted values. Can someone check what I did wrong in the add and print fucntions? Will add main.cpp also if needed.

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
#ifndef __SINGLY_LINKED_LIST__
#define __SINGLY_LINKED_LIST__
#include <iostream>
using namespace std;
struct IntNode
{
	int data;
	IntNode * next;
	IntNode(const int & d = 0, IntNode *n = nullptr) : data(d), next(n) {}
};
class singleListInt
{
public:
	singleListInt() { init(); } //default constructor and only constructor
	~singleListInt() { eraseList(head); } //destructor
	singleListInt(const singleListInt & rhs) { //Copy Constructor {
		eraseList(head);
		init();
		*this = rhs;
	}
void add(int x) { //printing out zeros instead of inputted values.

	IntNode *myNode = new IntNode;
		if (head == nullptr) {
				head = myNode;
		}
		else {
			IntNode *current = head;
			IntNode *prev = 0;
			while (current != nullptr) {
				if (current->data >= myNode->data) {
					break;
				}
				else {
					prev = current;
					current = current->next;
				}
			}
			if (current == head) {
				myNode->next = head;
				head = myNode;
			}
			else {
				myNode->next = current;
				prev->next = myNode;
			}
		}
}

void remove(int x){
	IntNode *current = head;
	IntNode *prev = nullptr;
}
int size() { //To Do: Implement 
	IntNode *number = head->next;
	int i = 0;

	while (number != nullptr) {
		number = number->next;
		i++;
	}
	return i;
}
void print() {
		IntNode *ptr = head->next;
		//To Do: Print list in ascending order with one space between values
//print function not working either
		while (ptr != nullptr) {
			cout << ptr->data << " ";
			ptr = ptr->next;
		}
		cout << endl;
	}
bool contains(const int & x) {
	//To Do: return true x is in list or return false if x is not in list : Done.
	IntNode *current = head;
	while (current != nullptr) {
		if (current->data != x) {
			current = current->next;
		}
		else {
			return true;
		}
		return false;
	}
}
void clearList() {
		eraseList(head);
		init();
		return;
	}
private:
	IntNode *current;
	IntNode *temp;
	void init()
	{
		theSize = 0;
		head = new IntNode;
		head->next = nullptr;
	}
	void eraseList(IntNode * h)
	{
		//To Do: remove all the nodes in the list

	};
	IntNode *head;
	int theSize;
};
#endif
Topic archived. No new replies allowed.