?? 新建 文本文檔.txt
字號:
5.34⑤ 試編寫遞歸算法,逆轉(zhuǎn)廣義表中的數(shù)據(jù)元素。
例如:將廣義表
(a,((b,c),()),(((d),e),f))
逆轉(zhuǎn)為:
((f,(e,(d))),((),(c,b)),a)。
要求實(shí)現(xiàn)以下函數(shù):
void Reverse(GList &L);
/* 遞歸逆轉(zhuǎn)廣義表L */
廣義表類型GList的定義:
typedef enum {ATOM,LIST} ElemTag;
typedef struct GLNode{
ElemTag tag;
union {
char atom;
struct {
GLNode *hp, *tp;
} ptr;
}un;
} *GList;
void Reverse(GList &L)
/* 遞歸逆轉(zhuǎn)廣義表L */
{int i;
GList ptr[20];
GList p;
if(L->tag||L->un.ptr.tp)
{
for(i=0,p=L;p;p=p->un.ptr.tp,i++)
{
if( p->un.ptr.hp )Reverse( p->un.ptr.hp );
ptr[i] = p->un.ptr.hp;
}
for(p=L;p;p=p->un.ptr.tp)p->un.ptr.hp = ptr[--i];
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -