King of the hill
King of the hill

Reputation: 17

exception thrown in the pointer of singly linked list

ok, I created a linked list using the Node class and List class. But whenever I run it using the List object, it throws an exception when I return the head in the getHead() function in Node.cpp these are the files that I created please if anyone could know why this exception is coming it would be a blessing

//**NODE.h**
#include<iostream>
using namespace std;

class Node{
private:
    Node* head;
    int data;
public:
    Node();
    Node(int);   //insert data
    void setHead(Node*);
    Node* getHead();
    void setData(int);
    int getData();
    void printData(Node*);
};
//**Node.cpp**
#include"Node.h"

Node::Node(){
    //Node* head = new Node();
    head = NULL;
    data = 0;
}

Node::Node(int DATA){
    //Node* head = new Node();
    head = nullptr;
    data = DATA;
}

int Node::getData(){            //DATA SETTER
    return this->data;
}
void Node::setData(int DATA){   //DATA GETTER
    this->data = DATA;
}

Node* Node::getHead(){          
    return head;
}
void Node::setHead(Node* NextAddress){
    this->head = NextAddress;
}

void Node::printData(Node* node){
    while(node != nullptr){
        cout<< this->data <<"\n";
        node = node->head;
    }
}
//**List.h**
#include"Node.h"

class List : public Node{
private:
   Node* head;
   Node* current;
   Node* prevcurrent;
public:
   List();
   //void setList(int);
   void insertAtBack(Node* , int);
   void insertAtFront(Node* , int);
   bool search(int);
   void print();
   ~List();
};
//**List.cpp**
#include"List.h"

List::List(){
    head=nullptr;
    current=nullptr;
    prevcurrent=nullptr;
}

void List::insertAtBack(Node* head , int Data){
Node* newNode = new Node();
newNode->setData(Data);

newNode->setHead(head);     //sets the address of the next node into the newly added node
head = newNode;
}


void List::insertAtFront(Node* head, int Data) {
    Node* temp = new Node();

}

bool List::search(int num){
Node* temp = head;   
    while (temp->getHead()!=nullptr)
    {
        if (temp->getData() == num)
        {
            return true;
        }
        temp->setHead(temp->getHead());     //itereates the adress in the emp node to store the address of the next node fro the while loop
    }
    
}

void List::print(){
    Node* temp=head;
    while (temp->getHead() != nullptr)
    {
        cout<<temp->getData()<<" ";
    }
    
}

List::~List() {
    Node* temp = head;
    while (temp->getHead() != nullptr)
    {
        Node* temp2 = temp;
        temp = temp->getHead();
        delete temp2;
    }
}
//**main.cpp**
#include"List.h"
int main(){
// cout<<"running\n";
// Node box(5);         this works
// cout<<box.getData();
List obj;
Node node;              //this will throw an exception
obj.insertAtBack(&node,5);
obj.print();
}

Upvotes: 0

Views: 82

Answers (1)

Angelos Gkaraleas
Angelos Gkaraleas

Reputation: 400

Exception is thrown due to print:

void List::print() {
    Node *temp = head;
    while (temp->getHead() != nullptr) {
        cout << temp->getData() << " ";
    }
}

Here, a check is missing if temp (or equally head) is a nullptr before you dereference it in temp->getHead()

Same on List::~List

Upvotes: 1

Related Questions