/*--------------------------------------------------
写一个string双向链表模块. 通过建立一一个程序设计语言名字的表来
演习这个模块. 为这个表提供一个sort()函数, 并提供一个函数去反转
表中字符串的顺序.
--------------------------------------------------*/
#include <iostream>
#include <string>
using namespace std;
namespace DOUBLY_LINKED // 双向链表的逻辑结构
{
typedef struct DOUBLY_STRING
{
string str;
DOUBLY_STRING *left, *right;
DOUBLY_STRING() {str = "\0"; left = 0; right = 0;}
DOUBLY_STRING(string sstr) {str = sstr; left = 0; right = 0;}
void sort(); // 双向链表排序
void reversion(); // 反转双向链表
} DS, *PDS;
}
namespace DL = DOUBLY_LINKED;
void DL::DS::sort()
{
PDS temp1, temp2, temp3, temp4;
temp4 = temp2 = temp1 = this->right;
while (temp4->str != "end")
{
while (temp1->str != "end")
{
temp3 = temp1;
temp1 = temp3->right;
if (temp1->str != "end")
{
int l = temp1->str.length() >= temp2->str.length()
? temp2->str.length() : temp1->str.length();
for (int i = 0; i < l; i++)
{
if (temp2->str[i] < temp1->str[i])
break;
else if (temp2->str[i] == temp1->str[i])
{
if (i == l-1 && l == temp1->str.length())
{
string tstr;
tstr = temp1->str;
temp1->str = temp2->str;
temp2->str = tstr;
break;
}
continue;
}
else
{
string tstr;
tstr = temp1->str;
temp1->str = temp2->str;
temp2->str = tstr;
break;
}
}
}
else
break;
}
temp2 = temp2->right;
temp4 = temp1 = temp2;
}
}
void DL::DS::reversion()
{
DL::PDS temp1, temp2;
temp2 = temp1 = this;
while (temp1->str != "end")
temp1 = temp1->right;
while (temp2->right != temp1->left && temp2->right->str != "end")
{
string tstr;
tstr = temp1->left->str;
temp1->left->str = temp2->right->str;
temp2->right->str = tstr;
if (temp2->right->right != temp1->left)
{
temp2 = temp2->right;
temp1 = temp1->left;
}
else
break;
}
}
// 创建动态双向链表(首 = "start"; 尾 = "end")
DL::PDS create(DL::PDS cpoint)
{
const DL::PDS start = new DL::DS("start");
DL::PDS temp1 = 0, temp2 = 0;
cout << "Please input the contents of a doubly linked list:\n"
<< "(input \"quit\" to quit)\n";
string *cstr = new string;
cin >> *cstr;
if (*cstr != "quit")
{
temp1 = new DL::DS;
temp1->str = *cstr;
start->right = temp1;
temp1->left = start;
}
else
{
*cstr = "end";
temp1 = new DL::DS;
temp1->str = *cstr;
temp1->left = start;
start->right = temp1;
temp1->right = 0;
cpoint = start;
return cpoint;
}
cstr = new string;
cin >> *cstr;
while (*cstr != "quit")
{
temp2 = temp1;
temp1 = new DL::DS;
temp1->str = *cstr;
temp1->left = temp2;
temp2->right = temp1;
cstr = new string;
cin >> *cstr;
}
*cstr = "end";
temp2 = temp1;
temp1 = new DL::DS;
temp1->str = *cstr;
temp1->left = temp2;
temp2->right = temp1;
temp1->right = 0;
cpoint = start;
return cpoint;
}
// 显示链表的每个结点
void show(DL::PDS spoint)
{
DL::PDS temp1, temp2;
cout << "\nThe contents of a doubly linked list = \n{";
temp2 = temp1 = spoint->right;
if (temp2->str == "end")
{
cout << "No anything!}\n";
return;
}
while (temp2->str != "end")
temp2 = temp2->right;
while (temp1->str != temp2->left->str)
{
cout << temp1->str << ", ";
temp1 = temp1->right;
}
cout << temp2->left->str;
cout << "}\nOver!\n";
}
// 删除动态链表
void deleted(DL::PDS dpoint)
{
string* str;
DL::PDS temp1, temp2;
temp1 = dpoint;
str = &temp1->str;
while (*str != "end")
{
temp2 = temp1;
temp1 = temp2->right;
delete str;
str = &temp1->str;
}
delete str;
}
int main()
{
DL::PDS point = 0;
point = create(point);
show(point);
point->sort();
show(point);
point->reversion();
show(point);
deleted(point);
return 0;
}