列表

详情


CC10. 牛牛的双链表求和

描述

牛牛输入了两个长度相同的数组分别是 a 和 b ,然后把数组 a 和 b 转换成链表 a 和链表 b 。把链表 a 中的全部值按顺序加到链表 b 中。    

输入描述

第一行输入一个正整数 n ,表示数组的长度。
第二行和第三行分别输入 n 个正整数,表示数组 a 和 数组 b 的值。

输出描述

把数组 a 和数组 b 转换成链表,然后把链表 a 中的值加到链表 b 中,然后输出加和后的链表。

示例1

输入:

5
5 4 2 1 3
2 4 5 8 9

输出:

7 8 7 9 12

原站题解

C 解法, 执行用时: 2ms, 内存消耗: 296KB, 提交时间: 2022-03-14

#include <stdio.h>
#include <stdlib.h>

typedef struct list{
    int data;
    struct list *next;
}list,*linklist;

//链表内容打印
void ptink(list *head)
{
        struct list *point=head;

        while(point!=NULL)
        {
                printf("%d ",point->data);
                point=point->next;//while里面的判断其实是在判断point->next是否为NULL
        }
        putchar('\n');
}


//头插法 
list *headInsert(list *head1,int *arr1,int n)
{
    int i;
    list *headList = head1;
    list *arrList = NULL;
    for(i=n-1;i>=0;i--)
    {
        arrList = (list *)malloc(sizeof(list));
        arrList->data = arr1[i]; 
        arrList->next = headList;
        headList = arrList;
    }
    head1 = headList;
    return head1;
} 
//尾插法 
list *trailInsert(list *head2,int *arr2,int n)
{
    int i;
    list *headList = head2;
    list *arrList = NULL;
    for(i=0;i<n;i++)
    {
        arrList = (list *)malloc(sizeof(list));
        arrList->data = arr2[i]; 
        arrList->next = NULL; //将headList放在arrList->next,即arrList的数据是放在headList的前面的 
        //headList=head;
        if(headList == NULL)
        {
            headList = arrList;  //
            head2 = headList;
        }
        else
        {
            while(headList->next != NULL)
            {
                headList = headList->next ;
            }
            headList->next = arrList;
        } 
    }
    return head2;
} 
//求和链表 
list *Sum(list *head,list *head1,list *head2)
{
    list *List = NULL;  //每个节点存数的 
    list *list0 = head;  //临时头链表
    
    list *list1 = head1;
    list *list2 = head2; 
    
    while(list1 != NULL)
    {
        List = (list *)malloc(sizeof(list));
        List->data = (list1->data)+ (list2->data) ;
        List->next =NULL;
        //尾插法 
        if(list0 == NULL)
        {
            list0 = List;  //
            head = list0;
        }
        else
        {
            while(list0->next !=NULL)
            {
                list0 = list0->next;  //链表往后位移
            }
            list0->next = List;
        }
        list1 = list1->next;
        list2 = list2->next;
    }
    return head;
}
int main()
{
    int i,n;    
    scanf("%d",&n);
    int arr1[n];    
    int arr2[n];    
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr1[i]); 
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr2[i]); 
    }
    
    list *head = NULL;
    list *head1 = NULL;
    list *head2 = NULL;
    
    head1 = headInsert(head1,arr1,n);
    //ptink(head1);
    head2 = trailInsert(head2,arr2,n);
    //ptink(head2);
    head = Sum( head, head1, head2);
    ptink(head);
    return 0;
}

C 解法, 执行用时: 2ms, 内存消耗: 304KB, 提交时间: 2022-04-07

#include <stdio.h>
#include <stdlib.h>

typedef struct list{
    int data;
    struct list *next;
}list,*linklist;

//链表内容打印
void ptink(list *head)
{
        struct list *point=head;

        while(point!=NULL)
        {
                printf("%d ",point->data);
                point=point->next;//while里面的判断其实是在判断point->next是否为NULL
        }
        putchar('\n');
}

//头插法 
list *headInsert(list *head1,int *arr1,int n)
{
    int i;
    list *headList = head1;
    list *arrList = NULL;
    for(i=n-1;i>=0;i--)
    {
        arrList = (list *)malloc(sizeof(list));
        arrList->data = arr1[i]; 
        arrList->next = headList;
        headList = arrList;
    }
    head1 = headList;
    return head1;
} 
//尾插法 
list *trailInsert(list *head2,int *arr2,int n)
{
    int i;
    list *headList = head2;
    list *arrList = NULL;
    for(i=0;i<n;i++)
    {
        arrList = (list *)malloc(sizeof(list));
        arrList->data = arr2[i]; 
        arrList->next = NULL; //将headList放在arrList->next,即arrList的数据是放在headList的前面的 
        //headList=head;
        if(headList == NULL)
        {
            headList = arrList;  //
            head2 = headList;
        }
        else
        {
            while(headList->next != NULL)
            {
                headList = headList->next ;
            }
            headList->next = arrList;
        } 
    }
    return head2;
} 
//求和链表 
list *Sum(list *head,list *head1,list *head2)
{
    list *List = NULL;  //每个节点存数的 
    list *list0 = head;  //临时头链表
    
    list *list1 = head1;
    list *list2 = head2; 
    
    while(list1 != NULL)
    {
        List = (list *)malloc(sizeof(list));
        List->data = (list1->data)+ (list2->data) ;
        List->next =NULL;
        //尾插法 
        if(list0 == NULL)
        {
            list0 = List;  //
            head = list0;
        }
        else
        {
            while(list0->next !=NULL)
            {
                list0 = list0->next;  //链表往后位移
            }
            list0->next = List;
        }
        list1 = list1->next;
        list2 = list2->next;
    }
    return head;
}
int main()
{
    int i,n;    
    scanf("%d",&n);
    int arr1[n];    
    int arr2[n];    
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr1[i]); 
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr2[i]); 
    }
    
    list *head = NULL;
    list *head1 = NULL;
    list *head2 = NULL;
    
    head1 = headInsert(head1,arr1,n);
    //ptink(head1);
    head2 = trailInsert(head2,arr2,n);
    //ptink(head2);
    head = Sum( head, head1, head2);
    ptink(head);
    return 0;
}



// #include <stdio.h>
// #include <stdlib.h>

// typedef struct link{
//     int data;
//     struct link *next;
// }link, *linklist;

// void SumList(link *la, link *lb)
// {
   
//     link *lc = (link *)malloc(sizeof(link));
//     link *pc = lc;
//     while(la->next != NULL)
//     {
//         link *temp_list = (link *)malloc(sizeof(link));
//         temp_list->data = la->data + lb->data;
//         temp_list->next = NULL;
        
//         pc->next = temp_list;
//         pc = pc->next;
//         la = la->next;
//         lb = lb->next; 
//     }
//     pc = lc;
//     while(pc->next != NULL)
//     {
//         printf("%d ", pc->data);
//         pc = pc->next;
//     }
// }
// int main()
// {
//     int i, n;
//     int temp;
//     scanf("%d", &n);
    
//     link *heada = (link *)malloc(sizeof(link));
//     link *pa = heada;
//     link *headb = (link *)malloc(sizeof(link));
//     link *pb = heada;
    
//     for(i = 0; i<n; i++)
//     {
//         scanf("%d", &temp);
//         link *alist = (link *)malloc(sizeof(link));
//         alist->data = temp;
//         alist->next = NULL;
//         pa->next = alist;
//         pa = pa->next;
//     }
//     for(i=0; i<n; i++)
//     {
//         scanf("%d", &temp);
//         link *blist = (link *)malloc(sizeof(link));
//         blist->data = temp;
//         blist->next = NULL;
//         pb->next = blist;
//         pb = pb->next;
//     }
//     SumList(heada, headb);
//     return 0;  
   
// }

C 解法, 执行用时: 2ms, 内存消耗: 312KB, 提交时间: 2022-07-29

#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
	int num;
	struct Node *next;	
}LinkNode;

LinkNode *creat_link(int n){
    int i;
    LinkNode *head = (LinkNode *)malloc(sizeof(LinkNode));
	LinkNode *tail;
	head->next = NULL;
	scanf("%d", &head->num);
	tail = head;
	for (i = 0; i < n-1; ++i)
	{
		LinkNode *p = (LinkNode *)malloc(sizeof(LinkNode));
		scanf("%d", &p->num);
		p->next = NULL;
		tail->next = p;
		tail = tail->next;
	}
    return head;
}

void add_node(int n, const LinkNode *head_1, LinkNode *head_2){
    int i;
    LinkNode *tail_1 = head_1;
    LinkNode *tail_2 = head_2;
    for(i = 0; i < n; ++i){
        tail_2->num = tail_2->num + tail_1->num;
        tail_1 = tail_1->next;
        tail_2 = tail_2->next;
    }
}

int main(int argc, char const *argv[])
{
	int n,i;
	scanf("%d", &n);
	LinkNode *head_1 = creat_link(n);
    LinkNode *tail;
    LinkNode *head_2 = creat_link(n);
    add_node(n, head_1, head_2);
	tail = head_2;
	while(tail != NULL){
		printf("%d ", tail->num);
		tail = tail->next;
	}
	printf("\n");
	return 0;
}

C 解法, 执行用时: 2ms, 内存消耗: 320KB, 提交时间: 2022-07-30

#include<stdio.h>
typedef struct Node
{
    int value;
    struct Node *link;
}node;
node*Init_list(int n)
{
    node*header=(node*)malloc(sizeof(node));
    header->value=0;
    header->link=NULL;
    node*pRear=header;
    int count=0;
    int v;
    while(count<n)
    {
        scanf("%d",&v);
        node*code=(node*)malloc(sizeof(node));
        code->value=v;
        code->link=NULL;
        pRear->link=code;
        pRear=code;
        count++;
    }
    return header;
}
int main()
{
    int n;
    scanf("%d",&n);
    node*p=Init_list(n)->link;
    node*q=Init_list(n)->link;
    while(p!=NULL)
    {
        p->value+=q->value;
        printf("%d ",p->value);
        p=p->link;
        q=q->link;
}
    
    return 0;
}









C 解法, 执行用时: 2ms, 内存消耗: 320KB, 提交时间: 2022-03-08

#include <stdio.h>
#include <stdlib.h>

typedef struct list{
	int data;
	struct list *next;
}list,*linklist;

//链表内容打印
void ptink(list *head)
{
        struct list *point=head;

        while(point!=NULL)
        {
                printf("%d ",point->data);
                point=point->next;//while里面的判断其实是在判断point->next是否为NULL
        }
        putchar('\n');
}

#if 1
//头插法 
list *headInsert(list *head1,int *arr1,int n)
{
	int i;
	list *headList = head1;
	list *arrList = NULL;
	for(i=n-1;i>=0;i--)
	{
		arrList = (list *)malloc(sizeof(list));
		arrList->data = arr1[i]; 
		arrList->next = headList;
		headList = arrList;
	}
	head1 = headList;
	return head1;
} 
//尾插法 
list *trailInsert(list *head2,int *arr2,int n)
{
	int i;
	list *headList = head2;
	list *arrList = NULL;
	for(i=0;i<n;i++)
	{
		arrList = (list *)malloc(sizeof(list));
		arrList->data = arr2[i]; 
		arrList->next = NULL; //将headList放在arrList->next,即arrList的数据是放在headList的前面的 
		//headList=head;
		if(headList == NULL)
		{
			headList = arrList;  //
			head2 = headList;
		}
		else
		{
			while(headList->next != NULL)
			{
				headList = headList->next ;
			}
			headList->next = arrList;
		} 
	}
	return head2;
} 
//求和链表 
list *Sum(list *head,list *head1,list *head2)
{
	list *List = NULL;  //每个节点存数的 
	list *list0 = head;  //临时头链表
	
	list *list1 = head1;
	list *list2 = head2; 
	
	while(list1 != NULL)
	{
		List = (list *)malloc(sizeof(list));
		List->data = (list1->data)+ (list2->data) ;
		List->next =NULL;
		//尾插法 
		if(list0 == NULL)
		{
			list0 = List;  //
			head = list0;
		}
		else
		{
			while(list0->next !=NULL)
			{
				list0 = list0->next;  //链表往后位移
			}
			list0->next = List;
		}
		list1 = list1->next;
		list2 = list2->next;
	}
	return head;
}
int main()
{
	int i,n;	
	scanf("%d",&n);
	int arr1[n];	
	int arr2[n];	
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr1[i]); 
	}
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr2[i]); 
	}
	
	list *head = NULL;
	list *head1 = NULL;
	list *head2 = NULL;
	
	head1 = headInsert(head1,arr1,n);
	//ptink(head1);
	head2 = trailInsert(head2,arr2,n);
	//ptink(head2);
	head = Sum( head, head1, head2);
	ptink(head);
	return 0;
}

#endif 

上一题