0

I made a Linked-List with only an Insert Node function and a Print function, but It doesnt work.

#ifndef LIST_H_
#define LIST_H_
#include <iostream>
using namespace std;

struct Node{
    int data;
    Node* next;
};

class List{

private:
    Node* head;

public:
    List(){
        head = NULL;
    }

    void insertEnd(int d){
        Node* newNode = new Node;
        newNode->next = NULL;
        newNode->data = d;

        if (head == NULL){
            head = newNode;
            return;
        }

        Node* cu = head;

        while (cu != NULL)
            cu = cu->next;
        cu->next = newNode;
    }

    void printList(){
        Node* temp = new Node;
        temp = head;
        while (temp != NULL){
            cout << temp->data << ", ";
            temp = temp->next;
        }
    }

};

And my main function:

#include <iostream>
#include "List.h"
using namespace std;

int main(){

List list1;

list1.insertEnd(1);
list1.insertEnd(2);
list1.insertEnd(3);

//list1.printList();

return 0;

}

This program works if I only insert one node, but if I do anything else it crashes and doesn't give me any error indications or anything.

I've checked on several sites if my pointers were doing the right thing, and I think they are, but what is going wrong here...?

Edit: fixed the problem... in while loop should have been

while (cu->next != NULL)
6
  • It most certainly will give you an error. If you're running this via a bat, add a pause at the end so you can read the error. Commented Jun 18, 2017 at 22:59
  • 1
    for example Node* cu = new Node; cu = head; - think this exist sense ? Commented Jun 18, 2017 at 23:00
  • insertEnd, printList() - completely wrong. Node* temp = new Node; temp = head; this is c++ ? Commented Jun 18, 2017 at 23:01
  • 2
    You might want to use a debugger if can't find out by yourself what happens. FYI cu->next = newNode will always crash because your while loop doesn't check the proper condition. it should check while(cu->next != null). Your current implementation somewhat ensures your pointer is a nullptr before breaking out of the while loop... Commented Jun 18, 2017 at 23:02
  • 1
    cu->next = newNode; after while (cu != NULL) - so cu - always 0 here Commented Jun 18, 2017 at 23:03

3 Answers 3

2

The function insertEnd is wrong.

After this loop

    while (cu != NULL)
        cu = cu->next;

the pointer cv is equal to NULL. As result the following statement

    cu->next = newNode;

leads to undefined behavior.

The simplest way to append a node to the list is the following

void insertEnd( int d )
{
    Node **last = &head;

    while ( *last != nullptr ) last = &( *last )->next;

    *last = new Node { d, nullptr };
}

The function has only three lines.:)

Take into account that this statement

    Node* temp = new Node;

in the function printList does not make sense and is a reason for a memory leak.

Sign up to request clarification or add additional context in comments.

Comments

1
void insertEnd(int d){ 
        Node* newNode = new Node; 
        newNode->next = NULL; 
        newNode->data = d; 

        if (head == NULL){ 
            head = newNode; 
            return; 
        } 

        Node* cu = head; 

        while (cu->next != NULL) 
            cu = cu->next; 
        cu->next = newNode; 
}

This function will do the trick. You had a few relatively simple problems. First, you were trying to make a copy of head to iterate over your list. Instead of assigning that to a dummy pointer, you were allocating new memory, assigning that new memory to your dummy pointer, and then assigning your head pointer to that dummy pointer. This will create a memory leak, as you will never be able to delete that memory if you lose track of it. I changed this:

Node* cu = new Node;
cu = head

to this:

Node* cu = head;

Second, Your segmentation fault comes when you check if cu is not null in your while loop. You are setting cu to cu->next in your loop, then checking if cu is null. if cu is null, then you assign cu->next to your new node. Your null pointer doesn't reference any memory, so trying to reference its members gives you a segment fault. You want to access the last possible valid pointer in your linked list. To do that, you check to see if cu->next is null. I changed this:

while (cu != NULL)
            cu = cu->next;

To This:

while (cu->next != NULL) 
            cu = cu->next;

Comments

1

Your while loop is incorrect. Change it to cu->next from cu

while (cu->next != NULL)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.