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

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

云尖软件开发笔试题

相关推荐
羑悻的小杀马特1 分钟前
RBTree(红黑树)的介绍和实现
数据结构·c++·红黑树
Bug退退退1231 小时前
LeetCode18.四数之和
java·数据结构·算法
一个不喜欢and不会代码的码农1 小时前
设计一个尽可能高效的划分算法,满足|n1-n2|最小且|S1-S2|最大
数据结构·算法·排序算法
CharlesC++2 小时前
一元n次多项式加法【数据结构-链表】
数据结构·链表
竹竹零2 小时前
JAVA队列
java·开发语言·数据结构·个人开发
代码雕刻家2 小时前
数据结构-5.4.二叉树的性质
数据结构·算法
Beginner x_u2 小时前
2017年-2021年 软件工程程序设计题(算法题)实战_c语言程序设计&&数据结构程序设计分析
c语言·数据结构·算法·软件工程
手捧向日葵的话语2 小时前
手撕数据结构 —— 队列(C语言讲解)
数据结构
手捧向日葵的话语2 小时前
手撕数据结构 —— 栈(C语言讲解)
数据结构