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

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;
}

云尖软件开发笔试题

相关推荐
CSharp精选营4 天前
关系型 vs 非关系型:从原理到选型,一文搞定数据库核心分类
数据结构·nosql·关系型数据库·非关系型数据库·技术选型
刘马想放假7 天前
Modbus 全栈技术解析:TCP、RTU、ASCII、RTU over TCP
数据结构·网络协议
北域码匠8 天前
冒泡排序太慢?鸡尾酒排序双向优化,原生 C# 零第三方库完整代码
数据结构·排序算法·泛型·c# 算法·鸡尾酒排序·原生 c# 开发·冒泡排序优化·嵌入式算法
Darling噜啦啦15 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
小小工匠16 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾16 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
Qres82116 天前
算法复键——树状数组
数据结构·算法
牛油果子哥q16 天前
并查集(DSU)超精讲,路径压缩、按秩合并、万能模板、连通性判定、最小生成树与刷题实战全解
数据结构·c++·最小生成树·并查集
凌波粒16 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
疯狂成瘾者16 天前
Java 集合 LinkedList 详解:链表结构、常用方法和队列使用
java·开发语言·链表