美文网首页
003-双向链表频次排序算法

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

作者: 浪尖的游鱼 | 来源:发表于2019-09-29 09:41 被阅读0次

总概

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

个人解决时长: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 <iostream>
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; i<m;i++){
        ptr[i] = p1;
        p1 = p1->next;
    }
    //将链表对应数组
    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; i<n; i++) {
        cout << a[i] << " ";
    }
    //输出数组测试
    head = ptr[0];
    ptr[0]->previous = nullptr;
    ptr[0]->next = ptr[1];
    for (int i = 1; i<m-1; i++) {
        ptr[i ]->previous = 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-双向链表频次排序算法

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

  • 双向链表的快速排序(swift版本)

    面试经常会被问到的单向链表的快速排序or双向链表的快速排序,现在用swift写了一个双向链表的快速排序,直接上代码...

  • 如何设计一个内存缓存库

    双向链表 + LRU淘汰算法 + 线程安全 双向链表的设计 用OC来设计双向链表(不是循环链表) 单个节点 整个链...

  • 将二叉搜索树转化为排序的双向链表

    题目 将二叉搜索树转化为排序的双向链表 例如,下图二叉搜索树转换为图中的排序的双向链表。 解析 转换为排序的双向链...

  • Go语言数据结构和算法-DoubleLinkedList(双向链

    Go语言数据结构和算法-DoubleLinkedList(双向链表) 双向链表的数据结构 Prepend(val)...

  • 二叉排序树转双向链表

    排序二叉树转换为排序双向链表 题目:输入一棵二叉排序树,将该二叉排序树转换成一个排序的带头结点的双向链表。如: 转...

  • 总结

    Android篇 数据结构与算法顺序表 - ArrayList源码链表 - 单向链表、双向链表 - LinkedL...

  • 单链表线性表

    用链表来表示线性表,还有循环链表,双向链表其中算法很多都是相同的,循环链表的特点是最后一个指针指向头指针,双向链表...

  • Java实现每日一道算法面试题(20):leecode23 合并

    1.算法题目 合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。 示例: 2.算法思路 算法思...

  • 排序二叉树转换为排序双向链表

    题目:输入一棵二叉排序树,将该二叉排序树转换成一个排序的带头结点的双向链表。如: 转换成双向链表4<->6<->8...

网友评论

      本文标题:003-双向链表频次排序算法

      本文链接:https://www.haomeiwen.com/subject/ddyiyctx.html