I am writing a program that replicates the functionality of the List ADT using arrays; I am beginning by creating a Node object with two attributes: the element and a pointer to the next node. When I run my code, however, it reports that all of my Nodes initialize with element -2 and a random pointer, regardless of how I construct them; does anyone know what could be causing this?
#include <fstream>
#include <iostream>
using namespace std;
/* Class Name: Node
Main Function: Store an element (in this case, a positive integer) and a pointer to the next Node in the List, or to "null"
Methods: getElement, setElement, getNext, setNext
*/
class Node {
public:
int element; //Node has two attributes: an integer, element, and a pointer to a Node, next
Node* next;
Node(int element, Node* next){ //Node can be constructed with both attributes inputted
this->element = element;
this->next = next;
}
Node (int element){ //If only one is inputted, then
this->element = element;
next = nullptr; //the "next" pointer is initialized as "NULL"
}
Node (Node* next){
element = 0; //or the node is initialized with an element of "0"
this-> next = next;
}
Node(){
int element = 0; //the Node can also be constructed given no input, using both default values
Node* next = nullptr;
}
/* Method Name: getElement
Input Parameters: N/A
Return Values: element, the value stored by the node
Main Function: Returns the element stored by the node
*/
int getElement() {
return element;
}
/* Method Name: setElement
Input Parameters: A new element to store in the Node
Return Values: the original element being stored, if the method executes correctly; -1, if method encounters an error
Main Function: Sets the node's element to a new value after checking if it is positive
*/
int setElement (int newElement){
if (newElement <= 0){
cerr << "Error: this List can only store positive integers." << endl << endl;
return -1;
}
int oldElement = element;
element = newElement;
return oldElement;
}
/* Method Name: getNext
Input Parameters: N/A
Return Values: next, a pointer to the next Node in the List
Main Function: Reads a file and stores its contents as an array of integers; reports if an error occurs
*/
Node* getNext() {
return next;
}
/* Method Name: setNext
Input Parameters: A new pointer for the Node to direct to
Return Values: the original pointer being stored, if the method executes correctly; -1, if method encounters an error
Main Function: Sets the node's element to a new value after checking that the node it points to exists
*/
Node* setNext (Node* newNext){
Node* oldNext = next;
next = newNext;
return oldNext;
}
};
int main(){
Node n1; //tests if a Node can be constructed given no input
cout << "When a node is constructed with no input, it has an element of " << n1.getElement() << " and a next of " << n1.getNext() << endl << endl;
Node n2 (5); //tests if a Node can be constructed given an element as input
cout << "When a node is constructed with element 5, it has an element of " << n2.getElement() << " and a next of " << n2.getNext() << endl << endl;
Node n3 (&n1); //tests if a Node can be constructed given an address as input
cout << "When a node is constructed pointing to Node 1, it has an element of " << n3.getElement() << " and a next of " << n3.getNext();
cout << ", which points to " << n3.getNext()->getElement() << endl << endl;
Node n4 (10, &n2); //tests if a Node can be constructed given an element and an address as unput
cout << "When a node is constructed with element 10 pointing to Node 2, it has an element of " << n4.getElement() << " and a next of " << n4.getNext();
cout << ", which points to " << n4.getNext()->getElement() << endl << endl;
return 0;
}
I was expecting the program to construct four nodes: one with element 0 and a nullptr, one with element 5 and a null pointer, one with element 0 pointing to node 1 and one with element 10 pointing to node 2.