溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

c++實(shí)現(xiàn)廣義表

發(fā)布時(shí)間:2020-06-17 16:00:55 來源:網(wǎng)絡(luò) 閱讀:827 作者:769374355 欄目:編程語言


廣義表是非線性的結(jié)構(gòu),是線性表的一種擴(kuò)展,是有n個(gè)元素組成有限序列。

廣義表的定義是遞歸的,因?yàn)樵诒淼拿枋鲋杏值玫搅吮?,允許表中有表。

    

<1> A = ()

<2> B = (a,b)

<3> C = (a,b,(c,d))

<4> D = (a,b,(c,d),(e,(f),h)) 

<5> E = (((),()))

如下圖所示:

c++實(shí)現(xiàn)廣義表

廣義表主要使用遞歸實(shí)現(xiàn),表與表之間使用鏈?zhǔn)浇Y(jié)構(gòu)來存儲(chǔ)。下面就來看看代碼怎樣實(shí)現(xiàn)的:

#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

//節(jié)點(diǎn)類型
enum Type
{
	HEAD,//頭節(jié)點(diǎn)
	VALUE,//數(shù)據(jù)項(xiàng)
	SUB,//子表的頭節(jié)點(diǎn)
};

//廣義表結(jié)構(gòu)
struct GeneralizedNode
{
public:
	GeneralizedNode()//無參構(gòu)造函數(shù)
		:_type(HEAD)
		, _next(NULL)
	{}

	GeneralizedNode(Type type, char ch = 0)
		:_type(type)
		,_next(NULL)
	{
		if (_type == VALUE)
		{
			_value = ch;//數(shù)據(jù)節(jié)點(diǎn)
		}
		if (_type == SUB)
		{
			_sublink = NULL;//子表節(jié)點(diǎn)
		}
	}

public:
	Type _type;//節(jié)點(diǎn)類型
	GeneralizedNode * _next;
	union
	{
		char _value;//數(shù)據(jù)
		GeneralizedNode * _sublink;//子表的頭指針
	};
};

//廣義表類
class Generalized
{
public:
	Generalized()//無參構(gòu)造函數(shù)
		:_head(new GeneralizedNode(HEAD))
	{}
	Generalized(const char* str)//構(gòu)造函數(shù)
		:_head(NULL)
	{
		_head = _CreateList(str);
	}

	Generalized(const Generalized & g)//拷貝構(gòu)造
	{
		_head = _CopyList(g._head);
	}

	Generalized& operator=(Generalized g)//賦值運(yùn)算符的重載
	{
		swap(_head, g._head);//現(xiàn)代寫法
		return *this;
	}

	~Generalized()//析構(gòu)函數(shù)
	{
		_Delete(_head);//遞歸析構(gòu)
	}

public:
	void print()//打印
	{
		_Print(_head);
	}

	size_t size()//元素個(gè)數(shù)
	{
		size_t ret=_Size(_head);
		return ret;
	}

	size_t depth()//廣義表的深度
	{
		size_t ret=_Depth(_head);
		return ret;
	}

public:
	GeneralizedNode * _CreateList(const char* &str)//創(chuàng)建廣義表
	{
		assert(str&&*str == '(');
		//判斷傳入的廣義表是否正確
		str++;//跳過'('

		GeneralizedNode* head = new GeneralizedNode();//創(chuàng)建頭節(jié)點(diǎn)
		GeneralizedNode* cur = head;
		while (*str)
		{
			if (_IsVaild(*str))
			{
				cur->_next = new GeneralizedNode(VALUE, *str);//創(chuàng)建數(shù)據(jù)節(jié)點(diǎn)
				cur = cur->_next;//節(jié)點(diǎn)后移
				str++;//指針后移
			}
			else if (*str == '(')//子表
			{
				GeneralizedNode* SubNode = new GeneralizedNode(SUB);//創(chuàng)建字表的頭節(jié)點(diǎn)
				SubNode->_sublink = _CreateList(str);//遞歸調(diào)用_CreateList函數(shù)
				cur->_next = SubNode;
				cur = cur->_next;
			}
			else if (*str == ')')//表的結(jié)束
			{
				str++;//跳過')'
				return head;//返回頭節(jié)點(diǎn)
			}
			else//其他字符(' '或者',')
			{
				str++;
			}
		}
		assert(false);//強(qiáng)制判斷程序是否出錯(cuò)
		return head;
	}

	bool _IsVaild(const char ch)//判斷輸入是否合理
	{
		if ((ch >= '0'&&ch <= '9')
			|| (ch >= 'a'&&ch <= 'z')
			|| (ch >= 'A'&&ch <= 'Z'))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	GeneralizedNode* _CopyList(GeneralizedNode* head)//復(fù)制
	{
		GeneralizedNode* cur = head;
		//創(chuàng)建新的頭節(jié)點(diǎn)
		GeneralizedNode* newHead = new GeneralizedNode();
		GeneralizedNode* tmp = newHead;
		while (cur)
		{
			if (cur->_type == VALUE)//數(shù)據(jù)節(jié)點(diǎn)
			{
				tmp->_next = new GeneralizedNode(VALUE, cur->_value);
				tmp = tmp->_next;
			}
			else if (cur->_type == SUB)//子表節(jié)點(diǎn)
			{
				GeneralizedNode* SubNode = new GeneralizedNode(SUB);
				tmp->_next = SubNode;
				SubNode->_sublink = _CopyList(cur->_sublink);//遞歸調(diào)用
				
				tmp = tmp->_next;
			}
			cur = cur->_next;
		}
		return newHead;
	}

	void _Delete(GeneralizedNode * head)
	{
		GeneralizedNode* cur = head;
		while (cur)
		{
			GeneralizedNode* del = cur;
			if (cur->_type == SUB)//子表節(jié)點(diǎn)時(shí)遞歸析構(gòu)
			{
				_Delete(cur->_sublink);
			}
			cur = cur->_next;
			delete del;
		}
	}

	void _Print(GeneralizedNode* head)//打印
	{
		GeneralizedNode* cur = head;
		if (cur == NULL)
		{
			return;
		}
		while (cur)
		{
			if (cur->_type == HEAD)
			{
				cout << "(";
			}
			else if (cur->_type == VALUE&&cur->_next)
			{
				cout << cur->_value<<",";
			}
			else if (cur->_type == VALUE&&cur->_next == NULL)
			{
				cout << cur->_value;
			}
			else if (cur->_type == SUB)
			{
				_Print(cur->_sublink);
				if (cur->_next)
				{
					cout << ",";
				}
			}
			cur = cur->_next;
		}
		if (cur == NULL)
		{
			cout << ")";
			return;
		}
	}

	size_t _Size(GeneralizedNode* head)//元素的個(gè)數(shù)
	{
		GeneralizedNode* cur = head;
		size_t count = 0;
		while (cur)
		{
			if (cur->_type == VALUE)
			{
				count++;
			}
			else if (cur->_type == SUB)
			{
				count += _Size(cur->_sublink);
			}
			cur = cur->_next;
		}
		return count;
	}

	size_t _Depth(GeneralizedNode* head)//廣義表的深度
	{
		GeneralizedNode* cur = head;
		size_t depth = 1;
		while (cur)
		{
			if (cur->_type == VALUE)
			{
				depth++;
			}
			else if (cur->_type == SUB)
			{
				size_t newdepth = _Depth(cur->_sublink);
				if (newdepth++ > depth)//當(dāng)后面子表的深度比現(xiàn)在的深時(shí),更新深度
				{
					depth = newdepth++;
				}
			}
			cur = cur->_next;
		}
		return depth;
	}

private:
	GeneralizedNode * _head;
};

測(cè)試代碼和結(jié)果如下:

void Test()
{
	Generalized g1("(a,b(c,d),(e,(f),h))");
	Generalized g2;
	g1.print();
	cout << endl;
	cout << "g1.size()="<< g1.size() << endl << endl;
	cout << "g1.depth()=" << g1.depth() << endl << endl;

	g2 = g1;
	g2.print();
	cout << endl;
	cout << "g2.size()=" << g1.size() << endl << endl;
	cout << "g2.depth()=" << g1.depth() << endl << endl;
}

c++實(shí)現(xiàn)廣義表

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI