大数减法
思路
- 以字符串形式读入每一个数
- 将每个字符转化为对应的数字,并以单链表存储
- 判断长度,用长度长的减去长度小的中对应的每一位(若不足,需做进位处理)
代码实现
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int length1=0,length2=0;
typedef struct node{
int data;
struct node *next;
}node;
void print(node *p){
while(p->next!=NULL){
p=p->next;
printf("%d",p->data);
}
}
void dele(node *s){
node *p;
while(s!=NULL){
p=s;
s=s->next;
free(p);
}
}
int compete(node *first1,node *first2){
node *s1=(node *)malloc(sizeof(node));
node *s2=(node *)malloc(sizeof(node));
s1->next=NULL;s2->next=NULL;
while(first1->next!=NULL){
first1=first1->next;first2=first2->next;
node *p=(node *)malloc(sizeof(node));
p->data=first1->data;
p->next=s1->next;
s1->next=p;
node *q=(node *)malloc(sizeof(node));
q->data=first2->data;
q->next=s2