数据结构实验报告 魔方阵(2)

2019-09-01 09:43

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

DataType; \

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)


数据结构实验报告 魔方阵(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:机关干部作风整顿建设学习内容目录

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: