数据结构---栈

顺序栈

#define Maxsize 50 //define the max size of the stack
typedef struct 
{
	Elemtype data[Maxsize];// store the element of the stack
	int top;//
	}SqStack;
	

1.Top值不能超过maxsize
2.空栈的判定条件通常为top==-1,满栈的判定条件通常为top==maxsize-1,,栈中数据元素个数为top+1

判空操作:

bool StackEmpty(SqStack S){
	if (s.top==-1) return true;
	else 			return false;
	}

进栈:

bool Push(SqStack &S,ElemType x)
{
	if(s.top==Maxsize-1) return false;
	S.data[++S.top]=x;return true;
	}

出栈

bool Pop(SqStack &S,ElemType &x)
{
	if (S.top==-1)return false;
	x=S.data[S.top--];
	return true;
	}
	

读取栈顶元素

bool GetTop(SqStack S,ElemType &x){
if (s.top==-1) return false;
x=S.data[S.top];
return true;
}

共享栈

#define Maxsize 100
typedef struct{
	Elemtype data[Maxsize];
	int top1;
	int top2;
	}SqDoubleStack;

进栈

bool Push(SqDoubleStack &S,ElemType x,int stackNum)
{
	if (S.top1+1==s.top2) return false;//stack is full;
	if (stackNum==1) S.data[++S.top1]=x;//stack 1 has elements to come in;
	else if(stackNum==2) S.data[--top2]=x;
	return true;
	}

出栈

bool Pop(SqStack &S,Elemtype &x,int stackNum)
{
 	if(stackNum==1)
 	{
 	if (S.top1==-1)return false;
	x=S.data[S.top1--];
	return true;
 	}
 	else 
 	{
 	if(S.top2==Maxsize-1)return false;
 	x=S.data[S.top2--];
 	return false;
 	}
 	}
 	 

链式栈

typedef struct SNode{
Elemtype data;//
struct SNode *next;
}SNode,*SLink;
typedef struct LinkStack{
	SLink top;
	int count;
	}LinkStack;
 

进栈

bool Push(LinkStack *S,ElemType x)
{
	SLink p=(SLink)malloc(sizeof(SNode));
	p->data=x;
	p->next=S->top;
	S->top=p;
	S->count++;
	return true;
	}

出栈
bool Pop(LinkStack *S,ElemType &x)
{
if (s->top==NULL) return false;
x=S->top->data;
SLink p=S->top;
S->top=S->top->next;
free§;
S->count–;
return true;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不停---

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值