003-双向链表频次排序算法

总概

前几天突然有位出国深造的同学妹妹扔过来一道教授的算法题,哈哈,不知道为嘛自己一个菜鸡为嘛这么热情。

个人解决时长:90min
语言:C++
重点:数据结构链表以及操作,算法设计
遇到的难点:很久没用C++,边复习边做的
题目概述:按照频次排序数组,记住是频次

题目

//CIS554 HW1
//Due: 11:59PM, Friday ( September 13)
//Do not modify main funaiton.
//Do not introduce new functions
//In-place implementation is required.

#include 
using namespace std;

class Node {
public:
    int value;
    Node* next;
    Node* previous;
    Node(int i) { value = i; next = previous = nullptr; }
    Node() { next = previous = nullptr; }
};

class DoublyLinkedList {
public:
    Node * head;
    Node* tail;
    DoublyLinkedList() { head = tail = nullptr; }
    void makeRandomList(int m,int n);
    void printForward();
    void printBackward();

    //inplement the following member functions:

    void sort(int m,int n);//sort all values based on frequency in ascending order.
                //In case of ties, the smaller values will appear earlier
                //Example:  for list with values  7 6 12 4 33 12 6 6 7
                //sorted results: 4 33 7 7 12 12 6 6 6
                //If your algorithm is inefficient, you might lose points.
                //You will not modify L.
};

void DoublyLinkedList::sort(int m,int n) {

};
void DoublyLinkedList::makeRandomList(int m, int n) {
    for (int i = 0; i < m; i++) {
        Node* p1 = new Node(rand() % n);
        p1->previous = tail;
        if (tail != nullptr) tail->next = p1;
        tail = p1;
        if (head == nullptr) head = p1;
    }
}



void DoublyLinkedList::printForward() {
    cout << endl;
    Node* p1 = head;
    while (p1 != nullptr) {
        cout << p1->value << " ";
        p1 = p1->next;
    }
}



void DoublyLinkedList::printBackward() {
    cout << endl;
    Node* p1 = tail;
    while (p1 != nullptr) {
        cout << p1->value << " ";
        p1 = p1->previous;
    }
}



int main() {
    DoublyLinkedList d1;
    d1.makeRandomList(30, 20);
    d1.printForward();
    d1.printBackward();
    d1.sort(30,20);//数组长度
    d1.printForward();
    d1.printBackward();
    cout << endl;
    getchar();
    getchar();
    return 0;
}

题目说明都在代码备注里面了,不多说了,其实频次排序倒也是不难(也不是一下子能想出来的),难点其实是链表。所以直接走了一个非常讨巧的方法:

    Node** ptr = new Node*[m];
    Node* p1 = head;
    Node* temp;
    for(int i = 0; inext;
    }
    //将链表对应数组
    int *a = new int[n];
    for (int i = 0; i < n; i++) {
        a[i] = 0;
    }
    for (int i = 0; i < m; i++) {
        a[ptr[i]->value] ++;
    }
    //统计频次
    int min_num ;
    int max_index;
    bool flag = true;
    int index = 0;
    while (flag)
    {
        flag = false;
        min_num = m + 1;
        max_index = 0;
        for (int j = 0; j < n; j++) {
            if (a[j] < min_num) {
                min_num = a[j];
                max_index = j;
            }
        }
        a[max_index] = m+1;
        //找到频次min的,置最大
        for (int i = index; i < m; i++) {
            if (ptr[i]->value == max_index) {
                temp = ptr[i];
                ptr[i] = ptr[index];
                ptr[index] = temp;
                index++;
            }
        }
        //将频次最大的向前滚
        if (index < m-1) {
            flag = true;
        }
        //是否继续循环
    }
    //排序数组
    for (int i = 0; iprevious = nullptr;
    ptr[0]->next = ptr[1];
    for (int i = 1; iprevious = ptr[i-1];
        ptr[i ]->next = ptr[i+1];
    }
    tail = ptr[m - 1];
    ptr[m-1]->next = nullptr;
    ptr[m - 1]->previous = ptr[m - 2];
    //数组还原链表

将链表映射为数组再进行排序,最后再根据数组重构链表。
至于频次排序,只是用了非常简单的统计再排序。
大家有什么更好的想法欢迎交流!

你可能感兴趣的:(003-双向链表频次排序算法)