一下午,去吃螺蛳粉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;
}
}