栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构之SWUSTOJ954: 单链表的链接

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构之SWUSTOJ954: 单链表的链接

题目:

解析:

这个题两个数据类型不一样,可能大家会疑惑这样两种数据类型不一样的链表怎么才能够链接在一起呢,其实我们只需要将数据类型置为char即可,并不需要设计两个结构体,并且在输入和输出时要注意用%c,因为%c也可以用于输入输出整数的

代码:

#include
#include
typedef struct SLListChar
{
    char data;
    struct SLListChar* next;
}SL;
void SLListInit(SL** plist)
{
    *plist = (SL*)malloc(sizeof(SL));
    (*plist)->next = NULL;
}
void SLListCreate(SL** plist,int n)
{
    SL* cur = *plist;
    char x = 0;
    while (n > 0)
    {
        scanf("%c", &x);
        getchar();//这里的getchar及其重要,因为需要处理空格
        SL* newnode = (SL*)malloc(sizeof(SL));
        newnode->data = x;
        cur->next = newnode;
        cur = newnode;
        n--;
    }
    cur->next = NULL;//最后将末尾置为NULL
}
void SLListMerge(SL** ListA, SL** ListB)
{
    SL* tail = *ListA;
    while (tail->next)//注意结束循环的条件
    {
        tail = tail->next;
    }
    SL* prev = (*ListB)->next;//找到B链表的有效数据节点位
    tail->next = prev;//把它链接在后面
}
void SLPrint(SL** ListA)
{
    SL* cur = (*ListA)->next;
    while (cur)
    {
        printf("%c ", cur->data);
        cur = cur->next;
    }
}
int main()
{
    SL* ListA = NULL;
    SLListInit(&ListA);
    int n1 = 0;
    scanf("%d", &n1);
    getchar();
    SLListCreate(&ListA, n1);

    SL* ListB = NULL;
    SLListInit(&ListB);
    int n2 = 0;
    scanf("%d", &n2);
    getchar();
    SLListCreate(&ListB,n2);

    SLListMerge(&ListA, &ListB);
    SLPrint(&ListA);
    return 0;
}
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/784048.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号