经过第一关的操作,已经破译出敌方的任务等级是“Weapons Free”,本关的任务是继续处理情报数据,破解敌方使用的武器和武器的“发射等级”。
首先需要将2个小分队收集的情报数据,合并成一个集合,并去掉重复数据。
第一关:实现2个函数,一个函数是插入一个结点到一个带头结点的链表的尾部,一个函数是实现打印一个链表。
函数接口定义:
在这里描述函数接口。例如:
ptr insertNode(ptr last, int x) ;
void printList(ptr head) ; //注意是带头结点的链表
head是头指针,last是尾指针,insertNode的返回值是最新的尾指针,x是新结点的data值。printList不要打印头结点,只打印真正的元素列表
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
#include <stdio.h> // 定义链表节点结构
typedef struct Node {
int data;
struct Node* next; }*ptr;
ptr insertNode(ptr head, int x) ;
void printList(ptr head) ;
int main() {
struct Node* list1 = (struct Node*)malloc(sizeof(struct Node));
struct Node* list2 = (struct Node*)malloc(sizeof(struct Node));
ptr last1 ,last2; last1=list1;
last2=list2;
list1->next=list2->next=NULL;
int x,i=0,n; // 构建第一个链表
scanf("%d",&n);
while(i<n) { scanf("%d",&x);
last1=insertNode(last1, x); i++; } // 构建第二个链表
scanf("%d",&n);
i=0; while(i<n) {
scanf("%d",&x);
last2=insertNode(last2, x); i++; }
printf("List 1: ");
printList(list1);
printf("List 2: ");
printList(list2);
return 0; } /* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
14
2 3 5 7 6 8 9 10 78 99 98 87 65 67
7
4 7 6 9 10 78 98
输出样例:
在这里给出相应的输出。例如:
List 1:2 3 5 7 6 8 9 10 78 99 98 87 65 67
List 2:4 7 6 9 10 78 98
为了简单输出,尾元素后面有一个空格!
代码实现:
ptr insertNode(ptr last, int x)
{
ptr p;
p=(ptr)malloc(sizeof(struct Node));//定义指针
p->data=x;
last->next=p;
p->next=NULL;
return p;}
void printList(ptr head) {
ptr q;
q = head->next;
while (q)
{
printf("%d ", wyf->data);
q = q->next;
}
printf("\n");//注意空格
}