链表A + B

      有两个用链表表示的整数,每个节点包含一个数位,这些数位是反向存放的,也就是个位排在链表的首部。编写函数对这两个整数求和,并用链表形式返回结构。

* 链表通用数据结构

list.h

#ifndef _LIST_H_
#define _LIST_H_

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

typedef struct Node_
{
	int    data;
	struct Node_ *next;
}Node;

typedef struct List_
{
	Node *head;
	Node *tail;
	int  length;
}List;

Node* Node_Create(int data);
List* List_Create();
int list_is_empty(List *list);
int List_Add(List *list, int data);
int List_Delete(List *list, int pos);

typedef void (*List_Use)(int);
int List_Search(List *list, List_Use list_use);
#endif
list.c

#include "list.h"

Node* Node_Create(int data)   //创建节点
{
	Node *node = (Node*)malloc(sizeof(Node));
	if (node != NULL) {
		node->data = data;
		return node;
	}
}

int list_is_empty(List *list)
{
	return ((list->head == NULL) && (list->tail == NULL));
}

List* List_Create()   //创建链表
{
	List *list = (List*)malloc(sizeof(List));
	if (list != NULL){
		list->head = NULL;
		list->tail = NULL;
		return list;
	}
}

int List_Add(List *list, int data)
{
#ifdef LIST_ADD_TAIL				//1.单向循环链表尾插入节点
	Node *node = Node_Create(data);
	if (list == NULL)
		return -1;
	else {
		if (list_is_empty(list)) {
			list->head = node;
			list->tail = node;
			node->next = NULL;	
		}
		else {
			node->next = list->tail->next;
			list->tail->next = node;
			list->tail = node;
		}
	}
#endif

#ifdef LIST_ADD_HEAD					//2.单向循环链表头插入节
	Node *node = Node_Create(data);
	if (list == NULL)
		return -1;
	else {
		if (list_is_empty(list)) {
			list->head = node;
			list->tail = node;
			node->next = NULL;	
		}
		else {
			node->next = list->head;
			list->head = node;
		}
	}
#endif
	list->length++;
	return 0;
}

int List_Search(List *list, List_Use list_use)
{
	Node *current = list->head;
	if (list == NULL || list_is_empty(list))
		return -1;
	else {
		do {
			list_use(current->data);
			current = current->next;
		}while (current != NULL);
	}
	return 0;
}
main.c

#include "list.h"

void List_Plus(List *listA, List *listB, List *listC)    //链表节点相加,并把结果重新插入到新链表中
{
	Node *currentA = listA->head;
	Node *currentB = listB->head;
	int sum = 0;
	int jinwei = 0;
	int digit = 0;
	int a = 0;
	int b = 0;
	while (currentA != NULL || currentB != NULL) {
		a = (currentA != NULL ? currentA->data : 0);
		b = (currentB != NULL ? currentB->data : 0);
		sum = a + b + jinwei;
		digit = sum % 10;
		jinwei = sum / 10;
		printf("%d ",digit);
		List_Add(listC,digit);
		currentA = (currentA != NULL ? currentA->next : NULL);
		currentB = (currentB != NULL ? currentB->next : NULL);
	}
	if (sum > 9)
		List_Add(listC,jinwei);
		printf("%d ",jinwei);
}

void List_Print(int data)
{
	printf("%d ",data);
}

int main(int argc,char *argv[])
{
	int i;
	List *listA = List_Create();
	List *listB = List_Create();
	List *listC = List_Create();
	int a[4] = {9, 9, 9, 9};
	int b[4] = {9, 9, 9, 9};

	for (i = 0; i < 4; i++)
		List_Add(listA, a[i]);

	for (i = 0; i < 4; i++)
		List_Add(listB, b[i]);

	List_Plus(listA, listB, listC);
	List_Search(listA,List_Print);
	printf("\n");
	List_Search(listC,List_Print);
	printf("\n");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值