ZBLOG

/****************链表*********************/ struct ListNode//定义链表结构体 { int val; struct ListNode* next; }; struct ListNode* creatlink(int n)//创建长度为n的链表 { struct ListNode* head, * node, * end;//头节点 普通节点 尾结点 head = (struct ListNode*)malloc(sizeof...

这段程序实现了链表的创建、链表相加和打印链表的功能。

补充程序如下:

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

struct ListNode {
    int val;
    struct ListNode* next;
};

struct ListNode* creatlink(int n) {
    struct ListNode* head, * node, * end;
    head = (struct ListNode*)malloc(sizeof(struct ListNode));
    end = head;
    for (int i = 0; i < n; i++) {
        node = (struct ListNode*)malloc(sizeof(struct ListNode));
        scanf("%d", &node->val);
        end->next = node;
        end = node;
    }
    end->next = NULL;
    return head->next;
}

struct ListNode* linksum(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode* head, * node, * end;
    head = (struct ListNode*)malloc(sizeof(struct ListNode));
    end = head;
    
    int carrybit = 0;

    while (l1 != NULL && l2 != NULL) {
        node = (struct ListNode*)malloc(sizeof(struct ListNode));

        if (l1->val + l2->val + carrybit <= 9) {
            node->val = l1->val + l2->val + carrybit;
            carrybit = 0;
        }
        else {
            node->val = l1->val + l2->val + carrybit - 10;
            carrybit = 1;
        }

        end->next = node;
        end = node;

        l1 = l1->next; 
		l2 = l2->next;
    }

	while (l1 != NULL) {
		node =(struct ListNode*)malloc(sizeof(struct ListNod));

		if (l1->val + carrybit <= 9) {
			node->val = l1->val + carrybit;
			carrybit = 0;
		}
		else {
			node->val = l1->val + carrybit - 10;
			carrybit = 1;
		}

		end->next = node;
        end = node;

        l1 = l1->next;
    }

	while (l2 != NULL) {
		node =(struct ListNode*)malloc(sizeof(struct ListNod));

		if (l2->val + carrybit <= 9) {
			node->val = l2->val + carrybit;
			carrybit = 0;
		}
		else {
			node->val = l2->val + carrybit - 10;
			carrybit = 1;
		}

        end->next = node;
        end = node;

        l2= l2 -> next ;
    }

	if (carrybit == 1) {
	    node =(struct ListNode*)malloc(sizeof(struct ListNod));
        node -> val=1;

    	end -> next=node ;
    	end=end -> next; 
    }
    
    end -> next=NULL ;

    return head -> next ;
}

void printflink(struct ListNode* head) {
    while (head != NULL) {  
        printf("%d ", head -> val);
        head=head -> next ; 
    }
	printf("\n");
}

int main() {
    int n,m;
	struct ListNode* link1,*link2,*link3;//定义链表指针
	scanf("%d",&n);
	link1=creatlink(n);//创建长度为n的链表
	scanf("%d",&m);
	link2=creatlink(m);

	printf("链表相加结果为:\n");
	link3=linksum(link1, link2);//两链表相加
	printflink(link3);//输出结果链表

	return 0;
}

在这个补充程序中,我们首先通过调用creatlink函数来创建两个链表 link1link2。然后我们调用 linksum 函数来计算两个链表的和,并将结果保存在 link3 中。最后,我们使用 printflink 函数打印出结果链表的值。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=8937

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?