0

Edit: Just tried doing while (temp != NULL && newNode->info > temp->info){ and it still doesn't work for some reason, I tried inputting 5 and 4 and got a segmentation error again

Sorry for the bad code I'm kind of new to this, I'm trying to make a sorted singly linked list. I'm not sure what's wrong with it and would greatly appreciate if somebody could help me with this issue?

I was able to perhaps input a couple values, each time a different number of values for some reason (not because I inputted -1). And then it just says "Exception has occurred. Segmentation error." on this particular line, and I'm not sure why since I was careful to compare values instead of the memory address:

while (newNode->info > temp->info){

Full code:

#include <iostream>
using namespace std;

class node {
public:
    int info;
    node *next;

    node (int data, node *ptr = 0) {
        info = data;
        next = ptr;
    }
};

class osll{

    public:
    node *head, *tail;

    osll(){
        head = tail = 0;
    }

    bool isEmpty(){
        return head == 0;
    }

    void sort(int input){

        node *newNode = new node (input);

        if (isEmpty()){
            newNode ->next = head;
            head = newNode;
            if (tail == 0)
                tail = head;
        }
        if (newNode ->info > head ->info){

            node *temp = head;
            while (newNode->info > temp->info){
                temp = temp ->next;
            }
            
            // will figure out how to link newNode to 
            // whatever temp value that stops here
            // once this error goes away
        }
    }

    
};



int main () {
    osll l;
    int input = 0;

    while (input != -1) {
        cout << "Enter a value: ";
        cin >> input;
        l.sort(input);
    }

    return 0;

}
5
  • 1
    What happens in your while loop if temp becomes a null pointer? Commented Sep 3, 2021 at 21:01
  • Did you try searching the internet first? I believe I saw the same question title within the last week. Commented Sep 3, 2021 at 21:08
  • Check for temp != NULL also in while loop Commented Sep 3, 2021 at 21:14
  • It seems that if I keep adding values in a decreasing manner it still works but only until an increase does it not seem to work, so 5 4 3 2 1 works but 5 4 3 88 returned a segmentation fault message Commented Sep 3, 2021 at 21:45
  • The programmer's secret weapon is the debugger. With the debugger you could step through the function line by line and keep an eye on the program doing something unexpected like storing the wrong value or taking a bad path. Usually just seeing what the program is doing is enough to shake loose what you should have done instead. If not, what you learn can help you make wickedly targeted minimal reproducible examples that the experts out here can help you resolve with far less effort on everyone's part. Commented Sep 3, 2021 at 22:11

1 Answer 1

1

If your new number is the largest in the list it causes a segmentation-fault because you don't check if you reached the end of the list in the while-loop. After the last element temp will be null, so .temp->info will cause a segmentation-fault.

So you should do for example (line 40)

while (temp != null && newNode->info > temp->info)
Sign up to request clarification or add additional context in comments.

4 Comments

Just tried this with inputting 3 and 5 and it still doesn't work, I'm not sure why
I fixed only the seg-fault. I didn't finished the code, so it doesn't to the linking, but you no longer get a seg-fault. If you do please make sure you saved and re-complied the code (easy to forget) before running.
@minhanhb never assume you have only one bug. the code also seems to discard nodes if newNode ->info > head ->info is not true.
It discards all the time. It links nowhere. But in case 3 5 newNode ->info > head ->info is true

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.