链表A + B
2015-07-24 08:43
363 查看
有两个用链表表示的整数,每个节点包含一个数位,这些数位是反向存放的,也就是个位排在链表的首部。编写函数对这两个整数求和,并用链表形式返回结构。
* 链表通用数据结构
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);
#endiflist.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;
}
* 链表通用数据结构
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);
#endiflist.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;
}
相关文章推荐
- 编程中最没用的东西是源代码,最有用的东西是算法和数据结构。
- Java数据结构-线性表之双向链表
- Java数据结构-线性表之栈(顺序栈和链栈)
- Java数据结构-树及树的存储结构
- Java数据结构-线性表之单链表应用-重复节点的删除
- Java数据结构-线性表之静态链表
- Java数据结构-线性表之链表应用-检测链表是否有环
- Java数据结构-二叉树及其遍历
- Java数据结构-线性表之单链表应用-单链表的逆置
- Java数据结构-线性表之循环链表
- Java数据结构-线性表之队列
- Java数据结构-线性表之单链表应用-单链表合并
- Java数据结构-线性表之单链表LinkedList
- Java数据结构-线性表之顺序表ArrayList
- 最简单的数据结构----线性表
- 数据结构学习-从《大话数据结构》开始
- Java数据结构-二叉树及其遍历
- 课程笔记 09:数据结构(清华) 列表-排序器
- hashMap实现原理
- 数据结构学习--创建链表