顺序栈
#include <iostream>
#include<stdio.h>
#define MaxSize 100
typedef int ElemType;
typedef struct SqStack {
ElemType data[MaxSize];
int top;
}SqStack;
//初始化
void InitStack(SqStack &S) {
S.top = -1;
}
//判空
bool StackEmpty(SqStack S) {
return S.top == -1;
}
//入栈
bool Push(SqStack& S, ElemType e) {
if (S.top == MaxSize-1) {
return false;
}
S.data[++S.top] = e;
return true;
}
///出栈
bool Pop(SqStack& S,ElemType &e) {
if (S.top == -1) {
return false;
}
e = S.data[S.top--];
return true;
}
//获取栈顶元素
bool GetTop(SqStack S, ElemType& e) {
if (S.top == -1) {
return false;
}
e = S.data[S.top];
return true;
}
int main()
{
SqStack S;
InitStack(S);
printf("判空:%d\n", StackEmpty(S));
Push(S, 1);
Push(S, 2);
Push(S, 3);
ElemType e;
Pop(S, e);
printf("e=%d\n", e);
GetTop(S, e);
printf("e=%d\n", e);
printf("判空:%d\n", StackEmpty(S));
return 0;
}
链栈
#include <iostream>
#include<stdio.h>
#define MaxSize 100
typedef int ElemType;
typedef struct LinkNode {
ElemType data;
struct LinkNode *next;
}*LiStack,LinkNode;
//初始化
void InitStack(LiStack &S) {
S = NULL;
}
//判空
bool StackEmpty(LiStack S) {
return S == NULL;
}
//入栈
bool Push(LiStack &S, ElemType e) {
LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
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;
}
LinkNode* p = S;
e = p->data;
S = S->next;
free(p);
return true;
}
//获取栈顶元素
bool GetTop(LiStack S, ElemType& e) {
if (S == NULL) {
return false;
}
e = S->data;
return true;
}
int main()
{
LiStack S;
InitStack(S);
printf("判空:%d\n", StackEmpty(S));
Push(S, 1);
Push(S, 2);
Push(S, 3);
ElemType e;
Pop(S, e);
printf("e=%d\n", e);
GetTop(S, e);
printf("e=%d\n", e);
printf("判空:%d\n", StackEmpty(S));
return 0;
}
入栈出栈的排列组合有=
1
n
+
1
C
2
n
n
\frac{1}{n+1}C^n_{2n}
n+11C2nn种(卡特兰数)
共享栈
#include<stdio.h>
#define MaxSize 100
typedef int ElemType;
typedef struct {
ElemType stack[MaxSize];
int top[2];
}Stack;
void InitStack(Stack &S) {
S.top[0] = -1;
S.top[1] = MaxSize;
}
bool Push(Stack &S, ElemType e, int i) {
if (S.top[1] - S.top[0] == 1) {
return false;
}
if (i == 1) {
S.stack[++S.top[0]] = e;
}
else {
S.stack[--S.top[1]] = e;
}
return true;
}
bool Pop(Stack &S, ElemType& e, int i) {
if (i == 1) {
if (S.top[0] == -1) {
return false;
}
e = S.stack[S.top[0]--];
}
else {
if (S.top[1] == MaxSize) {
return false;
}
e = S.stack[S.top[1]++];
}
//cout << e << endl;
return true;
}
int main() {
Stack S;
InitStack(S);
Push(S, 1,1);
Push(S, 2,2);
Push(S, 3,1);
Push(S, 4,2);
Push(S, 5,1);
cout << S.top[0] << " " <<S.top[1] << endl;
ElemType e = 0;
Pop(S, e, 1);
cout << e << endl;
//cout << S.top[0] << " " << S.top[1] << endl;
Pop(S, e, 1);
cout << e << endl;
Pop(S, e, 2);
cout << e << endl;
}
提升
//用栈判断单链表是否中心对称
//算法思想:利用快慢指针找到中间位置,将其后面的节点值全部入栈,然后重新从头结点开始于栈顶元素一一匹配,匹配失败返回false,匹配成功,链表指针移向下一个,栈顶元素弹出,继续匹配下一个,直到栈空返回true
bool dc(LinkList L) {
LNode* slow = L, * fast = L;
int top = -1;
ElemType Stack[100];//声明一个栈
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
slow = slow->next;
while(slow) {
Stack[++top] = slow->data;//入栈
slow = slow->next;
}
LNode* p = L->next;
while (top != -1) {
ElemType x = Stack[top--];//出栈
if (x != p->data) {
return false;
}
p = p->next;
}
return true;
}
//I代表入栈,O代表出栈,判断入栈出栈操作是否合法,合法条件:栈出栈数任何情况下都必须小于等入,且栈数最终为空
//算法思想: 定义一个变量统计栈中元素个数,入栈-1,出栈+1,如果元素个数 为负数则错误,最终个数不为0也是错误
bool Judge(char A[]){
int s = 0;
for(int i = 0; A[i]!='\0'; i++){
if(A[i] == 'I'){
s++;
}
if(A[i] == 'O'){
s--;
}
if(s < 0){
return false;
}
}
return s == 0;
}
void Train_Arrange(char *train){
//用字符串train表示火车,H表示硬座,S表示软座
char *p = train,*q = train, x;
Stack s;
InitStack(s);
while(*p!='\0'){
if(*p == 'H'){
Push(s, *p);
}else{
*(q++) = *p;
}
p++;
}
while(!StackEmpty(s)){
Pop(s,x);
*(q++) = x;
}
}
//算法思想:设置一一个栈用于保存n和对应的Pn(x)值,栈中相邻元素的P(x)有题中关系。然后边出栈边计算Pn(x),栈空后该值就计算出来了。算法的实现如下:
double P(int n, double x) {
struct Stack {
int no; //保存n
double val;//保存Pn(x)值
}st[MaxSize];
double fv1 = 1, fv2 = 2 * x;
int top = -1;
for (int i = n; i >= 2; i--) {
st[++top].no = i;
}
while (top != -1) {
st[top].val = 2 * x * fv2 - 2 * (st[top].no - 1) * fv1;
fv1 = fv2;
fv2 = st[top].val;
top--;
}
if(n==0)
return fv1;
return fv2;
}
double P1(int n, double x) {
if (n == 0) {
return 1;
}
if (n == 1) {
return 2 * x;
}
return 2 * x * P(n - 1, x) - 2 * (n - 1) * P(n - 2, x);
}
int main() {
while (1) {
int a;
double b;
cin >> a >> b;
cout << P(a, b) << endl;
cout << P1(a, b) << endl;
}
}