C++ 数据结构 *** 广义表的部分实现

本文介绍了一种广义表的数据结构实现方法,并通过模板类的形式定义了广义表的基本操作,包括输入、输出及深度计算等。此外,还实现了广义表的复制与清理功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这几天学了个广义表,之所以用了几天是因为唐nj的ppt错误点太多,以及周五周六晚上都跟着小酸奶一起出去吃饭。。。

代码如下:
#pragma once
#ifndef HEAD_H
#define HEAD_H

#include<iostream>
using namespace std;


enum nodetype { HEAD, ATOM, LIST };

template<class Type>
class List;

template<class Type>
class Node {
	friend class List<Type>;
private:
	nodetype tag;
	union {
		int ref;
		Type atom;
		Node<Type>* subNode;
	};
	Node<Type>* next;
public:
	Node(nodetype tag=HEAD,Node<Type>* next=NULL);
};
template<class Type>
Node<Type>::Node(nodetype tag,Node<Type>* next) {
	this->tag = tag;
	this->next = next;
}

template<class Type>
class List {
private:
	Node<Type>* head;
	void ShowHelp(Node<Type>* hd)const;
	int DepthHelp(const Node <Type>* hd);
	void ClearHelp(Node<Type>* hd);
	void CreatHelp(Node<Type>*& hd);
	void CopyHelp(const Node<Type> *sourcehd, Node<Type>* &desthd);
public:
	List();
	List(Node<Type>* hd);
	List(const List<Type>& copy);
	~List();
	Node<Type> *First(void)const;
	Node<Type>*Next(Node<Type> *elem)const;
	bool Empty(void)const;
	void Push(Node<Type>*elem);
	void Push(List<Type>& subList);
	int depth(void);
	void Input(void);
	void Output(void);
};
template<class Type>
void List<Type>::ShowHelp(Node<Type>* hd)const {
}
template<class Type>
int List<Type>::DepthHelp(const Node <Type>* hd) {
	if (hd->next == NULL)return 1;
	int subDepth = 0;
	for (Node<Type>* tmp = hd->next; tmp != NULL; tmp = tmp->next) {
		if (tmp->tag == LIST) {
			int curDepth = DepthHelp(tmp->subNode);
			if (curDepth > subDepth)subDepth = curDepth;
		}
	}
	return subDepth + 1;
}
template<class Type>
void List<Type>::ClearHelp(Node<Type>* hd) {
	hd->ref--;
	if (hd->ref == 0) {
		Node<Type>* tmppre, *tmpptr;
		for (tmppre = hd, tmpptr = hd->next; tmpptr != NULL; tmppre = tmpptr, tmpptr=tmpptr->next) {
			delete tmppre;
			if (tmpptr->tag == LIST)
				ClearHelp(tmpptr->subNode);
		}
		delete tmppre;
	}
}
template<class Type>
void List<Type>::CreatHelp(Node<Type>*& hd){
	char ch = getchar();
	switch(ch) {
		case')':return;
		case'(':hd = new Node<Type>(LIST);
			Node<Type>* subnode;
			subnode = new Node<Type>(HEAD);
			subnode->ref = 1;
			hd->subNode = subnode;
			CreatHelp(subnode->next);

			ch = getchar();
			if (ch != ',')cin.putback(ch);
			CreatHelp(hd->next);
			break;
		default:
			cin.putback(ch);
			Type amdata;
			cin >> amdata;
			hd = new Node<Type>(ATOM);
			hd->atom = amdata;

			ch = getchar();
			if (ch != ',')cin.putback(ch);
			CreatHelp(hd->next);
			break;
	}
}
template<class Type>
void List<Type>::CopyHelp(const Node< Type> *sourcehd, Node<Type>* &desthd) {
	desthd = new Node<Type>(HEAD);
	Node<Type>* destp = desthd;
	desthd->ref = 1;
	for (Node<Type>* tmp = desthd->next; tmp != NULL; tmp = tmp->next) {
		destp = destp->next = new Node<Type>(tmp->tag);
		if (tmp->tag == LIST)
			CopyHelp(tmp->subNode, destp->subNode);
		else destp->atom = tmp->atom;
	}
}
template<class Type>
List<Type>::List() {
	head = new Node<Type>(HEAD, NULL);
	head->ref = 1;
}
template<class Type>
List<Type>::List(Node<Type>* hd) {
	head = new Node<Type>(HEAD);
	head->ref = 1;
}
template<class Type>
List<Type>::List(const List<Type>& copy) {
	CopyHelp(copy.head, head);
}
template<class Type>
List<Type>::~List() {
	ClearHelp(head);
}
template<class Type>
Node<Type>* List<Type>::First(void)const {
	return head->next;
}
template<class Type>
Node<Type>*List<Type>::Next(Node<Type> *elem)const {
	return elem->next;
}
template<class Type>
bool List<Type>::Empty(void)const {
	if (head->next == NULL)
		return 1;
	else return 0;
}
template<class Type>
void List<Type>::Push(Node<Type>*elem) {

}
template<class Type>
void List<Type>::Push(List<Type>& subList) {

}
template<class Type>
int List<Type>::depth(void) {
	return  DepthHelp(head);
}
template<class Type>
void List<Type>::Input(void) {
	head = new Node<Type>(HEAD);
	head->ref = 1;
	getchar();
	List<Type>::CreatHelp(head->next);
}
template<class Type>
void List<Type>::Output(void) {

}
#endif
测试:
#pragma warning(disable:4996)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<cstring>
#include<sstream>
#include<set>
#include<string>
#include<iterator>
#include<vector>
#include<map>
#include<algorithm>
#include"head.h"
using namespace std;



int main(void) {
	List<int> m;
	m.Input();
	m.Output();
	int c=m.depth();
	cout << c << endl;
	List<int> n = m;
	n.Output();
	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值