该方法是我自己想出的方法,书上写的是递归方法,我用的是栈的方法写出的,比较简单。输入广义表的格式为:(a,(b,c),(d,e,f))。具体有四个方面:
注意:p表示当前结点,hp表示头结点,pt表示尾结点,ht为含有hp结点与tp的缩写
(1)若遇到‘(’,在当前结点的hp上新建一个ht,并将新结点赋给p。
(2)若遇到‘ , ’,则在tp结点新建ht结点。
(3)若遇到‘)’,就出栈一个元素,无其他任何处理。
(4)除此之外,在hp结点建立一个赋值结点。
以上为思路。
广义表的定义为:
typedef struct GeneralNode
{
int tag;
union
{
char date;
struct { struct GeneralNode* hp, * tp; }ptr;
};
}*GList;
下面是栈的定义以及相关方法:
#define MAXSIZE 50
typedef struct node
{
char ch[MAXSIZE];
int top;
}StackNode, *PStackNode;
//栈的初始化
PStackNode Init_Stack()
{
PStackNode p;
p = (PStackNode)malloc(sizeof(StackNode));
if (p)
{
p->top = -1;
}
return p;
}
//判空
int Empty_Stack(PStackNode p)
{
if (p->top == -1)
{
printf("栈空\n");
return 1;
}
else
{
return 0;
}
}
//进栈
int Push_Stack(PStackNode p, char date)
{
if (p->top == MAXSIZE - 1)
{
printf("栈满无法入栈\n");
return 0;
}
else
{
p->ch[++p->top] = date;
return 1;
}
}
int Out_Stack(PStackNode p, char* date)
{
if (Empty_Stack(p))
{
printf("栈空,无法出栈\n");
return 0;
}
else
{
*date = p->ch[p->top];
p->top--;
return 1;
}
}
下面为广义表建立函数:
//建立广义表:传入参数 G为表头 str为字符数组 返回值为广义表的头结点
GList Creat_General(GList G,char str[])
{
if (strcmp(str, "()") == 0)
{
printf("该表为空栈\n");
G = NULL;
return NULL;
} //判断是否为空表
else
{
PStackNode S;
S = Init_Stack();//初始化符号栈
//将第一个括号进栈,并创建表头
Push_Stack(S, str[0]);
GList p, temp;//p表示当前结点,G为头结dian
p = (GList)malloc(sizeof(struct GeneralNode));
p->tag = 1;
p->ptr.hp = p->ptr.tp = NULL;
G = p;
int i = 1;
while (i < strlen(str) && !Empty_Stack(S))
{
if (str[i] == '(')//判断当前元素是否为'(',若是,则在当前结点的hp上创建新的ht结点(ht 为hp,tp结点的统称)
{
Push_Stack(S, str[i]);
temp = (GList)malloc(sizeof(struct GeneralNode));
temp->ptr.hp = temp->ptr.tp = NULL;
temp->tag = 1;
p->ptr.hp = temp;
p = temp;
temp = NULL;
}
else if (str[i] == ',')
{
temp = (GList)malloc(sizeof(struct GeneralNode));
temp->ptr.hp = temp->ptr.tp = NULL;
temp->tag = 1;
p->ptr.tp = temp;//判断当前元素是否为',',若是,则在当前结点的tp上创建新的ht结点(ht 为hp,tp结点的统称)
p = temp;
temp = NULL;
}
else if (str[i] == ')')
{
char x;
Out_Stack(S, &x);
}
else
{
temp = (GList)malloc(sizeof(struct GeneralNode));
temp->ptr.hp = temp->ptr.tp = NULL;
temp->tag = 0;//若当前结点为元素结点
temp->date = str[i];
p->ptr.hp = temp;
}
i++;
}
return G;
}
}
以下是总的代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 50
typedef struct node
{
char ch[MAXSIZE];
int top;
}StackNode, *PStackNode;
//栈的初始化
PStackNode Init_Stack()
{
PStackNode p;
p = (PStackNode)malloc(sizeof(StackNode));
if (p)
{
p->top = -1;
}
return p;
}
//判空
int Empty_Stack(PStackNode p)
{
if (p->top == -1)
{
printf("栈空\n");
return 1;
}
else
{
return 0;
}
}
//进栈
int Push_Stack(PStackNode p, char date)
{
if (p->top == MAXSIZE - 1)
{
printf("栈满无法入栈\n");
return 0;
}
else
{
p->ch[++p->top] = date;
return 1;
}
}
int Out_Stack(PStackNode p, char* date)
{
if (Empty_Stack(p))
{
printf("栈空,无法出栈\n");
return 0;
}
else
{
*date = p->ch[p->top];
p->top--;
return 1;
}
}
typedef struct GeneralNode
{
int tag;
union
{
char date;
struct { struct GeneralNode* hp, * tp; }ptr;
};
}*GList;
void Print_GeneralNode2(GList G);
void Print_GeneralNode(GList G,int tag);
//建立广义表:传入参数 G为表头 str为字符数组
GList Creat_General(GList G,char str[])
{
if (strcmp(str, "()") == 0)
{
printf("该表为空栈\n");
G = NULL;
return NULL;
} //判断是否为空表
else
{
PStackNode S;
S = Init_Stack();//初始化符号栈
//将第一个括号进栈,并创建表头
Push_Stack(S, str[0]);
GList p, temp;//p表示当前结点,G为头结dian
p = (GList)malloc(sizeof(struct GeneralNode));
p->tag = 1;
p->ptr.hp = p->ptr.tp = NULL;
G = p;
int i = 1;
while (i < strlen(str) && !Empty_Stack(S))
{
if (str[i] == '(')//判断当前元素是否为'(',若是,则在当前结点的hp上创建新的ht结点(ht 为hp,tp结点的统称)
{
Push_Stack(S, str[i]);
temp = (GList)malloc(sizeof(struct GeneralNode));
temp->ptr.hp = temp->ptr.tp = NULL;
temp->tag = 1;
p->ptr.hp = temp;
p = temp;
temp = NULL;
}
else if (str[i] == ',')
{
temp = (GList)malloc(sizeof(struct GeneralNode));
temp->ptr.hp = temp->ptr.tp = NULL;
temp->tag = 1;
p->ptr.tp = temp;//判断当前元素是否为',',若是,则在当前结点的tp上创建新的ht结点(ht 为hp,tp结点的统称)
p = temp;
temp = NULL;
}
else if (str[i] == ')')
{
char x;
Out_Stack(S, &x);
}
else
{
temp = (GList)malloc(sizeof(struct GeneralNode));
temp->ptr.hp = temp->ptr.tp = NULL;
temp->tag = 0;//若当前结点为元素结点
temp->date = str[i];
p->ptr.hp = temp;
}
i++;
}
return G;
}
}
int main()
{
char ch[] = "(a,b,(c,d))\0";
GList G = NULL;
GList temp;
temp = Creat_General(G,ch);
printf("该广义表为:");
Print_GeneralNode(temp, 1);
return 0;
}
若有其他更好的方法,欢迎与我一起交流,以上只是个人的一点见解,无任何参考。