zoukankan      html  css  js  c++  java
  • 单链表排序

    从大到小排序.
    链表带头结点, 链表不动, 只对链表里的值进行排序.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    struct list {
        int data;
        struct list *next;
    };
    typedef struct list s_list;
    typedef struct list *p_list;
    
    void getRandomList (p_list p, int n); // 为p随机赋n个值
    void printList (p_list phead); // 打印phead
    void del (p_list p, int pos); // 删除p中第pos个节点
    void swap (int *x, int *y); // 交换两个变量的值
    int getLength (p_list p); // 得到p的长度
    void order (p_list p); // 对p进行排序
    
    int main () {
        int n = 10; // 链表初始值个数
        p_list head = (s_list*)malloc(sizeof(s_list));
        head->next = NULL;
    
        // 随机赋值
        getRandomList(head, n);
        printList(head);
    
        order(head);
        printList(head);
    
        return 0;
    }
    
    // 参数: p为链表头结点, n是需要赋值的个数
    void getRandomList (p_list p, int n) {
        int i = 0;
        p_list temp;
        srand(time(0));
    
        while (n) {
            temp = (s_list*)malloc(sizeof(s_list));
            temp->data = rand() % 150 + 1; // data in [1, 150]
            temp->next = p->next;
            p->next = temp;
            n--;
        }
    }
    
    // 参数: phead为链表的头结点
    void printList (p_list phead) {
        p_list p;
        int length = 0;
        p = phead->next;
        while (p != NULL) {
            printf("%6d", p->data);
            p = p->next;
            length++;
        }
        printf("---Length: %d
    ", length);
    }
    
    // 删除操作
    // 参数: p是要删除链表的头结点, pos是删除的位置
    void del (p_list p, int pos) {
        p_list pre = (s_list*)malloc(sizeof(s_list)); // 要删节点的前一个
        p_list cur = (s_list*)malloc(sizeof(s_list)); // 要删的节点
        int count = 1;
        pre = p;
    
        while (count < pos) {
            pre = pre->next;
            count++;
        }
        cur = pre->next;
        pre->next = cur->next;
        free(cur);
    }
    
    void swap (int *x, int *y) {
        int temp;
        temp = *x;
        *x = *y;
        *y = temp;
    }
    
    // 对链表进行从大到小排序
    // 参数: p要排序的链表
    void order (p_list p) {
        p_list temp = (s_list*)malloc(sizeof(s_list));
        int n = getLength(p); // 链表长度
        int i;
        int j;
    
        /*  假如n=5, 也就是单链表中有5个元素需要排序
            通过分析, 我们需要的是双层循环. 我需要变量像下面这样变化
            i = 4, j = 4
            i = 4, j = 3
            i = 4, j = 2
            i = 4, j = 1
    
            i = 3, j = 3
            i = 3, j = 2
            i = 3, j = 1
    
            i = 2, j = 2
            i = 2, j = 1
    
            i = 1, j = 1
            我下面的循环, 就是为了实现如上所述的效果.
        */
        n = n - 1;
        for (i=n; i>=1; i--) {
            temp = p->next;
            for (j=i; j>=1; j--) {
                // printf("i = %d, j = %d
    ", i ,j);
                if (temp->data < temp->next->data) {
                    swap(&temp->data, &temp->next->data);
                }
                temp = temp->next;
            }
        }
    }
    
    int getLength (p_list p) {
        p_list temp = (s_list*)malloc(sizeof(s_list));
        int length = 0;
        temp = p->next;
        while (temp) {
            temp = temp->next;
            length++;
        }
        return length;
    }
    
  • 相关阅读:
    元类、orm
    MySQL进阶
    python操作mysql
    tf矩阵基础
    tensorflow安装时遇到的问题
    Loading
    弹球落地
    3dMenu
    响应式布局:flex
    渐变linear-gradient
  • 原文地址:https://www.cnblogs.com/asheng2016/p/7636948.html
Copyright © 2011-2022 走看看