北京科技大学数据结构试验报告附录含代码Word下载.docx

上传人:b****6 文档编号:21590489 上传时间:2023-01-31 格式:DOCX 页数:21 大小:199.68KB
下载 相关 举报
北京科技大学数据结构试验报告附录含代码Word下载.docx_第1页
第1页 / 共21页
北京科技大学数据结构试验报告附录含代码Word下载.docx_第2页
第2页 / 共21页
北京科技大学数据结构试验报告附录含代码Word下载.docx_第3页
第3页 / 共21页
北京科技大学数据结构试验报告附录含代码Word下载.docx_第4页
第4页 / 共21页
北京科技大学数据结构试验报告附录含代码Word下载.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

北京科技大学数据结构试验报告附录含代码Word下载.docx

《北京科技大学数据结构试验报告附录含代码Word下载.docx》由会员分享,可在线阅读,更多相关《北京科技大学数据结构试验报告附录含代码Word下载.docx(21页珍藏版)》请在冰豆网上搜索。

北京科技大学数据结构试验报告附录含代码Word下载.docx

//建立头节点

r=H;

p=(List)malloc(sizeof(list));

//申请新节点

while(scanf("

%d"

p)&

&

p->

data!

=0)//输入数据,直到为零(结束标志)

{

r->

next=p;

//新节点链入表尾

r=p;

p=(List)malloc(sizeof(list));

}

r->

next=NULL;

//将尾节点的指针域置空

returnH;

//返回已创建的头节点

}

ListAdjmax(ListH)//比较相邻两数之和

{//返回相邻两数之和最大的第一个数指针

Listp,r,q;

intsum=0;

p=H->

next;

if(H->

next==NULL)//判断是否为空

printf("

EmptyList!

"

);

q=(List)malloc(sizeof(list));

q->

data=0;

}

while(p!

=NULL)//比较相邻两数之和

r=p->

next;

if(p&

r)

if(r->

data+p->

data>

sum)

{

q=p;

sum=r->

data+p->

data;

}//不断赋给sum新的最大值

else;

p=p->

returnq;

intmain()

charch;

printf("

///请输入整形数据,以空格隔开,0结束。

///\n"

Ready?

\nY/N(enter'

y'

or'

Y'

tocontinue)\n"

%c"

&

ch)&

(ch=='

||ch=='

))

ListH,pmax;

H=Creatlist();

pmax=Adjmax(H);

相邻两数之和最大的第一个数为:

%d\nContinue?

Y/N"

pmax->

data);

free(H);

scanf("

ch);

return0;

实验二:

#include<

stdio.h>

malloc.h>

conio.h>

typedefstructnode//栈节点类型

chardata;

//存储一个栈元素

//后继指针

}snode,*slink;

intEmptystack(slinkS)//检测栈空

if(S==NULL)return

(1);

elsereturn(0);

charPop(slink*top)//出栈

chare;

slinkp;

if(Emptystack(*top))return(-1);

//栈空返回

else

e=(*top)->

data;

//取栈顶元素

p=*top;

*top=(*top)->

//重置栈顶指针

free(p);

return(e);

voidPush(slink*top,chare)//进栈

p=(slink)malloc(sizeof(snode));

//生成进栈p节点

p->

data=e;

//存入元素e

next=*top;

//p节点作为新的栈顶链入

*top=p;

voidClearstack(slink*top)//置空栈

while(*top!

=NULL)

p=(*top)->

Pop(top);

//依次弹出节点直到栈空

*top=p;

*top=NULL;

charGetstop(slinkS)//取栈顶

if(S!

=NULL)return(S->

data);

return(0);

//符号优先级比较

intPrecede(charx,chary)//比较x是否"

大于"

y

switch(x)

{

case'

('

:

x=0;

break;

+'

-'

x=1;

*'

/'

x=2;

default:

x=-1;

switch(y)

y=1;

y=2;

y=3;

y=100;

if(x>

=y)return

(1);

//中后序转换

voidmid_post(charpost[],charmid[])//中缀表达式mid到后缀表达式post的转换的算法

inti=0,j=0;

charx;

slinkS=NULL;

//置空栈

Push(&

S,'

#'

//结束符入栈

do

x=mid[i++];

//扫描当前表达式分量x

switch(x)

{case'

{while(!

Emptystack(S))

post[j++]=Pop(&

S);

}break;

case'

)'

{while(Getstop(S)!

='

//反复出栈直至遇到'

Pop(&

//退掉'

{while(Precede(Getstop(S),x))//栈顶运算符(Q1)与x比较

//Q1>

=x时,输出栈顶符并退栈

Push(&

S,x);

//Q1<

x时x进栈

default:

post[j++]=x;

//操作数直接输出

}

}while(x!

post[j]='

\0'

;

//后缀表达式求值

intpostcount(charpost[])//后缀表达式post求值的算法

inti=0;

floatz,a,b;

//置栈空

while(post[i]!

{x=post[i];

//扫描每一个字符送x

{case'

b=Pop(&

a=Pop(&

z=a+b;

Push(&

S,z);

case'

z=a-b;

z=a*b;

z=a/b;

//执行相应运算结果进栈

default:

x=post[i]-'

0'

//操作数直接进栈

}

i++;

if(!

Emptystack(S))return(Getstop(S));

//返回结果

voidmain()

charpost[255],mid[255]="

请输入要处理的中缀表达式:

\n"

scanf("

%s"

mid);

相应的后缀表达式为:

mid_post(post,mid);

%s\n"

post);

表达式的值为:

%d\n"

postcount(post));

getch();

 

实验三:

#definemax1000

charch[max];

intfront,rear;

}squeue,*sq;

voidClearqueue(sqQ)

Q->

front=Q->

rear;

intEmptyqueue(sqQ)

if(Q->

rear==Q->

front)

return1;

else

return0;

voidEnqueue(sqQ,charch)

rear>

=max)

FULLQUEUE!

ch[Q->

rear]=ch;

rear++;

}}

voidDequeue(sqQ)

if(Emptyqueue(Q))

EmptyQUEUE!

出队:

%c\n"

Q->

ch[Q->

front]);

front++;

voidPrintqueue(sqQ)

;

队列中全部元素:

while(Q->

front!

=Q->

rear-1)

{

printf("

Q->

front++;

sqQueue;

charf;

*******************************************\n"

请输入字符X\nX≠'

@'

并且X≠'

字符入队;

X='

字符出队;

打印队列中各元素。

Queue=(sq)malloc(sizeof(squeue));

Queue->

front=Queue->

rear=0;

f)&

f!

if(f!

Enqueue(Queue,f);

Dequeue(Queue);

if(f=='

Printqueue(Queue);

else;

实验四:

#defineN8

#defineMAX100

#defineM2*N-1

typedefstruct

charletter;

intw;

intparent,lchild,rchild;

}Huffm;

charbits[N+1];

intstart;

}ctype;

voidinputHT(HuffmHT[M+1])

inti;

for(i=1;

i<

=M;

i++)

HT[i].w=0;

HT[i].parent=0;

HT[i].lchild=0;

HT[i].rchild=0;

请输入电文字符集:

for(i=1;

=N;

HT[i].letter);

请输入字符出现的频率:

HT[i].w);

voidCreatHT(HuffmHT[M+1])

inti,j,min1,min2;

inttag1,tag2;

//权值最小两个点标号;

for(i=N+1;

tag1=tag2=0;

min1=min2=MAX;

for(j=1;

j<

=i-1;

j++)

if(HT[j].parent==0)

if(HT[j].w<

min1)

{min2=min1;

min1=HT[j].w;

tag2=tag1;

tag1=j;

}

else

if(HT[j].w<

min2)

{

min2=HT[j].w;

tag2=j;

}

HT[tag1].parent=HT[tag2].parent=i;

HT[i].lchild=tag1;

HT[i].rchild=tag2;

HT[i].w=HT[tag1].w+HT[tag2].w;

voidHuffmcode(HuffmHT[M+1])//Huffm编码函数

inti,j,p,tag;

ctypemcode,code[N+1];

code[i].ch=HT[i].letter;

mcode.ch=code[i].ch;

mcode.start=N+1;

tag=i;

p=HT[i].parent;

for(j=0;

mcode.bits[j]='

'

while(p!

=0)

mcode.start--;

if(HT[p].lchild==tag)

mcode.bits[mcode.start]='

else

mcode.bits[mcode.start]='

1'

tag=p;

p=HT[p].parent;

code[i]=mcode;

%c'

的Huffm编码为:

code[i].ch);

for(j=0;

code[i].bits[j]);

******************************************************\n"

电文字符集含8个字符,连续输入,不同频率之间以空格隔开\n"

ch='

while(ch=='

HuffmHT[M+1];

inputHT(HT);

CreatHT(HT);

Huffmcode(HT);

Continue?

fflush(stdin);

实验五:

string.h"

typedefstructbsnode

charword[20];

structbsnode*lchild,*rchild;

}BStree,*BST;

BSTBSTinsert(BSTT,BSTs)

BSTf,p;

if(T==NULL)

returns;

p=T;

f=NULL;

while(p)

f=p;

if(strcmp(s->

word,p->

word)==0)

free(s);

returnT;

if(strcmp(s->

word)<

0)

p=p->

lchild;

else

rchild;

if(strcmp(s->

word,f->

0)

f->

lchild=s;

rchild=s;

returnT;

BSTCreatBst()

BSTT,s;

charkeyword[20];

T=NULL;

gets(keyword);

while(keyword[0]!

s=(BST)malloc(sizeof(BStree));

strcpy(s->

word,keyword);

s->

lchild=s->

rchild=NULL;

T=BSTinsert(T,s);

gets(keyword);

voidInorder(BSTT)

if(T)

Inorder(T->

lchild);

%s"

T->

word);

rchild);

{charch;

BSTT;

************************************************************\n"

请输入英文句子,每输入一个单词以回车结束,句子结束以'

结束。

T=CreatBst();

按LDR遍历此二叉排序树(字典顺序):

Inorder(T);

free(T);

\nCo

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1