zoukankan      html  css  js  c++  java
  • 单链表的链接(0954)swust-oj

    Description

    建立长度为n的单链表A和长度为m的单链表B,n>0,m>0。编程实现将B表链接在A表的尾端,形成一个单链表A。数据类型指定为字符型。

    Input

    一行为A表的长度n; 第二行为A表中的数据元素 第三行为B表的长度m; 第四行为B表中的数据元素。

    Output

    输出为链接好后的A表中的所有数据元素。

    Sample Input
    4
    A B C D
    6
    1 2 3 4 5 6
    Sample Output

     A B C D 1 2 3 4 5 6


    代码:

    #include<stdio.h>

    #include<malloc.h>
    typedef struct node
    {
    char data[50];//定义字符数组
    node * next;
    }Node;
    void Create(Node*&L,int n)//不一定需要有a[],,,,尾插法建立链表
    {
    Node* s,*r;
    int i;
    L=(Node*)malloc(sizeof(node));
    r=L;
    for(i=0;i<n;i++)
    {
    s=(Node*)malloc(sizeof(node));
    scanf("%s",s->data);//读入数据
    r->next=s;
    r=s;
    }
    r->next=NULL;
    }
    void output(Node*L)//输出函数
    {
    Node*read;
    read=L->next;
    while(read->next)
    {
    printf("%s ",read->data);
    read=read->next;
    }
    printf("%s",read->data);
    }
    void combine(Node*a,Node*b)//连接函数*a和*b是前后两次输入的
    {
    Node *q;
    q=a->next;//q先指向第一次输入的链表的头结点
    while(q->next)
    {
    q=q->next;     //先输入的
    }
    q->next=b->next;   //当第一次输入的都遍历完之后,q指针就指向第二次输入的链表
    }

    int main()
    {
    int i;
    int n;
    Node*a,*b;
    scanf("%d",&n);
    Create(a,n);
    int m;
    scanf("%d",&m);
    Create(b,m);
    combine(a,b);
    output(a);
    free(b);
    return 0;
    }

  • 相关阅读:
    csu 1513 Kick the ball! 搜索
    训练赛bug总结
    csu 1780 简单的图论问题? 搜索
    贪吃蛇
    hdu 1541 Stars 树状数组
    FZU 2092 收集水晶 BFS记忆化搜索
    [ An Ac a Day ^_^ ] UVALive 2035 The Monocycle BFS
    52. N皇后 II
    修改全局变量-global 修改外部嵌套函数中的变量 nonlocal
    安装glove 不报错
  • 原文地址:https://www.cnblogs.com/FENGXUUEILIN/p/4396378.html
Copyright © 2011-2022 走看看