Fahad
Fahad

Reputation: 37

Bubble Sort By Moving Nodes in Linked List (C++)

I am doing bubble sort on linked list by comparing values in nodes and then moving nodes but there is a problem in my function. When i run my code it creates nodes fine but when bubblesort() function runs it throws an exception and it says "p2 was nullptr". I don't know what is wrong in my code and any help would be appreciated.

Here is my code:

#include <iostream>
#include <stdlib.h>
using namespace std;

class Node {
public:
    int number;
    Node* next;
};

class LinkedList {
    Node* head;
    Node* tail;
public:
    LinkedList() {
        head = NULL;
        tail = NULL;
    }
    void createnode(int num) {
        Node* temp = new Node;
        temp->number = num;
        temp->next = NULL;
        if (head == NULL) {
            head = temp;
            tail = temp;
        }
        else {
            tail->next = temp;
            tail = temp;
        }
    }
    void bubblesort(int size) {
        Node* temp;
        int i, j, swapped;
        for (i = 0; i <= size; i++){
            temp = head;
            swapped = 0;
            for (j = 0; j < size - i - 1; j++){
                Node* p1 = temp;
                Node* p2 = p1->next;
                if (p1->number > p2->number){
                    Node* temp1 = p2->next;
                    p2->next = p1;
                    p1->next = temp1;
                    temp = p2;
                    swapped = 1;
                }
                temp = temp->next;
            }
            if (swapped == 0)
                break;
        }
    }
    void displaynodes() {
        Node* temp;
        temp = head;
        while (temp != NULL) {
            cout << temp->number << "  ";
            temp = temp->next;
        }
        cout << endl;
    }
};

int main() {
    LinkedList l;
    int size, num;
    cout << "How many Numbers Do You Want to Store: ";
    cin >> size;
    for (int i = 0; i < size; i++) {
        cout << "Enter Number " << i+1 << ": ";
        cin >> num;
        l.createnode(num);
    }
    system("CLS");
    cout << "Data Of Nodes Before Bubble Sort: " << endl;
    l.displaynodes();
    l.bubblesort(size);
    cout << "Data Of Nodes After Bubble Sort: " << endl;
    l.displaynodes();
    system("pause");
}

Upvotes: 0

Views: 404

Answers (1)

asmmo
asmmo

Reputation: 7100

You have many bad coding practice like using namespace std;, bad naming style and using Null instead of nullptr.

However the error was in the sorting snippet. You are trying to swapping the nodes but you can't do that without using the node previous to the current node or using doubly linked list. So in the following code, I swapped the numbers not the nodes.

void bubblesort(int size) {
    int i, j, swapped = 1;
    for (i = 0; swapped && i < size; i++){
        swapped = 0;
        Node* temp = head;
        for (j = 0; j < size - i - 1; j++){

            if (temp->number > temp -> next ->number){
                int tempNumber = temp -> number;
                temp -> number = temp -> next -> number;
                temp -> next -> number = tempNumber;
                swapped = 1;
            }
            temp = temp->next;
        }
    }
}

Upvotes: 1

Related Questions