When I call the default constructor for my class LinkedList I attempt to assign values to the head node of the linked list before any other operations occur. I have isolate the error, via debugging, to the instructions in the default constructor. As soon as
head -> next = NULL;
head -> RUID = 0;
head -> studentName = "No Student in Head";
are called the program crashes. This occurs when I call the default constructor in main.
Here is my class declaration and my struct declaration along with the default constructor:
struct Node
{
string studentName;
int RUID;
Node* next;
};
class LinkedList
{
private:
// Initialize length of list
int listLength;
public:
// Head of the list, which points to no data yet
Node *head;
LinkedList();
bool insertNode(Node* newNode, int position);
int generateRUID();
};
LinkedList::LinkedList()
{
head -> next = NULL;
head -> RUID = 0;
head -> studentName = "No Student in Head";
listLength = 0;
}
I believe this all of the relevant code to this issue. If someone could shed light on this it would be much appreciated.
LinkedList::head is a Node*, not a Node and you don't initialize it, so the object (binary, in-memory) representation is undefined and is therefore dangerous to dereference.
Change your LinkedList to explicitly initialize the head member. I recommend storing it by-value (as Node) rather than as a heap-allocated value (Node*) for simplicitly, unless you know you'll need to reparent nodes.
Using Node*:
LinkedList::LinkedList() :
head( Node() ),
listLength( 0 )
{
this->head->next = nullptr;
this->head->RUID = 0;
this->head->studentName = "No Student in Head";
}
Related
I created a program to make a link list in c++
I have a class CL I am trying to insert nodes at head again and again but this is giving unexpected results.
I tried to insert Nodes using this method
class Node {
public:
string name;
Node* Next;
};
class CL {
public:
Node* head;
public:
CL(void) { head = NULL; } // constructor
Node* insertAtHead(string name); //insert at start of list
void displayList(void);
};
int main()
{
CL poliop;
poliop.insertAtHead("am");
poliop.insertAtHead("a");
poliop.displayList();
}
void CL::displayList(void){
Node* t;
t = head;
while(t != NULL){
cout<<(t->name)<<endl;
t = t->Next;
}
}
Node* CL::insertAtHead(string name)
{
Node temp;
temp.name = name;
temp.Next = head;
head = &temp;
}
OUTPUT
a
a
a
a
a
a
a
a
.
.
.
Just a few quick things that could be causing this:
In your main() function, you don't declare a CL object. So when you make calls to
insertAtHead("am")
insertAtHead("a")
displayList()
it doesn't use those CL functions and does something else. Maybe do something like:
CL list = CL();
list.insertAtHead("am");
list.insertAtHead("a");
list.displayList();
Since you are using pointers for Nodes, instead of NULL you should probably use nullptr:
public:
CL(void) { head = nullptr; } // constructor
In your displayList(void) function, you're using an unidentified variable x?
cout<<(head->x)<<endl;
Maybe it's supposed to be this instead:
cout<<(head->name)<<endl;
BUT, more importantly, in your displayList(void) function you traverse the linked list using the head pointer, in effect, you end up assigning the end of the list to be equal to the head of the linked list as soon as the displayList(void) function finishes execution. I would recommend changing it to look like this instead:
void LinkList::displayList(void){
Node* temp = head;
while(temp != nullptr){
cout<<(temp->name)<<endl;
temp = temp->Next;
}
}
I hope one of these things helps you fix the issue of undefined behavior you're dealing with! c:
Today I was taught Linked list in class and I wanted to implement it on my own.
Here's the part of the code that I wrote. Note that traverseLL traverses the Linked list and insertAtEnd inserts a new node at the end of the linked list.
I believe I can implement Linked list logic / methods / functions on my own. But my question is, inside insertAtEnd function when I create a newNode with the parameters - my data to be inserted, and nullptr (because inserting at the end), It inserts garbage values (or memory addresses maybe) in my node, ignoring the data passed to the constructor.
using namespace std;
#define NL '\n'
class Node {
public:
int data;
Node* next;
Node (int data, Node* nextPtr=nullptr) {
data = data;
next = nextPtr;
}
};
void insertAtEnd(Node* &head, int data) {
Node* newNode = new Node(data, nullptr); // <---- Issue in this line
// When I do as above, my linkedlist nodes always store garbage values and not the data being passed.
// However, when I un-comment the below line, I get the correct output.
// newNode->data = data;
if (head == nullptr)
head = newNode;
else {
Node* temp = head;
while (temp->next != nullptr)
temp = temp->next;
temp->next = newNode;
}
}
void traverseLL(Node* head) {
if (head == nullptr)
return;
while (head->next) {
cout << head->data << " -> ";
head = head->next;
}
cout << head->data << NL;
}
int main() {
Node* head = nullptr;
insertAtEnd(head, 10);
insertAtEnd(head, 20);
insertAtEnd(head, 30);
traverseLL(head);
return 0;
}
For example, the output for the above code when keeping newNode->data = data line commented, is :
16259544 -> 16258392 -> 16258392
But when I un-comment that line, my output becomes, which is intended:
10 -> 20 -> 30
Why is this happening? Even though I've defined my constructor, why is it not working?
I think the cause for this is the statement data = data in the constructor.
Reason for this:
Before executing the first statement of constructor, the member variables of the class are allocated memory and contain junk/default values, and when the statement data = data is seen the compiler changes the parameter but not the member variable.
As a result, you are seeing junk/garbage values.
To resolve this we can either explicitly specify the member using this or use member initialization syntax.
You can use any of the following workarounds.
Workarounds:
You can change your class constructor code like any of the below formats:
1.
class Node {
public:
int data;
Node* next;
Node (int data, Node* nextPtr=nullptr) {
this->data = data; // we are explicitly specifying which data to use
next = nextPtr;
}
};
class Node {
public:
int data;
Node* next;
Node (int d, Node* nextPtr=nullptr) {
data = d; // as the member variable and local variable are of different names, no conflict
next = nextPtr;
}
};
class Node {
public:
int data;
Node* next;
// use the member initialization syntax
// Here we are initializing the data while allocating memory itself,
// so answer is perfectly right!
Node (int d, Node* nextPtr=nullptr) : data(data), next(nextPtr) {}
};
More on the member initialization and constructor:
https://en.cppreference.com/w/cpp/language/constructor
How do C++ class members get initialized if I don't do it explicitly?
Hope this helps,
Thanks.
UPDATE: The issue was stemming from the fact that the Passenger class does not have a default constructor. I don't understand why that breaks the list implementation though. If someone could leave a comment explaining this to me, I'd appreciate it a lot!
OG Post:
Hello fellow stackoverflowers. I'm working on a simple university assignment but have run into a problem I can't seem to wrap my head around to solve. I'm new to C++ and I'm learning now that implementing generics is not as straightforward in C++ as it is in Java. Inside my LinkedList class there is a struct Node:
struct Node {
T data;
Node *next;
}; // Node
Simple enough. And when I create a list of integers in my main.cpp, the implementation functions as it should.
int main() {
LinkedList<int> integers;
integers.append(1);
integers.append(2);
integers.append(3);
// append, prepend, get, insert, and remove
// functions all work as they should.
} // main
I also have a Passenger class. The problem occurs when I attempt to create a list of Passenger instances.
int main() {
LinkedList<Passenger> passengers;
Passenger passenger1("LastName1", "FirstName1", 0);
Passenger passenger2("LastName2", "FirstName2", 1);
passengers.append(passenger1);
passengers.append(passenger2);
} // main
The error messages read exactly as follows:
./LinkedList.hpp:21:10: error: implicit default constructor
for 'LinkedList<Passenger>::Node' must explicitly initialize
the member 'data' which does not have a default constructor
./LinkedList.cpp:26:23: note: in implicit default constructor
for 'LinkedList<Passenger>::Node' first required here:
Node *newNode = new Node;
The append function in the LinkedList class is the following:
template<class T>
void LinkedList<T>::append(T data) {
Node *newNode = new Node;
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
newNode->next = head;
this->head = newNode;
} else {
Node *ptr = head;
while (ptr->next != NULL) {
ptr = ptr->next;
} // while
ptr->next = newNode;
} // if
length++;
} // append
Thank you so much to anyone who helps me out here!
It appears that you call:
Node *newNode = new Node;
Which is equivalent to
Passenger *newNode = new Passenger;
Which appears to be invalid because Passenger does not have a no argument constructor. I suspect you could fix this by doing:
Node *newNode = new Node(data, nullptr);
I have a structure with two pointers that I am storing inside of an array of the same type. I am unsure of how to access the pointers in each specific index of the array.
class List
{
public:
List();
private:
struct L_Node
{
L_Node *next;
L_Node *prev;
int iValue; // not actually doing anything in this example
}
L_Node head[4];
L_Node tail[4];
}
In the constructor, I am wanting head[0] next to be pointing to tail[0] next, head[1] next to tail[1] next and so on, with *prev to be pointing from tail to head to form a doubly linked list. For head, prev = NULL, and in tail next=NULL. In between the head and tail arrays, I am trying to have some dynamic instances of L_Node.
As of now, in my constructor, I have
head->next = new L_Node;
head->prev = NULL;
tail->next = NULL;
tail->prev = new L_Node;
head->next->next = tail;
tail->prev->prev = head;
It compiles, however I am unsure of how it is behaving. Is head[0] pointing through the dynamic struct to tail[0] and so on?
Thanks in advance for any help or tips.
EDIT:
would a typedef for a pointer such as
typedef L_Node *L_Ptr;
be of help?
From your description, this is what you need in the constructor of List:
List::List()
{
for (int i = 0; i != 4; ++i )
{
head[i].next = tail[i];
tail[i].prev = head[i];
}
}
I would add a constructor in L_Node to have sane initial values for its member data.
L_Node() : next(NULL), prev(NULL), iValue(0) {}
I've just implemented the Linked List. It works perfectly fine but even tough I've seen notation I am unable to create working destructor on Node, that's why it's unimplemented here in code.
I need to implement working destructor on node
Destructor of List but this one is simple I will just use the destructor from Node class(but I need this one).
Make the List friendly to Node so I will not have to use getNext(), but I think I can
handle it myself(not sure how, but I'll find out).
Please look at the code it is perfectly fine, just will work if you copy it.
#include <cstdio>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class Node {
public:
Node(Node* next, int wrt) {
this->next = next;
this->wrt = wrt;
}
Node(const Node& obiekt) {
this->wrt = obiekt.wrt;
this->next = obiekt.next;
}
~Node() {}
void show() {
cout << this->wrt << endl;
}
int getWrt(){
return this->wrt;
}
Node* getNext(){
return this->next;
}
void setNext(Node* node){
this->next = node;
}
private:
Node* next;
int wrt;
};
class List{
public:
List(int wrt){
this->root = new Node(NULL, wrt);
}
List(const List& obiekt){
memcpy(&this->root,&obiekt.root,sizeof(int));
Node* el = obiekt.root->getNext();
Node* curr = this->root;
Node* next;
while(el != NULL){
memcpy(&next,&el,sizeof(int));
curr->setNext(next);
curr = next;
next = curr->getNext();
el = el->getNext();
/* curr->show();
next->show();
el->show(); */
}
}
void add(int wrt){
Node* node = new Node(NULL, wrt);
Node* el = this->root;
while(el->getNext() != NULL){
//el->show();
el = el->getNext();
}
el->setNext(node);
}
void remove(int index){
Node* el = this->root;
if(index == 0){
//deleting old one
this->root = this->root->getNext();
}
else{
int i = 0;
while(el != NULL && i < index - 1){
// el->show();
el = el->getNext();
i++;
}
if(el!=NULL){
Node* toRem = el->getNext();
Node* newNext = toRem->getNext();
el->setNext(newNext);
//deleteing old one
}
}
}
void show(){
Node* el = this->root;
while(el != NULL){
el->show();
el = el->getNext();
}
}
~List(){}
private:
Node* root;
};
int main(){
List* l = new List(1); //first list
l->add(2);
l->add(3);
l->show();
cout << endl;
List* lala = new List(*l); //lala is second list created by copy cosntructor
lala->show();
cout << endl;
lala->add(4);
lala->remove(0);
lala->show();
return 0;
}
I suggest you to start with implementing destructor of List. Since you dynamically allocated memory by using new, you should free it by using delete. (If you used new[], it would be delete[]):
~List()
{
Node* currentNode = this->root; // initialize current node to root
while (currentNode)
{
Node* nextNode = currentNode->getNext(); // get next node
delete currentNode; // delete current
currentNode = nextNode; // set current to "old" next
}
}
Once you have proper destructor, you should try whether your copy constructor is correct:
List* lala = new List(*l);
delete l; // delete list that was used to create copy, shouldn't affect copy
you will find out that your copy constructor is wrong and also causes your application to crash. Why? Because purpose of copy constructor is to create a new object as a copy of an existing object. Your copy constructor just copies pointers assuming sizeof(Node*) equal to sizeof(int). It should look like this:
List(const List& list)
{
// if empty list is being copied:
if (!list.root)
{
this->root = NULL;
return;
}
// create new root:
this->root = new Node(NULL, list.root->getWrt());
Node* list_currentNode = list.root;
Node* this_currentNode = this->root;
while (list_currentNode->getNext())
{
// create new successor:
Node* newNode = new Node(NULL, list_currentNode->getNext()->getWrt());
this_currentNode->setNext(newNode);
this_currentNode = this_currentNode->getNext();
list_currentNode = list_currentNode->getNext();
}
}
Also your function remove is wrong since it "removes" reference to some Node but never frees memory where this Node resides. delete should be called in order to free this memory.
"I need to implement working destructor on node" - No, you don't. Node itself doesn't allocate any memory, thus it shouldn't free any memory. Node shouldn't be responsible for destruction of Node* next nor cleaning memory where it's stored. Don't implement destructor nor copy constructor of Node. You also want to read this: What is The Rule of Three?
"Make the List friendly to Node so I will not have to use getNext()" - You want to say within Node class, that class List is its friend:
class Node
{
friend class List; // <-- that's it
Note that from these 5 headers that you include your code requires only one: <iostream>.
Also note that writing using namespace std; at the beginning of the file is considered bad practice since it may cause names of some of your types become ambiguous. Use it wisely within small scopes or use std:: prefix instead.
The linked list destructor will be called either when delete is used with a previously allocated pointer to a linked list or when a linked list variable goes out of scope (e.g., a local variable is destroyed when returning from a function).
The destructor for the linked list should be responsible to free the memory you previously reserved for the nodes (i.e., using add operation). So, basically, you need to traverse the list of nodes and apply the delete operation on each one of them. There is a little trick: when you are about to delete a node you must be careful not to lose the pointer to the next element (when a node is deleted you cannot be sure that next member will still be valid).
If you want to create a destructor for your Node, it should be quite simple actually.
Here it is:
class Node {
private:
int wrt;
Node* next;
public:
Node(Node* next, int wrt) {
this->next = next;
this->wrt = wrt;
}
// Your desired destructor using recursion
~Node() {
if ( next != NULL )
delete next;
}
};
It's that simple :)
Basically, right before the Node is deleted, if next is not empty, we delete next, which will again call the destructor of next, and if next->next is not empty, again the destructor gets called over and over.
Then in the end all Nodes get deleted.
The recursion takes care of the whole thing :)