美文网首页
链表逆序排序

链表逆序排序

作者: python小青 | 来源:发表于2019-10-17 14:40 被阅读0次

代码如下:

struct NodeList{

    int data;

    struct NodeList * next;

};

void test(){

    struct NodeList * header = malloc(sizeof (struct NodeList));

    struct NodeList * node2 = malloc(sizeof (struct NodeList));

    struct NodeList * node3 = malloc(sizeof (struct NodeList));

    struct NodeList * node4 = malloc(sizeof (struct NodeList));

    struct NodeList * node5 = malloc(sizeof (struct NodeList));

    header->data = -1;

    header->next = node2;

    node2->data = 200;

    node2->next = node3;

    node3->data = 300;

    node3->next = node4;

    node4->data = 400;

    node4->next = node5;

    node5->data = 500;

    node5->next = NULL;

    //逆序排序

    if(header->next == NULL){

        return;

    }

    struct NodeList * preNode = NULL;

    struct NodeList * currNode = header->next;

    struct NodeList * nextNode = currNode->next;

    while (nextNode != NULL) {

        currNode->next = preNode;

        preNode = currNode;

        currNode = nextNode;

        nextNode = currNode->next;

    }

    currNode->next = preNode;

    header->next = currNode;

    struct NodeList * theCurNode = header;

    while (theCurNode->next != NULL) {

        printf("--%d\n",theCurNode->next->data);

        theCurNode = theCurNode->next;

    }

    //释放

     theCurNode = header;

     struct NodeList * tempNode = NULL;

     while (theCurNode != NULL) {

         tempNode = theCurNode;

         theCurNode = theCurNode->next;

         free(tempNode);

         tempNode = NULL;

     }

}

int main()

{

  test();

return 0;

}

打印结果:

代码截图:

相关文章

  • 链表逆序排序

    代码如下: struct NodeList{ int data; struct NodeList * ne...

  • Python 将链表逆序

    说明:链表逆序,是将链表中的单向链表逆序,双向链表逆序和正序都是一样的,所以没有任何意义。 代码: class N...

  • MS(10):数据结构算法篇

    一、排序 最快的排序算法是哪个?给阿里2万多名员工按年龄排序应该选择哪个算法?堆和树的区别;写出快排代码;链表逆序...

  • Day9

    学习内容:数据链表(2/2)收获: 进一步掌握了单向链表的构建、遍历、查找、释放、删除、排序及逆序等操作; 完成了...

  • Java 数组的排序、逆序

    数组的排序、逆序测试数据 数组选择排序 数组冒泡排序 数组逆序

  • 2.单链表

    该部分包含以下内容-单链表的增删改查-计算链表长度-逆序链表-寻找(删除)链表倒数第K个元素-逆序打印链表(使用栈)

  • LeetCode 2. Add Two Numbers

    单链表逆序相加

  • LeetCode算法题及优秀解答精选

    0.知识点全解 笔试面试知识整理-知识共享 <剑指offer> 1. LeetCode分类解题 单链表逆序 从排序...

  • 链表逆序

    定义ListNode节点结构体 例题:链表链接 LeetCode 206. Reverse Linked Lis...

  • 链表逆序

    -(void)reverse(node *head) { node*per,; node *current = h...

网友评论

      本文标题:链表逆序排序

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