廣義表的C語言實現

廣義表的C語言實現

 

最近弄了一下這個廣義表,將它的部分操作寫了出來,以供大家共同學習,研究。

開發工具:VC6.0

 


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define AtomType char
#define MAXSIZE  1024
#define ElemTag int
#define OK       1
#define ERROR    0


typedef struct GLNode
{
 ElemTag tag;
 union
 {
  AtomType  atom;
  struct GLNode *hp;
 }atom_hp;
    struct GLNode *tp;
}GLNode,*GList;


//功能:分離出廣義表中表頭部分
//返回:分離後剩下的字符串,不包括逗號
void Disastr(char *s,char *hstr)
{
 int i,j,k,r;
 char rstr[MAXSIZE];
 
 i=j=k=0;
 while(s[i] && (s[i] != ',' || k))
 {
  if (s[i] == '(')  {  k++ ; }        // k 作爲括號計數器        
  else if (s[i] == ')') {k--;}
  if (s[i] != ',' || s[i] == ',' && k)
  {
   hstr[j] = s[i];
   i++;
   j++;
  }
 }

 hstr[j] = '/0';
 if (s[i] == ',') {i++;}

 r=0;
 while(s[i])                 // 處理剩餘的表尾部分
 {
  rstr[r] = s[i];
  r++;
  i++;
 }
 rstr[r] = '/0';
 strcpy(s,rstr);
}

//功能:根據輸入的字符串,建立廣義表
//返回:成功則返回建立的廣義表的表頭,否則,返回NULL
GLNode * GLCreate(char *s)
{
 GLNode *p,*q,*r,*head;
    char substr[MAXSIZE],hstr[MAXSIZE];//rstr[MAXSIZE];
    int len;

 len = strlen(s);
    if ( !strcmp(s,"()") || !len)  { head = NULL;}     // (1) 空表情況
 else if (len == 1)                                      // (2) 原子情況
 {
  head = (GLNode *)malloc(sizeof(GLNode));       // 建立一個新結點
  if (!head)  return NULL;     
  head->tag = 0;                                 // 構造原子結點
  head->atom_hp.atom = *s;
  head->tp = NULL;
 }
    else                                                // (3) 子表情況
 {
  head = (GLNode *)malloc(sizeof(GLNode));
  if (!head) return NULL;
  head->tag = 1;
  p = head;
  s++;
  strncpy(substr,s,len-2);                         // 剝去外層的()
  substr[len-2] = '/0';
  do 
  {
   Disastr(substr,hstr);                        // 分離出表頭
   r = GLCreate(hstr);
   p->atom_hp.hp = r;                           // 尾插法建表
   q=p;
   len = strlen(substr);
   if (len > 0)
   {
    p = (GLNode*)malloc(sizeof(GLNode));
    if (!p) return NULL;
    p->tag = 1;
    q->tp=p;
   }
  } while (len > 0);
  q->tp=NULL;
 }
 return head;
}

void DisplayList(GList head)
{
 GLNode *p,*q;
    
 if (!head)  return;
 if (head->tag==0)
 {
  printf("%c",head->atom_hp.atom);
  return;
 }
 printf("(");
 if (head)
 {
  do 
  {
   p = head->atom_hp.hp;
   q = head->tp;
   while (q && p && p->tag == 0)                //  同一層的原子結點
   {
    printf("%c,",p->atom_hp.atom);
    p = q->atom_hp.hp;
    q = q->tp;
   }
   if (p && !p->tag)                           // 最後一個原子結點
   {
    printf("%c",p->atom_hp.atom);
    break;
   }
   else                                        // 子表情況
   {
    if (!p) printf("()");
    else DisplayList(p);
    if (q)  printf(",");
    head = q;
   }
  } while (head);
  printf(")");
 }
}

//功能:取出廣義表的表頭部分
//返回:成功則返回廣義表的表頭,否則,返回空或退出
GList GetHead(GList L)
{
 if (!L)  return (NULL);                    // 空表無表頭
 if (L->tag == 0)  exit(0);                 // 原子結點不是表
 else return (L->atom_hp.hp);
}

//功能:取出廣義表的表尾部分
//返回:成功返回廣義表的表尾部分,否則,返回空或者退出
GList GetTail(GList L)
{
 if (!L) return (NULL);
 if (L->tag == 0) exit(0);
 else return (L->tp);
}

//功能:求出廣義表的長度
//返回值:廣義表的長度
int Length(GList L)
{
 int k=0;
 GLNode *s;

 if (!L) return 0;                    // 空表的長度爲零
 if (L->tag == 0) exit(0);            // 原子不是表
 s=L;
 while(s)                             // 統計表的最上層的長度
 {
  k++;
  s=s->tp;
 }

 return k;
}


//功能:求得廣義表的深度
//輸入:需求深度的廣義表的指針
int Depth(GList L)
{
 int d,max;
 GLNode *s;

 if (!L)  return (1);            // 空表的深度爲 1
 if (L->tag==0)  return 0;       // 原子的深度爲 0
 s=L;
 max=0;
 while (s)                        // 遞歸求每個子表深度的最大值
 {
  d = Depth(s->atom_hp.hp);
  if (d > max) max = d;
  s = s->tp;
 }
 return (max+1);                  // 表的深度爲子表深度加一
}


//功能:統計原子結點的個數
//輸入:需統計的廣義表指針
int CountAtom(GList L)
{
 int n1,n2;

 if (!L) return 0;                   // 空表無原子結點
 if (L->tag==0) return 1;            // 原子結點
 n1 = CountAtom(L->atom_hp.hp);      
 n2 = CountAtom(L->tp);
 return (n1+n2);
}

//功能:完成廣義表的複製,將res複製到dest中
//返回:成功返回1,否則,返回0
bool CopyList(GList *dest,GList res)
{
 if (!res) {*dest = NULL;return (OK);}

 *dest = (GLNode *)malloc(sizeof(GLNode));
 if (!*dest)  return (ERROR);

 (*dest)->tag = res->tag;
 if (res->tag==0)  (*dest)->atom_hp.atom = res->atom_hp.atom;
    else
    {
  CopyList(&(*dest)->atom_hp.hp,res->atom_hp.hp);
  CopyList(&(*dest)->tp,res->tp);
 }
 return (OK);
}

//功能:合併廣義表,如果p爲空,則申請空間,將q複製到p中
//例如:((a,b),c) 和(a,b)合併之後爲:((a,b),c,a,b)
//算法描述:先找到第一個廣義表的最後一個結點,將其鏈到第二個廣義表的首元素即可
void Merge(GList *p,GLNode *q)
{
 GLNode *r;

 if (!q) return;       //  如果複製的是個空表,返回
 if (!p)               // p爲空,申請空間
 {
  *p = (GLNode*)malloc(sizeof(GLNode));
  if (!(*p)) return ;
  (*p)->tag = 1;
 }
 else
 {
  if ((*p)->tag)         
  {
   r=*p;
   while(r->tp) r=r->tp;             // 找到最後一個子表的表尾指針
   if (q->tag) r->tp = q;        // 修改表尾指針
  }
 }
}

//功能:類似二叉樹的先序遍歷遍歷廣義表L
//eg:例如(a,(b,(c),d))結果爲:a,b,c,d
//算法描述:
//L若爲原子結點,顯示該數據,遞歸調用遍歷後續元素,也即:write(L->atom_hp.atom);PreOrder(L->tp);
//L是子表結點,遞歸調用遍歷該子表,遍歷後續元素,也即:PreOrder(L->atom_hp.tp);PreOrder(L->tp);
void PreOrder(GList L)
{
 if (L)
 {
  if (L->tag==0) printf("%c ",L->atom_hp.atom);   // 打印原子結點
  else  PreOrder(L->atom_hp.hp);                  // 往下遍歷,類似二叉樹中的左子樹

  if (L->tp) PreOrder(L->tp);                     // 往右遍歷,類似二叉樹中的右子樹
 }
}

// 判斷兩個廣義表是否相等,相等,返回1,否則,返回0
// 相等的定義:兩個廣義表具有相同的存儲結構,對應的原子結點的數據域也相等
//算法描述:
// 形式:條件
//Equal(p,q) = Equal(p->tp,q->tp) ; p->tag = 0 && q->tag = 0 && p->atom_hp.atom = q->atom_hp.atom
//Equal(p,q) = Equal(p->atom_hp.hp,q->atom_hp.hp) && Equal(p->tp,q->tp) ; p->tag = 1 && q->tag = 1
//Equal(p,q) = false     ; p->tag = 0 && q->tag = 0 p->atom_hp.atom != q->atom_hp.atom 或者 p->tag *p->tag + q->tag*q->tag =1
//Equal(p,q) = false      ; p q 其中之一爲NULL
bool Equal(GList p,GList q)
{
 bool flags = true;

 if (!p && q) flags = false;
 if (p && !q) flags = false;
    if (p && q)
 {
  if (p->tag == 0 && q->tag == 0 )
  {
   if (p->atom_hp.atom != q->atom_hp.atom) 
    flags = false;
  }
  else if (p->tag == 1 && q->tag == 1)
  {
   flags = Equal(p->atom_hp.hp,q->atom_hp.hp);
  }
  else flags = false;
  if (flags) flags = Equal(p->tp,q->tp);
 }
 return flags;
}

int main()
{
 char s[MAXSIZE],a[MAXSIZE];
 GList head;
    GList L;

 printf("please input a string:");
 scanf("%s",s);
 head = GLCreate(s);
 DisplayList(head);
 printf("/n");

 printf("The Head is:");
    DisplayList(GetHead(head));
    printf("/n");

 printf("The Tail is: ");
 DisplayList(GetTail(head));
 printf("/n");

 printf("The Length is %d/n",Length(head));
 printf("The Depth is %d/n",Depth(head));
 printf("The Atom number is %d/n",CountAtom(head));

 printf("Copy the List:/n");
    CopyList(&L,head);
 DisplayList(L);
 printf("/n");

 printf("Merge the List/n");
 Merge(&L,head);
 DisplayList(L);
 printf("/n");

 printf("PreOrder:");
 PreOrder(head);
 printf("/n");

 printf("input a string:");
 scanf("%s",a);
 L = GLCreate(a);
 DisplayList(L);
 printf(" Eqaul ");
 DisplayList(head);
 printf(":");
 if (Equal(L,head)) printf("yes!/n");
 else printf("no!/n");
 return 0;
}

發佈了26 篇原創文章 · 獲贊 11 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章