题目描述
建立长度为n的单链表A和长度为m的单链表B。编程实现将B表链接在A表的尾端,形成一个单链表A。数据类型指定为字符型。
输入
第一行为A表的长度n; 第二行为A表中的数据元素; 第三行为B表的长度m; 第四行为B表中的数据元素。输出
输出为链接好后的A表中的所有数据元素。样例输入
4 A B C D 6 1 2 3 4 5 6样例输出
A B C D 1 2 3 4 5 6#include<stdio.h> #include<string.h> #include<math.h> #include<malloc.h> #define INF 0x3f3f3f3f #define MAX(x,y) (x>y?x:y) #define MIN(x,y) (x>y?y:x) #define M 10001 typedef struct LNode { char data[M]; struct LNode* next; }LNode; void combine(LNode* a, LNode* b) { LNode* temp = a; while (temp->next) { temp = temp->next; } temp->next = b->next; } void printfLNode(LNode* a) { LNode* temp = a; while (temp->next) { temp = temp->next; printf("%s ", temp->data); } } int main() { LNode* a; LNode* b; int n; scanf("%d", &n); a = (LNode*)malloc(sizeof(LNode)); LNode* temp1 = a; for (int i = 0; i < n; i++) { LNode* p1 = (LNode*)malloc(sizeof(LNode)); scanf("%s", &p1->data); p1->next = NULL; temp1->next = p1; temp1 = p1; } int m; scanf("%d", &m); b = (LNode*)malloc(sizeof(LNode)); LNode* temp2 = b; for (int i = 0; i < m; i++) { LNode* p2 = (LNode*)malloc(sizeof(LNode)); scanf("%s", &p2->data); p2->next = NULL; temp2->next = p2; temp2 = p2; } combine(a,b); printfLNode(a); return 0; }
SWUST OJ954: 单链表的链接
最新推荐文章于 2023-02-03 22:15:27 发布