将一个单向链表插入到一个循环链表尾部

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

typedef char datatype;
typedef struct node
{
    datatype data;
    struct node *next;
} NODE_S;

//创建循环链表
NODE_S *createCircleLinkList()
{
    NODE_S *p = (NODE_S *)malloc(sizeof(NODE_S));
    if (NULL == p)
    {
        perror("p malloc err");
        return NULL;
    }
    p->next = p;
    return p;
}

//创建单向链表
NODE_S *createLinkList()
{
    NODE_S *h = (NODE_S *)malloc(sizeof(NODE_S));
    if (NULL == h)
    {
        perror("h malloc err");
        return NULL;
    }
    h->next = NULL;
    return h;
}
//单向链表长度
int lengthLinkList(NODE_S *h)
{
    int i = 0;
    while (h->next != NULL)
    {
        h = h->next;
        i++;
    }
    return i;
}

//循环链表插入数据
int insertIntoCircleLinkList(NODE_S *p,int post,datatype data)
{
    NODE_S *pnew = (NODE_S *)malloc(sizeof(NODE_S));
    if (NULL == pnew)
    {
        perror("pnew malloc err");
        return -1;
    }
    NODE_S *q = p;
    pnew->data = data;
    pnew->next = NULL;
    for (int i = 0; i < post; i++)
    {
        q = q->next;
    }
    pnew->next = p;
    q->next = pnew;
    return 0;
}
//单向链表插入数据
int insertIntoLinkList(NODE_S *h, int post, datatype data)
{
    if (post < 0 || post > lengthLinkList(h))
    {
        printf("insertIntoLinkList err");
        return -1;
    }
    NODE_S *hnew = (NODE_S *)malloc(sizeof(NODE_S));
    if (NULL == hnew)
    {
        perror("pnew malloc err");
        return -1;
    }
    hnew->data = data;
    hnew->next = NULL;
    for (int i = 0; i < post; i++)
    {
        h = h->next;
    }
    hnew->next = h->next;
    h->next = hnew;
    return 0;
}

NODE_S *insertLinklistToCircle(NODE_S *p, NODE_S *h)
{
    NODE_S *t = p;
    NODE_S *r = h;
    while (t->next != NULL)
    {
        t=t->next;
        if (t->next == p)
        {
            r = h->next;
            h->next=NULL;
            t->next = r;
        }
    }
    t->next = p;
    return p;
}

//单向循环链表遍历
void showCircleLinkList(NODE_S *p)
{
    NODE_S *q = p->next;
    do
    {
        printf("%c ",q->data);
        q=q->next;
    } while (q != p);
    printf("\n");
}
int main(int argc, char const *argv[])
{
    NODE_S *p = createCircleLinkList();
    NODE_S *h = createLinkList();
    insertIntoCircleLinkList(p,0,'A');
    insertIntoCircleLinkList(p,1,'B');
    insertIntoCircleLinkList(p,2,'C');
    insertIntoCircleLinkList(p,3,'D');
    insertIntoLinkList(h,0,'E');
    insertIntoLinkList(h,1,'F');
    insertIntoLinkList(h,2,'G');
    insertLinklistToCircle(p,h);
    showCircleLinkList(p);
    return 0;
}

云尖软件开发笔试题

相关推荐
今后12342 分钟前
【数据结构】二叉树的概念
数据结构·二叉树
散11213 小时前
01数据结构-01背包问题
数据结构
消失的旧时光-194314 小时前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
Gu_shiwww14 小时前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
苏小瀚15 小时前
[数据结构] 排序
数据结构
_不会dp不改名_17 小时前
leetcode_21 合并两个有序链表
算法·leetcode·链表
睡不醒的kun17 小时前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌17 小时前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
Whisper_long18 小时前
【数据结构】深入理解堆:概念、应用与实现
数据结构
IAtlantiscsdn18 小时前
Redis7底层数据结构解析
前端·数据结构·bootstrap