堆栈和队列基本函数docxWord文件下载.docx

上传人:b****8 文档编号:22881716 上传时间:2023-02-05 格式:DOCX 页数:21 大小:29.97KB
下载 相关 举报
堆栈和队列基本函数docxWord文件下载.docx_第1页
第1页 / 共21页
堆栈和队列基本函数docxWord文件下载.docx_第2页
第2页 / 共21页
堆栈和队列基本函数docxWord文件下载.docx_第3页
第3页 / 共21页
堆栈和队列基本函数docxWord文件下载.docx_第4页
第4页 / 共21页
堆栈和队列基本函数docxWord文件下载.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

堆栈和队列基本函数docxWord文件下载.docx

《堆栈和队列基本函数docxWord文件下载.docx》由会员分享,可在线阅读,更多相关《堆栈和队列基本函数docxWord文件下载.docx(21页珍藏版)》请在冰豆网上搜索。

堆栈和队列基本函数docxWord文件下载.docx

return1;

intPop_Seqstack(Seqstack*s,datatype*x)严出栈函数1•先决条件:

从栈中岀一个数据,并将其存放到x中咸功返回1,因栈空失败返回0*/

top==-l)

*x=s->

top];

top—;

intTop_Seqstack(Seqstack*s,datatype*x)

/*取栈顶元素函数1•先决条件:

取栈顶元素,并把其存放到x中,成功返回1,因栈空失败返回0*/

intPrintf_Seqstack(Seqstack*s)

/*遍历顺序栈函数1•先决条件:

遍历顺序栈,成功返回P/

inti,j=0;

for(i=s->

top;

i>

=0;

i-)

printf("

%dn,s->

data[i]);

/*因datatype不同而不同*/

j++;

if(j%!

0==0)

printf('

\nM);

printf(n\nn);

intConversation_Seqstack(intN,intr)

/*数制转换函数(顺序栈)1•先决条件:

具有置空栈,入栈,出栈函数2•函数作用:

将N转换为r进制的数*/

Scqstack*s;

datatypex;

printf(n%d转为%d进制的数为:

“,N,r);

/*以后可以删除去*/

s=Init_Seqstack();

do

Push_Seqstack(s$N%r);

N=N/r;

}whilc(N);

while(Pop_Seqstack(s,&

x))

if(x>

=10)/*为了能转为十进制以上的*/printf(”%c”,x+55);

else

printf(”%d”,x);

free(s);

/*释放顺序栈*/

printf(n\nM);

4.主函数

intmain()

intchoice;

do{

printfCU.置空栈2•判栈空3•入栈4•出栈5•収栈顶元素6•遍历7•退出\「);

printf(H请输入选择(1〜7):

”);

scanf(H%d'

\&

choice);

getchar();

switch(choice)case1:

if(s)

B空栈成功!

\nH);

break;

case2:

if(Empty_Seqstack(s))

printf(”此为空栈An”);

'

此不为空栈.\nM);

;

case3:

请输入一个整数:

scanf(”%d”,&

x);

if(Push_Seqstack(s,x))printf(n入栈成功An”);

,栈已满,无法入栈AnM);

case4:

if(Pop_Seqstack(s,&

出栈成功,出栈元素为:

%d\n”,x);

printf(”出栈失败,因栈为空.\nu);

case5:

if(Top_Seqstack(s,&

print”取栈顶元素成功,栈顶元素为:

printf(“取栈顶元素失败,因栈为空AiT);

case6:

Printf_Seqstack(s);

case7:

printf(M谢谢使用!

\n"

);

brcak;

default:

输入错误,请重新输入!

}while(choice!

=7);

二链栈

#includc<

typedefstructsnode

datatypedata;

structsnode*next;

}Stacknode,*Linkstack;

LinkstackInit_Linkstack()

/*初始化栈函数1・先决条件:

无2•函数作用:

初始化链栈,返回top地址*/

Linkstacktop;

top=(Linkstack)malloc(sizcof(Stacknodc));

toponext二NULL;

returntop;

intEmpty_Linkstack(Linkstacktop)

/*判栈空函数1•先决条件:

初始化链栈2•函数作用:

判断栈是否为空,空返回1,不空返回0引

if(top->

next==NULL)

intPush_Linkstack(Linkstacktop.datatypex)

八入栈函数1•先决条件:

将数据x入栈,成功返回1,失败返回0引

Stacknode*p;

p=(Stacknode*)malloc(sizeof(Stacknode));

p->

data=x;

next=top->

next;

top->

ncxt=p;

intPop_Linkstack(Linkstacktop^datatype*x)

八出栈函数1•先决条件:

若栈空退岀,若没空则将数据岀栈,并将其存放到x中,成功返回1,因栈空失败返回0*/

Stacknode*p=top->

*x=p->

data;

next=p->

free(p);

intTop_Linkstack(Linkstacktop,datatypc*x)

取栈顶元素并放到x中,成功返回1,

因栈空失败返回0*/

next==NULL)

*x=top->

next->

intPrintf_Linkstack(Linkstacktop)

/*遍历链栈函数1•先决条件:

遍历链栈,成功返回1*/

intj=0;

while(p)

printf(H%d"

p->

data);

if(j%10==0)

p=p->

printf(H\nM);

intConversation_Linkstack(intN,intr)

/*数制转换函数(链栈)1•先决条件:

将N转换为r进制的数*/

printf(”%d转为%d进制的数为:

°

N,r);

top=Init_Linkstack();

Push_Linkstack(top,N%r);

}while(N);

while(Pop_Linkstack(top,&

=10)/*为了能转为十进制以上的*/

printf(”%c”,x+55);

printf(H%dM,x);

printf(,,\iiM);

free(top);

/*释放栈顶空间*/

printfCl.置空栈2•判栈空3•入栈4•出栈5•取栈顶元素6遍历7•退出W);

printf(M请输入选择(1〜7):

getchar();

if(top)

\nM);

if(Empty_Linkstack(top))

printf(”此为空栈An”);

printfC毗不为空栈.\n"

case3:

"

);

scanf(”%d”,&

if(Push_Linkstack(top,x))

printfC'

入栈成功An”);

printfC'

栈已满,无法入栈AnM);

if(Pop_Linkstack(top,&

printfC*出栈成功,出栈元素为:

%d\n"

x);

else

printfC*Hj栈失败,因栈为空.\n'

*);

if(Top_Linkstack(top,&

printfC®

栈顶元素成功,栈顶元素为:

%d\n”,x);

printf(”取栈顶元素失败,因栈为空An”);

break;

Printf_Linkstack(top);

printf(M谢谢使用!

defai山:

prinlf("

输入错课,请重新输入!

\n”);

}whilc(choicc!

三•顺序队列

1.宏定义

#include<

#dcfincdatatype****

2•结构体

typedefstruct

datatypedata[MAXSIZE];

intrear,front;

intnum;

}Sequeue;

3.基本函数

Sequeue*Init_Sequeue()

/*初始化顺序队列函数1・先决条件:

初始化顺序队列,成功返回顺序队列首地址q*/

Sequeue*q;

q=(Sequeue*)malloc(sizeof(Sequeue));

q->

front=q->

rear=MAXSIZE-1;

num=();

returnq;

intIn_Sequeue(Sequeue*q,datatypex)

严入队函数1・先决条件:

初始化顺序队列2•函数作用:

将数据x入队到q中,成功返回1,因队满失败返回0*/

if(q->

num==MAXSIZE)

队满An**);

/*可视情况删除此句*/

rear=(q->

rear+1)%MAXSIZE;

datafq->

rear]=x;

num++;

intFront_Sequeue(Sequeue*q3atatype*x)

/*取队头元素函数1•先决条件:

初始化顺序表2•函数作用:

取队头元素并将其存放到x中咸功返回1,因队空而失败返回0旬

num==0)

printf(H队空.\『);

/*町视怙:

况删除此句旬return0;

*x=q->

data[q->

front+1];

intOut_Sequeue(Sequeuedatatype*x)

/*出队函数1・先决条件:

将数据x入队到q中,成功返回1,因队空失败返回0*/

printf(”队空.\n“);

front=(q->

front+1)%MAXSIZE;

*x=q->

front];

qonum—;

intEmpty_Sequeue(Sequeue*q)

/*判空函数1・先决条件:

是空返回1,非空返回0*/

intPrintfLSequeue(Sequeue*q)

/*遍历函数1・先决条件:

遍历输岀所有数据引

inti,j=q->

front+l,n=q->

num;

for(i=0;

i<

n;

i++)

%d"

q->

data[j]);

/*因datatype数据不同而不同*/

j=j%MAXSIZE;

printf(n\iiM);

4•主函数

datatypex;

do{

■▲/*fI\fix

W^■*▼"

f<

TwrTw»

T^rTw♦丫・»

Tw»

Tw9T^»

T^9T%»

TwrrwrTw»

T^^T%»

T^»

Twr^rTwrjw9n»

T>

<

|wrTw»

T^^T%\\•

printf(Ml.初始化2.判队空3.入队4.出队5.取队头元6.遍历7.退出\n“);

■▲,、/fI•士•上•士•上%L«

«

&

»

•上•士•上«

•土•士«

•上•上«

*•士«

•上«

%V•士«

%V«

^\11\

W"

■#T^#T*#T^#T^#T^#T^#T^#T^#T^吓・#T^吓・吓・\B•

scanf(M%dn,&

switch(choicc)

casel:

q=lnit_Sequeue();

if(q)

H空队成功!

if(Empty_Scqucuc(q))

printf(n此为空队An”);

printf(H此不为空队Ann);

scanf(H%dn,&

if(In_Sequeue(q,x))

printf(“入队成功An”);

printf(H队已满,无法入队AnH);

if(Out_Scqucuc(q,&

printf(”出队成功,出队元素为:

printfC*出队失败,因队为空Anu);

if(Front_Sequeue(q,&

取队头元素成功,队头元素为:

printfC*取队头元素失败,因队为空An“);

Printf_Sequeue(q);

printf(M谢谢便用!

default:

printf(”输入错误,请重新输A!

四•链队列

1•宏定义

#definedatatypeint

typedefstructnode

structnode*next;

JQnodc;

Qnode*front,*rear;

}Lqueue;

Lqueue*Init_Lqueue()

/*初始化链队列函数1・先决条件:

初始化链队列,成功返回顺序队列首地址q(头结点尸/

Lqueue*q;

Qnode*p;

q=(Lqueue*)malloc(sizeof(Lqueue));

p=(Qnode*)malloc(sizeof(Qnode));

ncxt=NULL;

front=p;

rear=p;

intIn_Lqueue(Lqueue*q,datatypex)

/*入队函数1•先决条件:

初始化链队列2•函数作用:

将数据x入队到q中咸功返回W

next=NULL;

rear->

next=p;

intEmpty_Lqueue(Lqueue*q)

/*判空函数1•先决条件:

front==q->

rear)

intFront_Lqueue(Lqueue*q,datatype*x)

取队头元素并将其存放到x中,成功返回1,因队空而失败返回0*/

if(Empty_Lqueue(q))

printf(”队空.\n”);

front->

intOut_Lqueue(Lqueuedatatype*x)

/*出队函数1•先决条件:

将数据x入队到q中,成功返回1,因队空失败返回0*/

Qnodc*p;

p=q->

next=p・>

next;

ncxt==NULL)

rear=q->

front;

intPrintf_Lqueue(Lqueue*q)

/*遍历函数1•先决条件:

遍历输出所有数据引

for(p=q->

p!

=NULL;

next)

%d”,p->

printf(”\n”);

4•主函数

intchoice;

printfCl.初始化2•判队空3入队4•出队5•取队头元6遍历7•退出W);

scanf(n%dH,&

q=lnit_Lqueue();

if(Empty_Lqucuc(q))

if(In_Lqueue(q,x))

if(Out_Lqueue(q,&

x))printf("

iTi队成功,出队元素为:

printf(”出队失败,因队为空.\nn);

if(Front_Lqueue(q,&

printf(”取队头元素成功,队头元素为:

elseprintfCl队头元素失败,因队为空.\n“);

Printf_Lqueue(q);

printf(H谢谢使用!

\ii"

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

当前位置:首页 > 高等教育 > 教育学

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

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