?? main.cpp
字號:
#include <iostream>
using namespace std;
#define MAX 50
struct NODE
{
bool tag;
union
{
struct NODE *dlink;
char data;
}element;
struct NODE *link;
};
NODE *GreatL(char **s) //按用戶輸入創(chuàng)建一個廣義表
{
NODE *q;
char ch;
ch=**s;
(*s)++; //與其后的 ch=**s;(*s)++ 功能一樣均用于依次訪問每個字符
if (ch!=' ') //表不為空
{
q=new NODE;
if (ch=='(') //遇到左括號,說明有子表
{
q->tag=true; //作標識
q->element.dlink=GreatL(s);//建子表
}
else //遇到字母,存入作為元素
{
q->tag=false;
q->element.data=ch;
}
}
else //表為空,置空q
{
q=NULL;
cout<<"表為空";
}
ch=**s;
(*s)++;
if (q!=NULL) //表不為空
if (ch==',') //表中有別的元素
q->link =GreatL(s); //遞歸存入
else
q->link=NULL; //否則將下一個地址置空
return q;
}
void OutPutL(NODE *ptr) //輸出廣義表
{
if (ptr!=NULL)
{
if (ptr->tag==true) //遇到子表
{
cout<<"("; //輸出左括號
if (ptr->element.dlink !=NULL) //子表元素不為空
OutPutL(ptr->element.dlink ); //遞歸輸出子表元素
}
else
cout<<ptr->element.data; //遇到字母,直接輸出
if (ptr->tag==true) //子表中的字母均輸出后,輸出右括號
cout<<")";
if (ptr->link!=NULL) //上一級鏈中的下一個結點不為空輸出逗號
{
cout<<",";
OutPutL(ptr->link); //遞歸實現(xiàn)輸出結點中的元素
}
}
}
NODE *copyL(NODE *A) //復制該廣義表,將A復制到B中去
{
NODE *B;
if (A==NULL) //表為空
{cout<<"表為空";
return NULL;
}
B=new NODE;
B->tag=A->tag;
if (A->tag==true) //標識為true,說明有子表
B->element.dlink =copyL(A->element.dlink ); //復制子表
else
B->element.data =A->element.data; //標識為false,復制元素
B->link=copyL(A->link);
return B;
}
int main()
{
NODE *L,*cpy;
char s[MAX],*p;
cout<<"輸入一個廣義表,如((a,b),c)"<<endl;
cin>>s;
p=s;
L=GreatL(&p);
cpy=copyL(L);
cout<<"復制以后的結果為:"<<endl;
OutPutL(cpy);
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -