ky0930

//第二章线性表的记录
//  main.c
//  ds_excecise
//
//  Created by rouge s on 2020/9/28.
//

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ElemType int
const int Range = 100;

typedef struct LNODE{
    ElemType data;
    struct LNODE *next;
}LNODE, *LinkList;

// 函数声明集合
LNODE* make(int n);
void printLNODE(LNODE *p);
void inverseprint(LNODE *p);
int ip(LNODE *p);
int DELmin(LNODE*p);
int sort(LNODE *p);

int main() {
    time_t t;
    srand((unsigned) time(&t));
    printf("\n开始了\n");
    LNODE *p;
    p = make(5);
    printLNODE(p);
    sort(p);
    printf("\n\n");
    printLNODE(p);
    printf("\n结束了\n");
    return 0;
}


LNODE* make(int n){
    LNODE *head, *node, *end;//定义头节点,普通节点,尾部节点;
    head = (LNODE *)malloc(sizeof(LNODE));//分配地址
    end = head;         //若是空链表则头尾节点一样
    for (int i = 0; i < n; i++) {
        node = (LNODE *)malloc(sizeof(LNODE));
        node->data = rand()%Range;
        end->next = node;
        end = node;
    }
    end->next = NULL;//结束创建
    return head;
}


void printLNODE(LNODE *p){
    p = p->next;
    while( p != NULL){
        printf("%d - > ",p->data);
        p = p->next;
    }
}

void inverseprint(LNODE *p){
    p = p ->next;
    ip(p);
}


int ip(LNODE*p){
    if (p == NULL)
        return 0;
    LNODE *N = p;
    p = p ->next;
    ip(p);
    printf("%d - > ",N->data);
    return 0;
}

int DELmin(LNODE*p){
    if (p->next == NULL)
        return 0;
    LNODE *L1=p->next,*L2;
    int count = 1,flag = 1, min = L1->data;
    L1 = L1->next;
    while (L1 != NULL){
        count ++;
        if (L1->data < min){
            flag = count;
            min = L1->data;
        }
        L1 = L1->next;
    }
    printf("\n\n最小值是 %d\n\nflag是%d\n\n",min,flag);
    L1 = p;
    for (count = 1; count <flag; count++){
        L1 =L1->next;
    }
    L2 = L1->next;
    L1->next = L2 ->next;
    free(L2);
    return 1;
}

int sort(LNODE *p){
    if (p -> next == NULL)
        return 0;
    LNODE *yuan=p;
    p = p ->next;
    int flag = 0;
    LNODE *ici;
    while ( flag !=1){
        flag =1;
        ici = p;
        while (p->next != NULL){
            if (p->data > p->next->data){
                flag = 0;
                int temp = p->data;
                p->data = p->next->data;
                p->next->data = temp;
            }
            p = p->next;
        }
    }
    printf("\n\n");
    printLNODE(yuan);
    printf("\n\n");
    return 1;
}
posted @ 2020-09-30 06:32  RougeBW  阅读(76)  评论(0编辑  收藏  举报