链表操作——C、C++(创建链表、反转链表、合并两个有序链表、合并 K个有序链表)

有发现问题可以一起交流哦
注释什么的都有在那了

/* 链表操作:反转链表   合并两个有序链表     合并 K个有序链表 */
#include <stdio.h>
#include <stdlib.h>

#define _DEBUG
#undef PDEBUG
#ifdef _DEBUG
    #define PDEBUG(fmt, args...) printf(fmt, ##args)
#else
    #define PDEBUG(fmt, args...)
#endif

/* 定义链表节点 */
typedef struct node{
    int val;
    struct node *next;
}listnode;

/* 创建链表节点 */
listnode *creatnode(int n);
/* 创建链表 */
listnode *creatlist(int arr[], int length);
/* 打印链表 */
void display(listnode *head);
/* 反转链表 */
listnode *reverlist(listnode *head);
/* 合并两个有序链表 */
listnode *mergeTwolist(listnode *l1, listnode *l2);
/* 合并 K 个有序链表 */
listnode *mergeKlist(listnode **listss, int listsSize);

int main(void)
{
    int arr_1[]={1, 2, 4, 5};
    int arr_2[]={2, 4, 6};
    int arr_3[]={0, 4, 6, 7, 9};
    int length_1 = sizeof(arr_1)/sizeof(arr_1[0]);
    int length_2 = sizeof(arr_2)/sizeof(arr_2[0]);
    int length_3 = sizeof(arr_3)/sizeof(arr_3[0]);
    int i, j;

    listnode *lists[3];

    /* 创建链表 s1 */
    PDEBUG("s1=:");
    lists[0] = creatlist(arr_1, length_1);
    display(lists[0]);
    /* 创建链表 s2 */
    PDEBUG("s2=:");
    lists[1] = creatlist(arr_2, length_2);
    display(lists[1]);
    /* 创建链表 s3 */
    PDEBUG("s3=:");
    lists[2] = creatlist(arr_3, length_3);
    display(lists[2]);

    /* 反转链表 s1 */
    PDEBUG("rever s1=:");
    lists[0] = reverlist(lists[0]);
    display(lists[0]);
    /* 反转链表 s2 */
    PDEBUG("rever s2=:");
    lists[1] = reverlist(lists[1]);
    display(lists[1]);

    lists[0] = reverlist(lists[0]);
    lists[1] = reverlist(lists[1]);
#if 1
    /* 合并链表 s1 和 s2 */
    PDEBUG("mergeTwolist s1+s2=:");
    listnode *s = mergeTwolist(lists[0], lists[1]);
    display(s);
#endif

#if 0
    /* 合并链表 s1 和 s2 和 s3 */
    PDEBUG("mergeKlist s1+s2+s3=:");
    listnode *Ks = mergeKlist(lists, 3);
    display(Ks);
#endif
    return 0;
}

/* 创建链表节点 */
listnode *creatnode(int n)
{
    listnode *p = (listnode *)malloc(sizeof(listnode));
    p->val = n;
    p->next = NULL;

    return p;
}
/* 创建链表 */
listnode *creatlist(int arr[], int length)
{
    listnode *head, *p, *tail;
    int len = length;
    int i;

    for( i=0; i<len; i++ )
    {
        p = creatnode(arr[i]);
        if(0==i)
            head = p;
        else
            tail->next = p;
        tail = p;
    }
    tail->next = NULL;

    return head;
}
/* 打印链表 */
void display(listnode *head)
{
    listnode *p = head;
    while( p != NULL )
    {
        PDEBUG("%d->", p->val);
        p = p->next;
    }
    PDEBUG("\n");
}
/* 反转链表 */
listnode *reverlist(listnode *head)
{
    listnode *pre = NULL;
    listnode *cur = head;
    listnode *tmp = cur;

    while( cur != NULL )
    {
        tmp = cur->next;
        cur->next = pre;
        pre = cur;
        cur = tmp;
    }
    return pre;
}
/* 合并两个有序链表 */
listnode *mergeTwolist(listnode *l1, listnode *l2)
{
    listnode *pre = (listnode *)malloc(sizeof(listnode));
    listnode *newlist = pre;

    while( l1!=NULL && l2!=NULL )
    {
        if( l1->val <= l2->val )
        {
            pre->next = l1;
            l1 = l1->next;
        }
        else{
            pre->next = l2;
            l2 = l2->next;
        }
        pre = pre->next;
    }
    pre->next = l1!=NULL?l1:l2;

    return newlist->next;
}
/* 合并 K 个有序链表 */
listnode *mergeKlist(listnode **listss, int listsSize)
{
    if(listsSize <= 0)
        return NULL;
    if( listsSize == 1 )
        return listss[0];
    if( listsSize == 2 )
        return mergeTwolist(listss[0], listss[1]);

    int interval = 1;
    int i;
    while( interval < listsSize )
    {
        for( i=0; i+interval<listsSize; i+=2*interval )
        {
            listss[i] = mergeTwolist(listss[i], listss[i+interval]);
        }
        interval *= 2;
    }
    return listss[0];
}
发布了18 篇原创文章 · 获赞 2 · 访问量 633

猜你喜欢

转载自blog.csdn.net/weixin_39618542/article/details/102077628