简体   繁体   中英

How to sort a linked-list?

I have a linked-list and I want to sort it in a special order. I tried to use bubble sort. As I have many data types in my struct(called Node), I can't swap the values.

struct Node
{
    int data;
    Node *next;

    Node(int x)
    {
        data = x;
        next = NULL;
    }

    union
    {
        sold s;
        apartment a;
        villa v;

    }u;

};

Actually I can't swap my union values in my_swap function. what I need is a new swap function. here's my code.

#include<iostream>
#include<vector>
#include<string>

using namespace std;

struct adderess {
    char city[50];
    char street[100];
    char alley[100];
    int code;
};

struct apartment {

    float structure_s;
    float price;
    int floor;
    bool elevator;
    adderess adr;

};
struct villa {
    float structure_s;
    float yard_s;
    float price;
    int floor;
    adderess adr;
};

struct sold {
    int type;
    float comission;
    bool con;

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

    Node(int x)
    {
        data = x;
        next = NULL;
    }

    union
    {
        sold s;
        apartment a;
        villa v;

    }u;

};


void print_list(Node *head)
{
    Node *start = head;

    while (start)
    {
        cout << start->data << " -> ";
        start = start->next;
    }
    cout << "\n\n";
}

void my_swap(Node *node_1, Node *node_2)
{
    int temp = node_1->data;
    node_1->data = node_2->data;
    node_2->data = temp;
}
double total_price(Node **n) {
    if ((*n)->data == 1)
        return((*n)->data*(*n)->data);

    else 
        return((*n)->data*(*n)->data*(*n)->data);

}
void bubble_sort(Node *head)
{
    int swapped;

    Node *lPtr; // left pointer will always point to the start of the list
    Node *rPrt = NULL; // right pointer will always point to the end of the list
    do
    {
        swapped = 0;
        lPtr = head;
        while (lPtr->next != rPrt)
        {
            if (total_price(&lPtr) >total_price(& lPtr->next))
            {
                my_swap(lPtr, lPtr->next);
                swapped = 1;
            }
            lPtr = lPtr->next;
        }
        //as the largest element is at the end of the list, assign that to rPtr as there is no need to
        //check already sorted list
        rPrt = lPtr;

    } while (swapped);
}

int main()
{
    Node *head = new Node(2);
    head->next = new Node(1);
    head->next->next = new Node(4);
    head->next->next->next = new Node(3);
    head->next->next->next->next = new Node(6);
    head->next->next->next->next->next = new Node(5);

    cout << "The original list = " << endl;
    print_list(head);


    bubble_sort(head);

    cout << "The Sorted list = " << endl;
    print_list(head);

    return 0;
}

Rather than swapping the values within two nodes, you can swap their positions itself in the linked list. For that you'll need to maintain a prev pointer which is the pointer occurring before lPtr in the linked list.

void my_swap(Node*& head, Node*& prev, Node*& node_1, Node*& node_2)
{
    if (prev == nullptr)
    {
        node_1->next = node_2->next;
        node_2->next = node_1;
        prev = node_2;
        head = node_2;
    }
    else
    {
        node_1->next = node_2->next;
        node_2->next = node_1;
        prev->next = node_2;
        prev = node_2;
    }
}

void bubble_sort(Node *head)
{
    bool swapped;

    Node *prev, *lPtr, *rPtr; // left pointer will always point to the start of the list
    rPtr = nullptr; // right pointer will always point to the end of the list
    do
    {
        swapped = false;
        prev = nullptr;
        lPtr = head;
        while (lPtr->next != rPtr)
        {
            if (total_price(&lPtr) > total_price(&lPtr->next))
            {
                my_swap(head, prev, lPtr, lPtr->next);
                swapped = true;
            }
            else
                lPtr = lPtr->next;
        }
        //as the largest element is at the end of the list, assign that to rPtr as there is no need to
        //check already sorted list
        rPtr = lPtr;

    } while (swapped);
}

I haven't checked if it runs correctly but hopefully you get the idea after going through the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM