3.功能要求
要求完成以下功能:
⑴ 插入:将某位本科生或研究生插入到广义表的相应位置; ⑵ 删除:将某本科生或研究生从广义表中删除; ⑶ 查询:查询导师、本科生(研究生)的情况; ⑷ 统计:某导师带了多少个研究生和本科生; ⑸ 输出:将某导师所带学生情况输出。
4.源代码
#include #include #include typedef #include void { char char GLNode int
h=CreatGList(str1); printf(\
DecomposeStr(str2, printf(\ printf(\
depth=GListDepth(h); printf(\
length=GListLength(h); printf(\ printf(\
原
深子
元
度素
个
数
length=%d\
number=%d\
length); number);
number=GListAtomNum(h);
深
度
depth=%d\
表
头
表
尾
广
义
表
str1=%s\
hstr); =%s\ =%s\
depth,
*h,
number,
char
main()
str1[]=\ str2[]=\
hstr[100];
*p; length;
char
p=GListSearch(h,'d'); if
printf(\ else printf(\ 头typedef { int union {
DataType
atom;
//
//原//
子头尾
元
素指指
域 针 针
struct {
struct
GListNode
*tail;
*head;
struct }val; }GLNode; void { int char for(i ch if(ch if(ch } if(i hstr[j] if(str[i] str[0]
=
'(';
=
<= '\\0';
n-1 ==
&& str[i] == ',')
//广义表表尾部分非空时
//添加结束符
//
添i++; '('
{ for(j = 0; j < i-1; j++) hstr[j] = str[j+1];//取表头字符串
',')
== ==
i,
ch; =
j, ch 0;
tag, =
i =
'(') ')') n str[0];
<=
= tag
n-1;
=
strlen(str);
0; i++) str[i]; tag++; tag--;
DecomposeStr(char
str[],
char
hstr[])
GListNode
subGL tag;
文
struct
件
:
GListNode
广
义
表
中
不
存
在
要
查
找
的
数
据
元
素
\\n\
DestroyGList(h);
数
据
元
素
%c
在
广
义
表
中
\
(p!=NULL) p->val.atom);
}subList; //子表域
{ if(str[i] == ',' && tag == 1 ) break;//搜索最外层的第一个逗号
for(j = 1; i <= n-2; i++, j++) str[j] = str[i]; //取表尾字符串 str[j] = ')'; //添')' str[++j] = '\\0'; //添加结束符 } else
{
str++; n-2);//
不
复
//
路制
过右
最
左边
边的
的
'(' ')'
strncpy(hstr,str,
//
广
义
表
表
尾
部
分
空
时
hstr[n-2] str--; } }
GLNode* {
strcpy(str,
=
\
'\\0';
//
//表
//恢尾
添复
字部
加符
串
结指分
束针为
位
符 置 空
CreatGList(char
len \if(len
==
h == 1)
=
= =
//
=
建
立
(GLNode
= =
\
!=
0)
= =
NULL;
//
赋
值
//
表
尾子
0)
//
建
(GLNode
=
立
h 原
= 子
结
GLNode
str[]) *h;
hstr[200]; strlen(str);
NULL;
点 0; str[0];
表
char int
if(strcmp(str, else { h->tag h->val.atom } else
{ h h->tag
DecomposeStr(str, h->val.subList.head
if(strcmp(str, h->val.subList.tail else
h->val.subList.tail } return } int {
==
NULL) 归=
dep >
0; =
return 求pre
!= max) max
GListLength(GLNode
GLNode if(h //
递
1;//
递广
= NULL;
pre max
+ =
//
GListDepth(pre->val.subList.head);
求=
表
归
出义
口
,
空表
表
深深
GListDepth(GLNode
int
max,
空非
*)malloc(sizeof(GLNode));
*)malloc(sizeof(GLNode));
1; hstr);
CreatGList(hstr);
空
时
CreatGList(str);
指
针 h; *h) dep; *pre;
度
为
1 0 度 h;
pre->val.subList.tail)
头
深
度 dep; 1; *h)
if(h->tag pre for(max { if(dep } return } int {
== 0) return 0; //递归出口,原子元素深度为
int GLNode for(p return } int {
if(h else {
==
=
h;
number = 0; *p;
p != NULL; p = p->val.subList.tail)
number;
number++;
GListAtomNum(GLNode
NULL)
return
*h) 0;
if(h->tag == 0) return 1; else return GListAtomNum(h->val.subList.head)
GListAtomNum(h->val.subList.tail); } } GLNode { GLNode
if(h==NULL)
return
NULL;//
查return
if(h->tag==0&&h->val.atom==x) { return }
if(h->tag==1&&h->val.subList.tail!=NULL) { return }
return NULL; //回溯至上一层 } void {
if(h==NULL)
if(h->tag==1&&h->val.subList.head!=NULL) DestroyGList(h->val.subList.head); DestroyGList(h->val.subList.tail); free(h); }
////
撤撤
销销
headtail
所所
指指
子子
表 表
if(h->tag==1&&h->val.subList.tail!=NULL)
return
;
DestroyGList(GLNode
*h)
p=GListSearch(h->val.subList.tail,x);
//
在
尾
链
中
查
找 p;
if
(p!=NULL)
找h;//
失查//
败找在
成头递功链
归递
归中
出出查
*p; 口 口 找 p;
*GListSearch(GLNode
*h,
DataType
x) +
if(h->tag==1&&h->val.subList.head!=NULL)
p=GListSearch(h->val.subList.head,x);
if
(p!=NULL)