数据结构-广义表

广义表是非线性的结构,是线性表的一种扩展,是有n个元素组成有限序列。

广义表的定义是递归的,因为在表的描述中又得到了表,允许表中有表。   

<1> A = ()

<2> B = (a,b)

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

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

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


主要代码:

//广义表结构(链表套链表)
#pragma once

#include <iostream>
#include <assert.h>

using namespace std;

enum Type
{
	HEAD,
	VALUE,
	SUB
};

struct GeneralizedNode
{
	Type _type;
	GeneralizedNode* _next;

	union
	{
		char _value;
		GeneralizedNode* _sublink;
	};

	GeneralizedNode(Type type, char value=' ')
		: _type(type)
		, _next(NULL)
	{
		if (_type == VALUE)
		{
			_value = value;
		}
		else if (_type == SUB)
		{
			_sublink = NULL;
		}
	}
};


class Generalized
{
public:
	typedef GeneralizedNode Node;
public:
	Generalized(const char* s="()")
	{
		_head = _Creat(s);
	}

	Generalized(const Generalized& g)
	{
		_head = _Copy(g._head);
	}

	Generalized& operator=(Generalized g)
	{
		swap(_head,g._head);
		return *this;
	}

	~Generalized()
	{
		Destroy(_head);
		_head = NULL;
	}

	void Print()
	{
		_Print(_head);
		cout << endl;
	}

	size_t Size()
	{
		return _Size(_head);
	}

	size_t Depth()
	{
		return _Depth(_head);
	}
protected:
	Node* _Creat(const char*& s)
	{
		assert(*s == '(');

		Node* head = new Node(HEAD);
		Node* tail = head;
		++s;

		while (*s != '\0')
		{
			if (IsValue(*s))
			{
				tail->_next = new Node(VALUE, *s);
				tail = tail->_next;
				++s;
			}
			else if (*s == '(')
			{
				tail->_next = new Node(SUB);
				tail = tail->_next;
				tail->_sublink = _Creat(s);
			}
			else if (*s == ')')
			{
				++s;
				return head;
			}
			else
			{
				++s;
			}
		}
	}

	Node* _Copy(Node* head)
	{
		Node* cur = head->_next;
		Node* newhead = new Node(HEAD);
		Node* tail = newhead;

		while (cur)
		{
			if (cur->_type == VALUE)
			{
				tail->_next = new Node(VALUE, cur->_value);
				tail = tail->_next;
			}
			else if (cur->_type == SUB)
			{
				tail->_next = new Node(SUB);
				tail = tail->_next;
				tail->_sublink = _Copy(cur->_sublink);
			}
			else
			{
				assert(false);
			}
			cur = cur->_next;
		}

		return newhead;
	}

	void Destroy(Node* head)
	{
		if (NULL == head)
		{
			return;
		}
		Node* cur = head->_next;
		Node* del = head;

		while (cur)
		{
			if (cur->_type == SUB)
			{
				Destroy(cur->_sublink);
			}
			delete del;
			del = cur;
			cur = cur->_next;
		}
		delete del;
	}

	void _Print(Node* head)
	{
		assert(head);
		
		Node* cur = head->_next;
		cout << "(";
		while (cur)
		{
			if (cur->_type == VALUE)
			{
				cout << cur->_value;
				if (cur->_next)
					cout << ",";		
			}
			else if (cur->_type == SUB)
			{
				_Print(cur->_sublink);
				if (cur->_next)
					cout << ",";
			}
			else
			{
				assert(false);
			}
			cur = cur->_next;
		}
		cout << ")";
		
	}

	size_t _Size(Node* head)
	{
		assert(_head);

		Node* cur = head->_next;
		size_t count = 0;

		while (cur)
		{
			if (cur->_type == VALUE)
			{
				count += 1;
			}
			else if (cur->_type == SUB)
			{
				count += _Size(cur->_sublink);
			}
			else
			{
				assert(false);
			}
			cur = cur->_next;
		}

		return count;
	}

	size_t _Depth(Node* head)
	{
		assert(head);

		size_t size = 0;
		size_t maxsize = 1;
		Node* cur = head->_next;

		while (cur)
		{
			if (cur->_type == SUB)
			{
				size = _Depth(cur->_sublink) + 1;
				if (size > maxsize)
				{
					maxsize = size;
				}
			}

			cur = cur->_next;
		}

		return maxsize;
	}
protected:
	Node* _head;
private:
	bool IsValue(const char c)  //判断给定字符串当前位置的字符是否是有效的字符(规定数字和字母为有效的字符)
	{
		if ((c >= '0' && c <= '9')
			|| (c >= 'a' && c <= 'z')
			|| (c >= 'A' && c <= 'Z'))
			return true;
		return false;
	}
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值