这几天学了个广义表,之所以用了几天是因为唐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;
}