Onam Kwon
Onam Kwon

Reputation: 41

C++ linked list insert_end()

#include <iostream>

using namespace std;

class Node {
    friend class LinkedList;
private:
    int value;
    Node *pNext;
public:
    Node();
    Node(int);
    ~Node();
    
};

class LinkedList {
private:
    Node *pHead;
    Node *pCursor;
public:
    LinkedList();
    void insert_front(int);
    void insert_end(int);
    void insert_after(int, int);
    void delete_node();
    void displayList();
    void reverseSLL();
    int size();
    
};

int main(int argc, const char * argv[]) {
    
    LinkedList SLL;
    
    for(int i=1;i<6;i++) SLL.insert_end(i);
    
    return 0;
}

Node::Node() {
    pNext = NULL;
}
Node::Node(int val) {
    value = val;
    pNext = NULL;
}
Node::~Node() {
    
}
LinkedList::LinkedList() {
    pHead = NULL;
    pCursor = NULL;
}

void LinkedList::insert_front(int val) {
    if(size()==0) pHead = new Node(val);
    else if(size()>0) {
        pCursor = pHead;
        pHead = new Node(val);
        pHead->pNext = pCursor;
    }
    displayList();
    return ;
}
void LinkedList::insert_end(int val) {
    if(size()==0) pHead = new Node(val);
    else if(size()>0) {
        pCursor = pHead;
        while(pCursor!=NULL) pCursor = pCursor->pNext;
        Node *pNewNode = new Node(val);
        pCursor->pNext = pNewNode;
    }
    displayList();
}

void LinkedList::insert_after(int nth, int val) {
    pCursor = pHead;
    Node *pNewNode = new Node(val);
    for(int i=0;i<nth-1;i++) pCursor = pCursor->pNext;
    pCursor->pNext = pNewNode;
    displayList();
}

void LinkedList::delete_node() {
    int val = 0;
    cout<<"Enter a node you want to delete: "; cin>>val;
    pCursor = pHead;
    if(size()==0) return;
    else if(size()==1) delete pCursor;
    else if(size()>1) {
        while(pCursor->value != val) {
            cout<<pCursor->value;
            pCursor = pCursor->pNext;
        }
        if(pCursor->pNext== NULL) delete pCursor;
        else {
            pHead = pCursor->pNext;
            delete pCursor;
        }
    }
    displayList();
}

void LinkedList::displayList() {
    if(size()==0) return;
    else if(size()>0) {
        pCursor = pHead;
        while(pCursor!=NULL) {
            if(pCursor->pNext==NULL) {
                cout<<pCursor->value;
                break;
            }
            cout<<pCursor->value<<"->";
            pCursor = pCursor->pNext;
        }
    } cout<<endl;
}

void LinkedList::reverseSLL() {
    if(size()==0) return;
    else if(size()>0) {
        pCursor = pHead;
        int reversedArr[size()];
        for(int i=0;i<size();i++) {
            reversedArr[i] = pCursor->value;
            pCursor = pCursor->pNext;
        }
        for(int i=size()-1;i>=0;i--) {
            if(i==0) cout<<reversedArr[i];
            cout<<reversedArr[i]<<"->";
        } cout<<endl;
    }
}

int LinkedList::size() {
    int temp = 0;
    pCursor = pHead;
    while(pCursor != NULL) {
        temp ++;
        pCursor = pCursor->pNext;
    }
    return temp;
}

I am trying to make a singly linked list and in "insert_end()" function, I see segmentation fault error. This function is supposed to add an element at the end of the linked list and then prints out the result.

void LinkedList::insert_end(int val) {
    if(size()==0) pHead = new Node(val);
    else if(size()>0) {
        pCursor = pHead;
        while(pCursor!=NULL) pCursor = pCursor->pNext;
        Node *pNewNode = new Node(val);
        pCursor->pNext = pNewNode;
    }
    displayList();
}

I do not know what my problem is. I saw this segmentation fault error in my other functions. What am I supposed to fix?

Upvotes: 0

Views: 75

Answers (1)

user14215102
user14215102

Reputation:

while(pCursor!=NULL) pCursor = pCursor->pNext;

iterates pCursor untill it's NULL

pCursor->pNext = pNewNode;

is now dereferencing the NULL pointer.

What should you be doing? Probably stop one earlier, iterate until pCursor->pNext is NULL and then allocate the new one into pCursor->pNext as you are already doing.

Upvotes: 2

Related Questions