能告诉我为什么错了么,或者直接给个更好地,谢谢。
#include<iostream>
using namespace std;
template <class t>
struct node {
t data;
03. node<t> *next;
05.};
template <class t>
class linklist
{
public:
linklist();
~linklist();
linklist(t a[],int n);
void printlist();
private:
node<t> *first;
};
template <class t>
void linklist<t>::printlist()
{
int p=first->next;
while(p!=NULL)
{
cout<<p->data;
p=p->next;
}
}
template <class t>
linklist<t>::~linklist()
{
while(first!=NULL)
{
q=first;
first=first->next;
delete q;
}
}
template <class t>
linklist<t>::linklist()
{
first=new node;
first->next=NULL;
}
template <class t>
linklist<t>::linklist(t a[],int n)
{int i;
first=new node;first->next=NULL;
for(i=0;i>n;i++)
{
s=new node;s->data=a[i];
s->next=first->next;first->next=s;
}
}
template <class t>
linklist<t>addTwoNumbers(linklist<t>a, linklist<t>b)
02.{
03. linklist *head = NULL, *next = NULL, *l = NULL;
04. int v, m = 0, c = 0, d = 0;
05. if (!a && !b) return NULL;
06.
07. if (a) {c = a->data; a = a->next;}
08. if (b) {d = b->data; b = b->next;}
09. v = c + d; m = v / 10; v %= 10;
10. head = tail = new ListNode(v);
11.
12. while (a && b){
13. v = a->data + b->data + m;
14. m = v / 10;
15. v %= 10;
16. tail->next = new ListNode(v); tail = tail->next;
17. a = a->next;
18. b = b->next;
19. }
20.
21. a ? (l = a): (l = b);
22.
23. while (l){
24. v = l->data + m;
25. m = v / 10;
26. v %= 10;
27. tail->next = new ListNode(v); tail = tail->next;
28. l = l->next;
29. }
30.
31. while (m){
32. v = m % 10;
33. m /= 10;
34. tail->next = new ListNode(v); tail = tail->next;
35. }
36.
37. return head;
38.}
int main()
{
int b[21],g[21];
cout<<"请输入一个21位的整数(每个数字以空格键分开):";
for(int i=0; i<21; i++)
{
cin>>b[i];
}
linklist<int>jb(b,21);
jb.PrintList();
cout<<"请输入另一个21位的整数(每个数字以空格键分开):";
for(int i=0; i<21; i++)
{
cin>>g[i];
}
linklist<int>bb(g,21);
bb.PrintList();
cout<<"结果为:";
addTwoNumbers(jb,bb);
return 0;
}
