将无序数组插入有序链表

使用场景:将生存周期短的数组存储到生存周期长的链表上,增加记录信息,减少长数组的内存开销

过程:
1、将无序数组排序
2、将有序数组插入有序链表

  • 链表为空的情况
  • 链表长度为1的情况
  • 链表长度大于1的情况

当然我这种做法比较复杂,但是优点是无反复申请释放内存,用有序链表插入有序链表简单一些。

c代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define TYEPCHANGE 1
#define TYPE_NOT_CHANGE 0

/************** 有无序数组的结构体 *************/
typedef struct aa{
    int blocknum;
    int blocktype[40];
}AA;

/************** 定义链表结构 *************/
typedef struct node{
    int blocktype;
    struct node *next;
}NODE;

/************** 有序链表结构 *************/
typedef struct bb{
    int a;
    struct node *node_list;;
}BB;

/************** 冒泡排序 *************/
void sort(int *array, int num)
{
    int temp;
    int i,j;
    for (i=0; i<num-1; i++){
        for(j=0; j<num-i-1; j++){
            if(array[j] > array[j+1]){
                temp = array[j];
                array[j] = array[j+1];
                array[j+1] = temp;
            }
        }
    }
}

/************** 调试打印链表函数 *************/
void print(NODE *tmp){
    int i=0;
    NODE *cur = tmp;
    if (cur == NULL)
        return;
    //printf("********* list is: *********\r\n");
    while(cur){
        printf("%d->", cur->blocktype);
        cur = cur ->next;
    }
    printf("\r\n");
    printf("\r\n");
}

/************** 调试打印数组函数 *************/
void print_a(AA t_a){
    int i=0;
    //printf("****** blocknum is %d , after sort is:*******\r\n", t_a.blocknum);
    for(; i<t_a.blocknum; i++){
        printf("%d,", t_a.blocktype[i]);
    }
    printf("\r\n");
}

/************** 有序数组插入有序链表 *************/
static int co_block_type_code(NODE **node_list_par, AA *array_par){
    int index = 0;
    int ret = TYPE_NOT_CHANGE;
    
    NODE *next_t, *cur, *pre;
    NODE *blocktype_node_tmp;

    cur = *node_list_par;
    
    /****  避免空指针 ****/
    if (cur == NULL)
        next_t = NULL;
    else
        next_t = cur->next;

    while(cur && next_t && index < array_par->blocknum){
        /****** 插入的数小于链表最左边的数,将此数插到最左边,并以此为头 ******/
        if (cur->blocktype > array_par->blocktype[index]){
            blocktype_node_tmp = (NODE *)malloc(sizeof(NODE));
            if (NULL == blocktype_node_tmp){
                return TYEPCHANGE;
            }
            memset(blocktype_node_tmp, 0, sizeof(NODE));
            blocktype_node_tmp->blocktype = array_par->blocktype[index];
            blocktype_node_tmp->next = cur;
            next_t = cur;
            cur = blocktype_node_tmp;
            *node_list_par = blocktype_node_tmp;
            index++;
            ret = TYEPCHANGE;
        }
        
        else if (cur->blocktype == array_par->blocktype[index]) {
            while(index < array_par->blocknum && cur->blocktype == array_par->blocktype[index])
                index++;
        } 
        /****** 插入的数位于链表两个数之间 ******/
        else if (cur->blocktype < array_par->blocktype[index] && next_t->blocktype > array_par->blocktype[index]){
            blocktype_node_tmp = (NODE *)malloc(sizeof(NODE));
            if (NULL == blocktype_node_tmp){
                return TYEPCHANGE;
            }
            memset(blocktype_node_tmp, 0, sizeof(NODE));
            blocktype_node_tmp->blocktype = array_par->blocktype[index];
            cur->next = blocktype_node_tmp;
            blocktype_node_tmp->next = next_t;
            cur = blocktype_node_tmp;
            index++;
            ret = TYEPCHANGE;
        }
        /****** 插入的数大于于链表相邻的两个数 ******/
        else{
            cur = next_t;
            if (NULL == cur->next)
                break;
            next_t = cur->next;
        }
    }
    
    /**************** 链表长度为1 start ****************/
    if(index < array_par->blocknum && cur && cur->blocktype > array_par->blocktype[index]){
        blocktype_node_tmp = (NODE *)malloc(sizeof(NODE));
        if (NULL == blocktype_node_tmp){
            return TYEPCHANGE;
        }
        memset(blocktype_node_tmp, 0, sizeof(NODE));
        blocktype_node_tmp->blocktype = array_par->blocktype[index];
        blocktype_node_tmp->next = cur;
        *node_list_par = blocktype_node_tmp;
        pre = blocktype_node_tmp;
        index++;
        ret = TYEPCHANGE;
    }
    
    while(index < array_par->blocknum && cur && cur->blocktype > array_par->blocktype[index]){
        if ( array_par->blocktype[index] == array_par->blocktype[index-1]){
            index++;
            continue;
        }
        
        blocktype_node_tmp = (NODE *)malloc(sizeof(NODE));
        if (NULL == blocktype_node_tmp){
            return TYEPCHANGE;
        }
        memset(blocktype_node_tmp, 0, sizeof(NODE));
        blocktype_node_tmp->blocktype = array_par->blocktype[index];
        pre->next = blocktype_node_tmp;
        blocktype_node_tmp->next = cur;
        pre = blocktype_node_tmp;
        index++;
    }
    /************** 链表长度为1 end *************/
    
    /****** 已经走到了链表的尽头,或者链表长度为空 ******/
    for(;index < array_par->blocknum; index++) {
        /****** 去重 ******/
        while(index < array_par->blocknum && cur && cur->blocktype == array_par->blocktype[index])
            index++;

        if (index >= array_par->blocknum)
            break;
        
        ret = TYEPCHANGE;
        blocktype_node_tmp = (NODE *)malloc(sizeof(NODE));
        if (NULL == blocktype_node_tmp){
            return TYEPCHANGE;
        }
        memset(blocktype_node_tmp, 0, sizeof(NODE));
        blocktype_node_tmp->blocktype = array_par->blocktype[index];
        blocktype_node_tmp->next = NULL;
        if (cur==NULL){
            *node_list_par = blocktype_node_tmp;
            cur = *node_list_par;
        }
        else{
            cur->next = blocktype_node_tmp;
            cur = blocktype_node_tmp;
            ret = TYEPCHANGE;
        }
    }
    return ret;
}

void free_all(BB *temp_b){
    NODE *cur, *next;
    cur = temp_b->node_list;
    while(cur){
        next = cur->next;
        free(cur);
        cur = next;
    }
    
}

void print_status(int ret){
	if (ret == TYEPCHANGE)
        printf("list is changed!\r\n");
    else
        printf("list is not changed!\r\n");
}

int main(int argc, char *argv[]){
    AA a0, a1, a2, a3, a4, a5;
    BB b1;
    b1.node_list = NULL;
    int ret;
    
    a0.blocknum = 1;
    a0.blocktype[0] = 3;
    
    a1.blocknum = 6;
    a1.blocktype[0] = 23;
    a1.blocktype[1] = 3;
    a1.blocktype[2] = 12;
    a1.blocktype[3] = 23;
    a1.blocktype[4] = 3;
    a1.blocktype[5] = 27;

    a2.blocknum = 16;
    a2.blocktype[0] = 213;
    a2.blocktype[1] = 31;
    a2.blocktype[2] = 112;
    a2.blocktype[3] = 4;
    a2.blocktype[4] = 71;
    a2.blocktype[5] = 1;
    a2.blocktype[6] = 32;
    a2.blocktype[7] = 123;
    a2.blocktype[8] = 13;
    a2.blocktype[9] = 21;
    a2.blocktype[10] = 12;
    a2.blocktype[11] = 1;
    a2.blocktype[12] = 34;
    a2.blocktype[13] = 9;
    a2.blocktype[14] = 12;
    a2.blocktype[15] = 14;
    
    a3.blocknum = 3;
    a3.blocktype[0] = 18;
    a3.blocktype[1] = 10;
    a3.blocktype[2] = 31;
    
    a4.blocknum = 4;
    a4.blocktype[0] = 220;
    a4.blocktype[1] = 22;
    a4.blocktype[2] = 1;
    a4.blocktype[3] = 0;
    
    a5.blocknum = 0;
    
    sort(a0.blocktype, a0.blocknum);
    sort(a1.blocktype, a1.blocknum);
    sort(a2.blocktype, a2.blocknum);
    sort(a3.blocktype, a3.blocknum);
    sort(a4.blocktype, a4.blocknum);
    sort(a5.blocktype, a5.blocknum);
    
    
    ret = co_block_type_code(&b1.node_list, &a0);
	print_status(ret);
    printf("array a0 is :  ");
    print_a(a0);
    printf("after insert a0 is:  ");
    print(b1.node_list);
    
    
    ret = co_block_type_code(&b1.node_list, &a1);
	print_status(ret);
    printf("array a1 is :  ");
    print_a(a1);
    printf("after insert a1 is:  ");
    print(b1.node_list);

    
    
    ret = co_block_type_code(&b1.node_list, &a2);
	print_status(ret);
    printf("array a2 is :  ");
    print_a(a2);
    printf("after insert a2 is:  ");
    print(b1.node_list);
    
    
    ret = co_block_type_code(&b1.node_list, &a3);
    print_status(ret);
    printf("array a3 is :  ");
    print_a(a3);
    printf("after insert a3 is:  ");
    print(b1.node_list);
    
    
    ret = co_block_type_code(&b1.node_list, &a4);
    print_status(ret);
    printf("array a4 is :  ");
    print_a(a4);
    printf("after insert a4 is:  ");
    print(b1.node_list);
    
    
    ret = co_block_type_code(&b1.node_list, &a5);
    print_status(ret);
    printf("array a5 is :  ");
    print_a(a5);
    printf("after insert a5 is:  ");
    print(b1.node_list);
    
    free_all(&b1);
}
结果:
list is changed!
array a0 is :  3,
after insert a0 is:  3->

list is changed!
array a1 is :  3,3,12,23,23,27,
after insert a1 is:  3->12->23->27->

list is changed!
array a2 is :  1,1,4,9,12,12,13,14,21,31,32,34,71,112,123,213,
after insert a2 is:  1->3->4->9->12->13->14->21->23->27->31->32->34->71->112->123->213->

list is changed!
array a3 is :  10,18,31,
after insert a3 is:  1->3->4->9->10->12->13->14->18->21->23->27->31->32->34->71->112->123->213->

list is changed!
array a4 is :  0,1,22,220,
after insert a4 is:  0->1->3->4->9->10->12->13->14->18->21->22->23->27->31->32->34->71->112->123->213->220->

list is not changed!
array a5 is :  
after insert a5 is:  0->1->3->4->9->10->12->13->14->18->21->22->23->27->31->32->34->71->112->123->213->220->

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值