c栈的实现,包括顺序和链式存储结构

中科医院曝光资质 http://finance.sina.com.cn/chanjing/b/20091014/11153079059.shtml
这是栈的实现代码。

如果对大家有帮助,请给一个免费的赞作为支持。有什么问题可以在评论区讨论。

先是顺序栈的代码实现,这种顺序存储结构要先声明栈空间的存储大小,栈中的数据是用数组进行存储的,在栈满扩充空间的时候会不方便。

/*这个程序是用来练习顺序栈的*/

#include

#include

#include

constintTRUE=1;

constintFALSE=0;

constintMAXSIZE=10;//定义栈空间大小

usingnamespacestd;

//定义链表中存储的数据类型

typedefintElemtype;

//定义函数返回值的数据类型

typedefElemtypeStatus;

//定义顺序栈结构

typedefstructNode{

Elemtypedata[MAXSIZE];

inttop;/*用于栈顶指针*/

}Sqstack;//给Node数据类型起名字,可以改成其他名字

StatusPush(Sqstack*S,Elemtypee);/*插入元素e为新的栈顶元素*/

StatusPop(Sqstack*S,Elemtype*e);/*元素出栈*/

voidShowStack(Sqstack*S);

voidIniStack(Sqstack*S);/*用来进行栈的循环操作*/

intmain(void){

Sqstack*S=(Sqstack*)malloc(sizeof(Node));/*初始化栈信息*/

S-top=-1;

IniStack(S);

free(S);

return0;

}

StatusPush(Sqstack*S,Elemtypee){

if(S-top==MAXSIZE-1){/*栈满*/

cout"栈空间已满!!!\n";

returnFALSE;

}

S-top++;/*栈顶指针加一*/

S-data[S-top]=e;/*将新插入的元素赋值给栈顶空间*/

cout"插入元素成功\n";

returnTRUE;

}

StatusPop(Sqstack*S,Elemtype*e){

if(S-top==-1){//说明栈为空

cout"栈已经空了!!\n";

returnFALSE;

}

*e=S-data[S-top];/*将要删除的栈顶元素赋值给e*/

S-top--;/*栈顶指针减一*/

cout"出栈成功\n";

returnTRUE;

}

voidShowStack(Sqstack*S){

intindex=S-top;

if(index==-1){

cout"栈已经空了\n";

return;

}

cout"栈中的元素有:\n";

while(index!=-1){

coutdata[index]"";

index--;

}

cout"\n";

}

voidIniStack(Sqstack*S){

intkey=1;

intchoose;

Elemtypee;

while(key0){

cout"请输入序号选择栈操作:\n"

"\t1.元素入栈\n"

"\t2.元素出栈\n"

"\t3.显示栈中元素\n"

"\t0.退出\n";

cinchoose;

switch(choose)

{

case1:

cout"请输入一个整数作为入栈的元素:\n";

cine;

Push(S,e);

break;

case2:

Pop(S,e);

coute"\n";

break;

case3:

ShowStack(S);

break;

case0:

key=0;

break;

default:

break;

}

}

}

然后是链式存储的栈实现,这种存储结构可以比较方便地扩充栈空间大小。

/*这个程序是用来练习链栈的*/

#include

#include

constintTRUE=1;

constintFALSE=0;

usingnamespacestd;

//定义链表中存储的数据类型

typedefintElemtype;

//定义函数返回值的数据类型

typedefElemtypeStatus;

//定义栈节点数据类型

typedefstructStackNode{

Elemtypedata;

structStackNode*next;

}StackNode,*LinkStackP;

//定义链栈栈顶指针数据类型

typedefstructLinkStack

{

LinkStackPtop;

intcount;

}LinkStack;

StatusPush(LinkStack*S,Elemtypee);/*元素入栈的函数*/

StatusPop(LinkStack*S,Elemtype*e);/*元素出栈的函数*/

StatusStackEmpty(LinkStackS);/*判断栈是否为空*/

voidShowStack(LinkStack*S);

voidIniStack(LinkStack*S);/*用来进行栈的循环操作*/

intmain(void){

//初始化栈空间

LinkStack*S=(LinkStack*)malloc(sizeof(LinkStack));

S-count=0;

S-top=NULL;

IniStack(S);

return0;

}

StatusPush(LinkStack*S,Elemtypee){

LinkStackPs=(LinkStackP)malloc(sizeof(StackNode));

s-data=e;

s-next=S-top;/*把当前的栈顶元素赋值给新节点的直接后继,也就是让旧有的元素后移,给新元素让位,让每一个新元素都会处于栈顶*/

S-top=s;/*让栈顶的指针指向新的节点*/

S-count++;

cout"元素入栈成功!!!\n";

returnTRUE;

}

StatusPop(LinkStack*S,Elemtype*e){

LinkStackPp;

if(StackEmpty(*S)){

cout"栈是空的!!!\n";

returnFALSE;

}

*e=S-top-data;/*将栈顶的元素返回给e*/

p=S-top;/*将栈顶的节点赋值给p*/

S-top=S-top-next;/*将栈顶指针后移*/

free(p);/*释放节点p*/

S-count--;

cout"出栈成功\n";

returnTRUE;

}

StatusStackEmpty(LinkStackS){

if(S.count==0){

returnTRUE;//栈确实是空的

}

returnFALSE;//栈不为空

}

voidShowStack(LinkStack*S){

LinkStackPp;

p=S-top;

intindex=S-count;

if(StackEmpty(*S)){

cout"栈已经空了!!\n";

return;

}

cout"栈中的元素有:\n";

while(index!=0){

coutp"";

p=p-next;//将指针后移

}

cout"\n";

}

voidIniStack(LinkStack*S){

intkey=1;

intchoose;

Elemtypee;

while(key0){

cout"请输入序号选择栈操作:\n"

"\t1.元素入栈\n"

"\t2.元素出栈\n"

"\t3.显示栈中元素\n"

"\t0.退出\n";

cinchoose;

switch(choose)

{

case1:

cout"请输入一个整数作为入栈的元素:\n";

cine;

Push(S,e);

break;

case2:

Pop(S,e);

coute"\n";

break;

case3:

ShowStack(S);

break;

case0:

key=0;

break;

default:

break;

}

}

}




转载请注明:http://www.aierlanlan.com/rzgz/3486.html

  • 上一篇文章:
  •   
  • 下一篇文章: