问题描述 :
目的:使用C++模板设计单链表的抽象数据类型(ADT)。并在此基础上,使用单链表ADT的基本操作,设计并实现单链表的应用算法设计。
内容:(1)请使用模板设计单链表的抽象数据类型。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。参考网盘中的ADT原型文件。)
(2)ADT的应用:使用该ADT设计并实现单链表应用场合的算法设计。
应用:假设2个任意长度的整数x、y分别由带头结点的单链表A和B存储,现要求设计一个算法,实现任意长的整数进行加法运算。
参考函数原型:
template
void Long_Int_Add( LinkList &A, LinkList &B, string &result, const int &len_A, const int &len_B );
辅助函数原型:
(1)从长整数的低位开始拆分(4位为一组,即不超过9999的非负整数),依次存放在单链表的每个结点的数据域中;头结点的数据域存放正负数标志(正数或0:1,负数:-1)。
template
void Input_Int_Division( LinkList &L, string &str, int &length ); (length:长整数分割后的block数,恰为存储用单链表的长度)
(2)计算结果中间位格式控制
string Int_String( int result );
(3)两个长整数的绝对值大小比较(x>y 返回值为1;x
template
int Two_LongNum_Compare( LinkList &A, LinkList &B, const int &len_A, const int &len_B );
(4)单链表ADT基本操作:单链表的逆置(实际计算时,鉴于单链表的顺序查找的特性,存储在单链表中的长整数需逆置,由原始的高位到低位排列,逆置为低位到高位降序排列。)
template
void LinkList::ListReverse();
输入说明 :
第一行:长整数x
第二行:长整数y
输出说明 :
第一行:格式化后的长整数x(从低位到高位每4位用","分开)
第二行:格式化后的长整数y(从低位到高位每4位用","分开)
第三行:空行
第四行:格式化后的计算结果(从低位到高位每4位用","分开)
(输入与输出之间用一空行分隔)
输入范例 :
-53456467576846547658679870988098
435643754856985679
输出范例 :
-5345,6467,5768,4654,7658,6798,7098,8098
43,5643,7548,5698,5679
-5345,6467,5768,4611,2014,9250,1400,2419
image.png
#include
#include
#include
using namespace std;
struct list
{
int data;
list* prior;
list* next;
};
int toint(string::iterator str, int n)
{
int m = 0;
for (int i = 0;i < n;i++)
{
m *= 10;
m += str[i] - '0';
}
return m;
}
void creat(string str, list** head, list** end)
{
list* p, * q;
q = *head = new list;
(*head)->prior = NULL;
if (str[0] == '-')
{
(*head)->data = -1;
str.erase(str.begin());
}
else (*head)->data = 1;
int i, len = str.length();
i = len % 4;
if (i != 0)
{
p = new list;
p->data = toint(str.begin(), i);
(*head)->next = p;
p->prior = *head;
q = p;
}
for (;i < len;i += 4)
{
p = new list;
p->data = toint(str.begin() + i, 4);
q->next = p;
p->prior = q;
q = p;
}
q->next = NULL;
*end = q;
}
void show(list* head)
{
list* p = head;
if (head == NULL)return;
if (head->data == -1)cout << '-';
p = head->next;
cout << p->data;
if (p->next == NULL)cout << endl;
else
{
for (p = p->next;p->next != NULL;p = p->next)
{
cout << ',' << setfill('0') << setw(4) << p->data;
}
cout << ',' << setfill('0') << setw(4) << p->data << endl;
}
}
int compare(string s1, string s2)
{
if (s1[0] == '-')s1.erase(s1.begin());
if (s2[0] == '-')s2.erase(s1.begin());
if (s1 < s2)return -1;
if (s1 == s2)return 0;
return 1;
}
void change(list* head)
{
list* p;
if (head == NULL)return;
for (p = head->next;p != NULL;p = p->next)
p->data *= -1;
}
void erase(list* head)
{
list* p, * q;
if (head == NULL || head->next == NULL)return;
for (p = head, q = head->next;q->data == 0 && q->next != NULL;)
{
p->next = q->next;
delete q;
q = p->next;
q->prior = p;
}
}
void add(string s1, string s2)
{
list* head1, * end1, * head2, * end2;
creat(s1, &head1, &end1);
creat(s2, &head2, &end2);
show(head1);
show(head2);
list* head, * end;
head = new list;
end = new list;
head->prior = NULL;
end->next = NULL;
if (head1->data == -1)change(head1);
if (head2->data == -1)change(head2);
list* p, * q, * r, * s;
for (p = end1, q = end2, r = NULL;p != head1 && q != head2;p = p->prior, q = q->prior)
{
if (r == NULL)
{
r = new list;
r->data = p->data + q->data;
r->next = NULL;
end = r;
}
else
{
s = new list;
s->data = p->data + q->data;
s->next = r;
r->prior = s;
r = s;
}
}
if (p == head1)
{
for (;q != head2;q = q->prior)
{
s = new list;
s->data = q->data;
s->next = r;
r->prior = s;
r = s;
}
}
else if (q == head2)
{
for (;p != head1;p = p->prior)
{
s = new list;
s->data = p->data;
s->next = r;
r->prior = s;
r = s;
}
}
if (r != NULL)
{
head->next = r;
r->prior = head;
}
else
{
head->next = end;
end->prior = head;
end->data = 0;
head->data = 1;
}
if (r != NULL && r->data < 0)head->data = -1;
else head->data = 1;
int m = 0;
if (head->data == 1)
{
for (p = end, q = end->prior;q != head;p = q, q = q->prior)
{
p->data += m;
if (p->data < 0)
{
m = -1;
p->data += 10000;
}
else if (p->data >= 0 && p->data < 10000)m = 0;
else if (p->data >= 10000)
{
m = 1;
p->data -= 10000;
}
}
p->data += m;
if (p->data >= 10000)
{
p->data -= 10000;
q = new list;
q->data = 1;
head->next = q;
q->prior = head;
q->next = p;
p->prior = q;
}
}
else
{
for (p = end, q = end->prior;q != head;p = q, q = q->prior)
{
p->data += m;
if (p->data <= -10000)
{
m = -1;
p->data += 10000;
}
else if (p->data <= 0 && p->data > -10000)m = 0;
else if (p->data > 0)
{
m = 1;
p->data -= 10000;
}
}
p->data += m;
if (p->data <= -10000)
{
p->data += 10000;
q = new list;
q->data = -1;
head->next = q;
q->prior = head;
q->next = p;
p->prior = q;
}
change(head);
}
cout << endl;
erase(head);
//if (head == NULL)cout << endl;
//else
//{
// p = head->next;
// cout << p->data;
// if (p->next == NULL)cout << endl;
// else {
// for (p = p->next;p != NULL;p = p->next)
// cout << "->" << setfill('0') << setw(4) << p->data;
// cout << endl;
// }
//}
show(head);
}
int main()
{
string s1, s2;
cin >> s1 >> s2;
add(s1, s2);
return 0;
}