栈:卡格蓝数

一下午,去吃螺蛳粉QwQ

如果有n个元素进栈,出栈的不同排列个数是:

 

 

顺序栈

#include<stdio.h>
#include<stdlib.h>

#define ElemType int
#define  Maxsize 10 
typedef struct{
	ElemType data[Maxsize];
	int top;
}SqStack;

void InitStack(SqStack &S){
	S.top=-1;
}

void testStack(){
	SqStack S;
	InitStack(S);
}  

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.top=S.top+1;
	S.data[S.top]=x;
	//S.data[++S.top]=x;
	return true;
}
bool Pop(SqStack &S,ElemType &x){
	if(S.top==-1){
		return false;
	}
	x=S.data[S.top];
	S.top=S.top-1;
	//x=S.data[S.top--];
	return true;
}

链表栈

#include<stdio.h>
#include<stdlib.h>
#define ElemType int


typedef struct Linknode{
	ElemType data;
	struct Linknode *next;
}*LiStack,Node;

void InitLiStack(LiStack &S){
	S=NULL;
}
bool Push(LiStack &S,ElemType e){
	Node *p=(Node *)malloc(sizeof(Node));
	if(p==NULL){
		return false;
	}
	p->data=e;
	p->next=S;
	S=p;
	return true;
}
bool Pop(LiStack &S,ElemType &e){
	if(S==NULL){
		return false;
	}
	e=S->data;
	Node *p;
	p=S;
	S=p->next;
	free(p);
	return true;
}

ElemType getElem(LiStack S){
	ElemType e;
	e=S->data;
	return e;
}

bool Empty(LiStack S){
	if(S=NULL){
		return true;
	}
	else{
		return false;
	}
}
int main(void){
	
	LiStack S;
	InitLiStack(S); 
	ElemType a[10]={23,4,2,78,1,8,5,9};
	for(int i=0;i<length(a);i++){
		Push(S,a[i]);
	}
	ElemType e;
	for(int j=0;j<5;j++){
		Pop(S,e);
		printf("%d",e);
	}
	printf("%d",getElem(S));
	
	return 0;
}

共享栈

#include<stdio.h>
#include<stdlib.h>
#define Maxsize 10
#define ElemType int
typedef struct{
	ElemType data[Maxsize];
	int top0;
	int top1;
}ShStack; 
void InitStack(ShStack &S){
	S.top0=-1;
	S.top1=Maxsize; 
}

bool Empty(ShStack &S){
	if(S.top0+1==S.top1){
		return true;
	}
	else{
		return false;
	}
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值