-1

Im trying to make a linked list where the user has to enter in the values on their own in the console and then they show in the linked list. After entering the first value it shows an error "signal: segmentation fault (core dumped)", how do i fix it. Im a begginer at c++ so i really have no idea of how to fix this, all help is greatly appreciated. Heres the code:

#include <iostream>
using namespace std;

class Node{
public:
    int value;
    Node *next;
public:
    Node(int value, Node *next = nullptr) {
      this->value = value;
      this->next = next;
    }
    int getValue() {return this->value;}
    Node *getNext() {return this->next;}
    void setNext(Node*next) {this->next = next;}
};

class LinkedList {
private:
    Node *head;
public:
    LinkedList() {
      this->head = nullptr;
    }

    ~LinkedList() {
      while(this->head != nullptr) pop();
    }

    friend std::ostream & operator <<(std::ostream &os, const LinkedList &rhs) {
      for(Node *curNode = rhs.head; curNode != nullptr; curNode = curNode->getNext()) {
        os << curNode->getValue();
        if (curNode->getNext() != nullptr) os << "";
      }
      return os;
    }
    
    void push(int value) {
      this->head = new Node(value, this->head);
    }

    int pop() {
      int value;
      if (this->head != nullptr) {
        value = this->head->getValue();
        Node *temp = this->head;
        this->head = this->head->getNext();
        delete temp;
      } else {
        throw std::range_error("List is empty!");
      }
      return value;
    }
};

void append(Node** head_ref, int new_data)
{

Node* new_node = new Node((*head_ref)-> value);

    Node *last = *head_ref;

    new_node->value = new_data;

    new_node->next = NULL;

    if (*head_ref == NULL)
    {
        *head_ref = new_node;
        return;
    }

    while (last->next != NULL)
    {
        last = last->next;
    }

    last->next = new_node;
    return;
}

void printList(Node* n)
{
    while (n != NULL) {
        std::cout << n->value << " ";
        n = n->next;
    }
}

int main()
{
    Node* head = NULL;

  int a;
  cout << "Please enter value 1#: ";
  cin >> a;
    append(&head, a);
  int b;
    cout << "Please enter value 2#: ";
  cin >> b;
    append(&head, b);
  int c;
    cout << "Please enter value 3#: ";
  cin >> c;
    append(&head, c);
  int d;
    cout << "Please enter value 4#: ";
  cin >> d;
    append(&head, d);
  int e;
    
    cout<<"Created Linked list is: ";
    printList(head);
    
    return 0;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
SprYz
  • 1
  • 1
  • Build it with `-g` (i.e. with debugging symbols) and run it with `valgrind`. It will tell you where exactly your memory-related bug strikes. And sometimes it tells you even much more, e.g. in a use-after-free case it gives you the stack trace that allocated the previously existing block and the stack trace that freed it afterwards. – Andrej Podzimek Jun 11 '22 at 13:11
  • Read about cin. Look for working examples. Best not to be given the answer, but to solve the problem yourself. – Bruce Jun 11 '22 at 13:12
  • Further notes: The `append()` function is rather confusing. (1) Make it so that it runs in constant time, not in linear time. This is always possible: You can (a) keep a pointer to the last `next` pointer or (b) prepend elements instead of appending them. Simplicity and efficiency makes debugging easier. (2) You don’t need any `if` or `for` statements in linked list manipulation. You don’t need `nullptr` checks either. (3) Last but not least, `valgrind` may tell you that you are dereferencing a `nullptr` in `while (last->next != NULL) ...` during the first insertion. – Andrej Podzimek Jun 11 '22 at 13:25

1 Answers1

0

So here's one error

void append(Node** head_ref, int new_data)
{
    Node* new_node = new Node((*head_ref)-> value);
    ...


int main()
{
    Node* head = NULL;
    ...
    append(&head, a);

When your program starts head is NULL. Which means the first time you call append the value of *head_ref is NULL, but then you do (*head_ref)-> value which is called dereferencing a null pointer, and will (likely) crash your program.

Since the intent is to set the new node to new_data, why not do it like this?

void append(Node** head_ref, int new_data)
{
    Node* new_node = new Node(new_data);
    ...

There may be many more errors (there usually is when beginners start working with pointers). To fix these problems for yourself you need to learn how to use the debugger that comes with your compiler. It's an essential skill for any programmer, and will save you hours of frustration.

john
  • 85,011
  • 4
  • 57
  • 81