/*#include
void main() { using namespace std; char a; cin.get(a);//get这个函数的使用方法 cout< cout<<"2.14"< cout< //但是没有6位的精度,加上showpoint就行了
}*/ #ifndef GLIST_H #define GLIST_H #include<iostream.h>
class Node { public: int tag; union { char data; Node* sublist; }val; Node* link; void Display(); int GLLength(); int GLDepth(); Node* GLCopy(); Node* GetHead(); Node* GetTail(); Node* CreatList(char*& ch); };
class GList { public: Node* head; };
Node* Node::CreatList(char*& ch) { Node* head=this; char a; a=*ch; ch++; if(a!='\0') { head=new Node; if(a=='{') { head->val.sublist=CreatList(ch); } else if(a==')') head=NULL; else {head->tag=0; head->val.data=a;} } else head=NULL; a=*ch; ch++; if(head!=NULL) if(a==',') head->link=CreatList(ch); else head->link=NULL; return head; } void Node::Display() { Node* head=this; if(head!=NULL) { if(head->tag==1) { cout<<'('; if(head->val.sublist==NULL) cout<<""; else (head->val.sublist)->Display(); } else cout<<head->val.data; if(head->tag==1) cout<<')'; if(head->link!=NULL) { cout<<','; head->link->Display(); } } } int Node::GLLength() { int n=0; Node* head=this; head=head->val.sublist; while(head!=NULL) { n++; head=head->link; } return n; } int Node::GLDepth() { Node* head=this; int max=0,dep; if(head->tag==0) { return 0; } head=head->val.sublist; if(head==NULL) return 1; while(head!=NULL) { if(head->tag==1) { dep=head->GLDepth(); if(dep>max) max=dep; } head=head->link; } return max+1; } Node* Node::GLCopy() { Node* q; Node* head=this; if(head==NULL) return NULL; q=new Node; q->tag=head->tag; if(head->tag==1) q->val.sublist=head->val.sublist->GLCopy(); else q->val.data=head->val.data; q->link=head->link->GLCopy(); return q; } Node* Node::GetHead() { Node* head=this; Node* p=head->val.sublist; Node* q,*t; if(p==NULL) { cout<<"空表不能求表头"<<endl; return NULL; } else if(head->tag==0) { cout<<"原子不能求表头"<<endl; return NULL; } if(p->tag==0) { q=new Node; q->tag=0; q->val.data=p->val.data; q->link=NULL; } else { t=new Node; t->tag=1; t->val.sublist=p->val.sublist; t->link=NULL; q=t->GLCopy(); delete t; } return q; } Node* Node::GetTail() { Node* head=this; Node* p=head->val.sublist; Node* q,*t; if(head==NULL) { cout<<"空表不能求表尾"<<endl; return NULL; } else if(head->tag==0) { cout<<"原子不能求表尾"<<endl; return NULL; } p=p->link; t=new Node; t->tag=1; t->link=NULL; t->val.sublist=p; q=t->GLCopy(); delete t; return q; } Node* GLContat(Node* gh,Node*& gt) { gh->link=gt->val.sublist; gt->val.sublist=gh; return gt; } #endif #include<iostream.h>
class Node { public: int tag; union { char data; Node* sublist; }val; Node* link; public: Node() { tag=1;//这个值一定要写进去,不是空时,再将其覆盖
} void Display(); int GLLength(); int GLDepth(); Node* GLCopy(); Node* GetHead(); Node* GetTail(); Node* CreatList(char*& ch); };
class GList { public: Node* head; }; Node* Node::CreatList(char*& ch) { Node* head=this;//这个this指向空,要想一个办法使this转变成左值
//head=head->val.sublist;
char a; a=*ch; ch++; if(a!='\0') { head=new Node; if(a=='{') { head->val.sublist=head->val.sublist->CreatList(ch); } else if(a=='}') head=NULL; else {head->tag=0; head->val.data=a;} } else head=NULL; a=*ch; ch++; if(head!=NULL) if(a==',') head->link=head->link->CreatList(ch); else head->link=NULL; return head; } void Node::Display() { Node* head=this; if(head!=NULL) { if(head->tag==1) { cout<<'{'; if(head->val.sublist==NULL) cout<<""; else (head->val.sublist)->Display(); } else cout<<head->val.data; if(head->tag==1) cout<<'}'; if(head->link!=NULL) { cout<<','; head->link->Display(); } } } int Node::GLLength() { int n=0; Node* head=this; head=head->val.sublist; while(head!=NULL) { n++; head=head->link; } return n; } int Node::GLDepth() { Node* head=this; int max=0,dep; if(head->tag==0) { return 0; } head=head->val.sublist; if(head==NULL) return 1; while(head!=NULL) { if(head->tag==1) { dep=head->GLDepth(); if(dep>max) max=dep; } head=head->link; } return max+1; } Node* Node::GLCopy() { Node* q; Node* head=this; if(head==NULL) return NULL; q=new Node; q->tag=head->tag; if(head->tag==1) q->val.sublist=head->val.sublist->GLCopy(); else q->val.data=head->val.data; q->link=head->link->GLCopy(); return q; } Node* Node::GetHead() { Node* head=this; Node* p=head->val.sublist; Node* q,*t; if(p==NULL) { cout<<"空表不能求表头"<<endl; return NULL; } else if(head->tag==0) { cout<<"原子不能求表头"<<endl; return NULL; } if(p->tag==0) { q=new Node; q->tag=0; q->val.data=p->val.data; q->link=NULL; } else { t=new Node; t->tag=1; t->val.sublist=p->val.sublist; t->link=NULL; q=t->GLCopy(); delete t; } return q; } Node* Node::GetTail() { Node* head=this; //head=head->val.sublist;
Node* p=head->val.sublist; Node* q,*t; if(head==NULL) { cout<<"空表不能求表尾"<<endl; return NULL; } else if(head->tag==0) { cout<<"原子不能求表尾"<<endl; return NULL; } p=p->link; t=new Node; t->tag=1; t->link=NULL; t->val.sublist=p; q=t->GLCopy(); delete t; return q; } Node* GLContat(Node* gh,Node*& gt) { gh->link=gt->val.sublist; gt->val.sublist=gh; return gt; } void main() { char *d="{a,b,c,{l,i,u,z,h,a,n,{l,i,u,z,h,a,n,m,e,i}}}"; GList a,b,c; a.head=(a.head)->CreatList(d);//想一个办法使这个值不用去复制
cout<<a.head->GLDepth()<<endl; cout<<a.head->GLLength()<<endl; (a.head)->Display(); cout<<endl; b.head=(a.head)->GetTail(); (b.head)->Display(); cout<<endl; b.head=(a.head)->GetHead(); (b.head)->Display(); cout<<endl; c.head=a.head->GLCopy(); c.head->Display(); }
|