栈和队列的运用Word格式文档下载.docx

上传人:b****4 文档编号:16743988 上传时间:2022-11-25 格式:DOCX 页数:15 大小:49.15KB
下载 相关 举报
栈和队列的运用Word格式文档下载.docx_第1页
第1页 / 共15页
栈和队列的运用Word格式文档下载.docx_第2页
第2页 / 共15页
栈和队列的运用Word格式文档下载.docx_第3页
第3页 / 共15页
栈和队列的运用Word格式文档下载.docx_第4页
第4页 / 共15页
栈和队列的运用Word格式文档下载.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

栈和队列的运用Word格式文档下载.docx

《栈和队列的运用Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《栈和队列的运用Word格式文档下载.docx(15页珍藏版)》请在冰豆网上搜索。

栈和队列的运用Word格式文档下载.docx

"

overflow"

;

else

{

top++;

stack[top]=x;

}

voidpop()

if(top==0)cout<

underflow"

elsetop--;

elemtypegettop()

if(top==0){cout<

return0;

}

elsereturnstack[top];

boolempty()

if(top==0)returntrue;

elsereturnfalse;

};

voidmain()

intn,i;

elemtypex;

seqstackS;

S.inistack();

cout<

请输入车厢数"

cin>

>

n;

请输入"

n<

节车厢代号(H代表硬座车厢,S代表软座车厢)"

for(i=1;

i<

=n;

i++)

cin>

x;

if(x=='

H'

)cout<

elseS.push(x);

while(!

S.empty())

x=S.gettop();

cout<

S.pop();

endl;

程序的结果:

2.第2题的算法实现

#define 

elemtype 

char

const 

maxsize=1000;

s1[maxsize],s2[maxsize];

class 

seqstack

stack[maxsize];

int 

top;

void 

inistack()

top=0;

push(elemtype 

x)

top++;

stack[top]=x;

pop()

top--;

gettop()

return 

stack[top];

bool 

empty()

if(top==0) 

true;

else 

false;

oper(elemtype 

op)

switch(op)

case'

+'

:

-'

1;

*'

/'

2;

('

0;

default:

change()

y,ch;

inistack();

push('

@'

);

i=0,j=0;

ch=s1[i];

while(ch!

='

if(ch=='

'

) 

ch=s1[++i];

){push(ch);

)'

){y=gettop();

while(y!

{s2[j++]=y;

s2[j++]='

pop();

y=gettop();

if((ch=='

)||(ch=='

))

if(oper(ch)>

oper(y)) 

{push(ch);

{y=gettop();

s2[j++]=y;

{while(((ch>

0'

)&

&

(ch<

9'

))||(ch=='

.'

{s2[j++]=ch;

s2[j]='

main()

{seqstack 

s;

ch;

i=0;

cout<

请输入中缀表达式('

结束):

cin>

{s1[i++]=ch;

s1[i]='

中缀表达式为:

for(int 

j=0;

j<

i;

j++)cout<

s1[j];

s.change();

后缀表达式为:

while(s2[j]!

{cout<

s2[j];

j++;

程序的结果

3.第3题的算法运算

classlink

elemtypedata;

link*next;

classlinkstack

link*top;

top=newlink;

top->

next=NULL;

link*s=newlink;

s->

data=x;

next=top->

next;

next=s;

link*s=top->

if(s!

=NULL)

top->

next=s->

deletes;

if(top->

next!

return(top->

next->

data);

elsereturnNULL;

boolempty()

next==NULL)returntrue;

voiddecTOhex(intn)

elemtypex;

inistack();

while(n);

{intj=n%16;

if(j<

10)x=j+48;

elsex=j+55;

push(x);

n=n/16;

while(!

empty())

x=gettop();

cout<

pop();

intn;

linkstacks;

请输入十进制正整数:

十进制数"

的十六进制为"

s.decTOhex(n);

4.第4题的算法实现

constintN=5;

constintM=6;

typedefstruct

intnum;

intarrtime;

}elemtype;

structseqstack

elemtypestack[N+1];

}s1,s2;

structlink

structlinkqueue

link*front,*rear;

}q;

seqstackinistack(seqstackS)

S.top=0;

returnS;

seqstackpush(seqstackS,elemtypex)

S.top++;

S.stack[S.top]=x;

seqstackpop(seqstackS)

S.top--;

returnS;

elemtypegettop(seqstackS)

returnS.stack[S.top];

intempty(seqstackS)

if(S.top==0)return1;

elsereturn0;

linkqueueiniqueue(linkqueues)

link*p;

p=newlink;

p->

s.front=s.rear=p;

returns;

linkqueueenqueue(linkqueues,elemtypex)

next=s.rear->

s.rear->

next=p;

s.rear=p;

linkqueuedlqueue(linkqueues)

link*p=s.front;

s.front=p->

deletep;

elemtypegethead(linkqueues)

returns.front->

data;

intemptyqueue(linkqueues)

if(s.front==s.rear)return1;

voidarrive(elemtypex)

if(s1.top==N)

q=enqueue(q,x);

elses1=push(s1,x);

voiddelive(elemtypex)

intf=1;

elemtypey;

link*r;

while((!

empty(s1))&

(f==1))

if(s1.stack[s1.top].num!

=x.num)

{y=gettop(s1);

s1=pop(s1);

s2=push(s2,y);

else{

f=0;

y=gettop(s1);

停车场中有编号为"

x.num<

的车"

该车将离开,应收停车费:

(x.arrtime-y.arrtime)*M<

元"

while(!

empty(s2))

{y=gettop(s2);

s2=pop(s2);

s1=push(s1,y);

if(!

emptyqueue(q)){

y=gethead(q);

q=dlqueue(q);

s1=push(s1,y);

}

if(empty(s1))

emptyqueue(q))

{

link*p=q.front;

while((p!

=NULL)&

(p->

data.num!

=x.num))

{r=p;

p=p->

if(p!

=NULL){

cout<

便道上有编号为"

的车辆"

该车将离开,应收停车费0.00元:

r->

next=p->

deletep;

}

else

cout<

便道上没有编号为"

的车辆,输入的车辆不存在!

!

voidpr1()

intt=s1.top;

停车场中的车辆编号和到达时间"

while(t!

=0)

s1.stack[t].num<

"

s1.stack[t].arrtime<

t--;

voidpr2()

link*p=q.front->

便道中的车辆编号和到达时间"

while(p!

p->

data.num<

data.arrtime<

p=p->

s1=inistack(s1);

s2=inistack(s2);

q=iniqueue(q);

while

(1){

请输入车辆编号、到达或离开的时间"

x.num>

x.arrtime;

请输入处理车辆的代号(1到达2离开3结束)"

if(n==1){arrive(x);

pr1();

pr2();

elseif(n==2){delive(x);

elsebreak;

实验总结:

这次实验当中,有很多符号,程序本身也很长,所以一个程序有很多错误,找的时候有不知所以。

有些定义也不是很清楚,总之,这次实验做的相当的差。

不过,我会吸取这次种种错误的原因,以便下次不会再出同样的错误。

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

当前位置:首页 > 小学教育 > 其它课程

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

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