函数作为参数传递实现链表排序.cpp


/*       用 new 和 delete 实现单链表的 增,删,改,查. 释放和显示
*     包括->头插,尾插,有序插.
*/
#include <iostream>

using namespace std;

struct Data {
    int id;
    string name;
    int age;
};
struct Node {
    Data data;
    Node* next;
};


using Functor = bool(*)(const Data&,const Data&);

bool InsertNodeFront(Node*& h, Data a)
{
    h = new Node{a, h};
    return true;
}

bool InsertNodeBack(Node*& h, Data a)
{
    auto t = h;
    auto p = new Node;
    if (p == NULL) {
        return false;
    }
    p->data = a;
    p->next = NULL;

    if (h == NULL) {
        h = p;
    } else {
        while (t->next != NULL) {
            t = t->next;
        }
        t->next = p;
    }
    return true;
}

bool RemoveNodeById(Node*& h, int id)
{
    Node* prev = NULL;
    Node* pfid = h;
    while (pfid!=NULL && pfid->data.id!=id) {
        prev = pfid;
        pfid = pfid->next;
    }
    if (pfid == NULL) {
        return false;
    } else if (pfid == h) {
        h = pfid->next;
    } else if (pfid->next == NULL) {
        prev->next = NULL;
    } else {
        prev->next = pfid->next;
    }

    delete pfid;

    return true;
}


//选择排序:
void SortList1(Node*& h)
{
    Node* newH = NULL;
    while (h != NULL) {
        Node* prev = NULL;
        Node* pmax = h;
        for(Node* p=h; p->next!=NULL; p=p->next) {
            if (p->next->data.id > pmax->data.id) {
                prev = p;
                pmax = p->next;
            }
        }
        if (prev != NULL) {
            prev->next = pmax->next;
        } else {
            h = pmax->next;
        }
        pmax->next = newH;
        newH = pmax;
    }
    h = newH;
}


//插入排序:
void SortList2(Node*& h)
{
    Node* newH = h;
    h = h->next;
    newH->next = NULL;

    while (h != NULL) {
        Node* t = newH;
        Node* prev = NULL;

        Node* p = h;
        h = h->next;

        while (t!=NULL && t->data.id<p->data.id) {
            prev = t;
            t = t->next;
        }
        if (prev != NULL) {
            prev->next = p;
            p->next = t;
        } else {
            p->next = newH;
            newH = p;
        }
    }
    h = newH;
}

bool CmpId(const Data& a, const Data& b)
{
    return a.id < b.id;
}
bool CmpAge(const Data& a, const Data& b)
{
    return a.age < b.age;
}
bool CmpName(const Data& a, const Data& b)
{
    return a.name < b.name;
}

void SortList3(Node*& h, Functor fun)
{
    Node* newH = h;
    h = h->next;
    newH->next = NULL;

    while (h != NULL) {
        Node* t = newH;
        Node* prev = NULL;

        Node* p = h;
        h = h->next;

        while (t!=NULL && fun(t->data, p->data)) {
            prev = t;
            t = t->next;
        }
        if (prev != NULL) {
            prev->next = p;
            p->next = t;
        } else {
            p->next = newH;
            newH = p;
        }
    }
    h = newH;
}

//链表反序:
void reverse(Node*& h)
{
    Node* prev = NULL;
    Node* curr = h;
    Node* next = NULL;

    while (curr != NULL) {
        next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
   
    h = prev;
}


void ShowList(Node* h)
{
    while (h != NULL) {
        cout << "--------------------" << endl;
        cout << "id  : " << h->data.id << endl;
        cout << "name: " << h->data.name << endl;
        cout << "age : " << h->data.age << endl;

        h = h->next;
    }
}

void FreeList(Node*& h)
{
    while (h != NULL) {
        auto t = h;
        h = h->next;
        delete t;
    }
}

int main()
{
    Data a = {1001, "zhangsan", 35};
    Data b = {1002, "lisi", 65};
    Data c = {1004, "wangwu", 32};
    Data d = {1003, "liliu", 34};
    Data e = {1013, "chenqi", 31};
    Node* header = NULL;

    InsertNodeBack(header, c);
    InsertNodeBack(header, b);
    InsertNodeBack(header, d);
    InsertNodeBack(header, a);

    //RemoveNodeById(header, 2);
    ShowList(header);

    cout << "------排序后-----" << endl;

    SortList3(header, CmpAge);
    ShowList(header);

    FreeList(header);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值