string 动态双向链表的创建、排序,反转等

/*--------------------------------------------------
写一个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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值