栈的顺序存储和链式存储

#include<malloc.h>
#include<iostream>
using namespace std;
#ifndef SEQSTACK
#define SEQSTACK
#define MaxNum 10
typedef int elemtype;




//顺序栈结构体
struct seqstack
{
elemtype sk[MaxNum];
elemtype top;
};


//创建一个空栈
seqstack* CreateSeqStack()
{
seqstack *s;
s = (seqstack*)malloc(sizeof(struct seqstack));
if(!s)
{
printf("malloc error \n");
return NULL;
}
else
{
s->top = -1;
}
}
//入栈
void PushSeqStack(seqstack *sp, elemtype value)
{
if(sp->top == MaxNum - 1)
{
printf("seqstack if full \n");
}
else
{
sp->top+=1;
sp->sk[sp->top] = value;
}

}
//出栈
void PopSeqStack(seqstack *sp)
{
if(sp->top == -1)
{
printf("empty seqstack \n");
}
else
{
sp->top-=1;
printf("pop seqstack seccuss \n");
}
}


//打印栈内所有元素
void DisplaySeqStack(seqstack *s)
{
if(s->top == -1)
{
printf("empty seqstack \n");
}
else
{
cout << "======================================================" << endl;
for(elemtype i = s->top;i >= 0;i --)
{
printf("the %d value is %d \n", i, s->sk[i]);
}
cout << "======================================================" << endl;
}
}


//取栈顶元素
elemtype GetTopValue(seqstack *sp)
{
if(sp->top == -1)
{
printf("empty seqstack \n");
return -1;
}
else
{
return sp->sk[sp->top];
}
}




#endif




/*
seqstack *sp;
sp = CreateSeqStack();
DisplaySeqStack(sp);
int tmp;
while(cin>>tmp)
{
if(999 == tmp)
break;
PushSeqStack(sp,tmp);
}
DisplaySeqStack(sp);
cout << "======================================================" << endl;
cout << "begin pop seqstack" << endl;
PopSeqStack(sp);
cout << "end pop seqstack" << endl;
cout << "======================================================" << endl;
DisplaySeqStack(sp);


cout << "begin get top value" << endl;
tmp = GetTopValue(sp);
cout << tmp << endl;
cout << "end get top seqstack" << endl;

*/












#include<iostream>
#include<malloc.h>
using namespace std;
#ifndef LINKSTACK
#define LINKSTACK


typedef int elemtype;
struct linkstack
{
elemtype data;
linkstack* next;
};


//创建空栈


linkstack* CreateLinkStack()
{
linkstack *sp=NULL;
return sp;
}


//入栈


void PushLinkStack(linkstack* &sp,elemtype eInputValue)
{
linkstack* lSk;
lSk = (linkstack*)malloc(sizeof(linkstack));
if(!lSk)
{
printf("malloc error \n");
return;
}

//判断当前栈是否为空
if(NULL == sp)
{
lSk->data = eInputValue;
lSk->next = NULL;
sp = lSk;
}
else
{
lSk->data = eInputValue;
lSk->next = sp;
sp = lSk;
}
}


//按顺序打印栈内数据元素


void DisplayLinkStack(linkstack* sp)
{
if(NULL == sp)
{
printf("empty linkstack \n");
}
else
{
linkstack *p = sp;
int iLocateValue = 1;
printf("====================================================================\n");
while(p != NULL)
{
printf("the %d value is %d \n", iLocateValue, p->data);
p = p->next;
iLocateValue += 1;
}
printf("====================================================================\n");
}
}


//出栈
void PopLinkStack(linkstack* &sp)
{
if(NULL == sp)
{
printf("pop error empty linkstack \n");
}
else
{
linkstack* p;
p = sp;
sp = p->next;
free(p);
printf("pop success  linkstack \n");
}
}
//取栈顶元素
elemtype GetTopValue(linkstack *sp)
{
elemtype eTmp;
if(NULL == sp)
{
printf("empty linkstack \n");
return -1;
}
else
{
return sp->data;
}
}




#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值