栈![在这里插入图片描述](https://img-blog.csdnimg.cn/20210519213253899.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUzMTAwNzQx,size_16,color_FFFFFF,t_70)
顺序栈
//入栈
int push(int s[],int x)
{
if (top==M)
{
printf("overflow");
return(-1);
}
s[top++]=x;
return top;
}
//出栈
int pop(int s[], int *q)。
{
if (top==0)
{
printf(“underflow");
return(-1);
}
*q=s[--top];
return top;
}
链栈
typedef struct node
{
int data;
struct node *link;
}JD;
void push(int x)
{
JD *p = (JD *)malloc(sizeof(JD));
p->data=x;
p->link=top;
top=p;
}
int Match_Brackets()
{
SqStack S ;
S=Init_Stack() ; /*堆栈初始化*/
char ch = 0;
scanf(“%c” , &ch) ;
while (asc(ch)!=13)
{
if ((ch==‘(’) || (ch==‘[’))
push(S , ch) ;
else if (ch==‘]’)
{
char x=pop(S) ;
if (x != ‘[’)
return -1;
else if (ch==‘)’)
{
char x=pop(S) ;
if (x != ‘(’)
return -1;
}
scanf(“%c” , &ch) ;
}
if (S.top!=0)
return -1;
else
return 0;
}
栈在递归调用中的应用
例子
//例1:最大公约数的递归实现
//程序1(非递归实现):
int gcd(int a, int b)
{
while (a != 0)
{
int c = b % a;
b = a;
a = c;
}
return b;
}
//程序2(递归实现)
int gcd(int a, int b)
{
if (a==0) return b;
else return gcd(b%a, a);
}
//n阶Hanoi塔问题
void hanoi (int n, char A, char B, char C)
{
if (n==1)
move(A, 1, C); // 将编号为1的圆盘从x移到z
else {
hanoi(n-1, A, C, B); // 将A上编号为1至n-1的圆盘移到B, C作辅助塔
move(A, n, C); // 将编号为n的圆盘从A移到C
hanoi(n-1, B, A, C); // 将B上编号为1至n-1的圆盘移到C, A作辅助塔
}
}
//Gray码的构造
队列
##链队列
队列的顺序存储
//入队
int en_cycque(int sq[], int front, int rear, int x)
{
if (((rear+1)%M)==front)
return -1;
else
{
sq[rear]=x;
rear=(rear+1)%M;
return 0;
}
}
//出队
int dl_cycque(int sq[], int front, int rear, int *q)
{
if(front==rear)
return -1;
else
{
*q=sq[front];
front=(front+1)%M;
return 0;
}
}
双端队列
练习
1:递归输出1. .n的所有排列
int a[MAXN+1];
bool mark[MAXN+1]; // all equal to false initially
Void searchPermutation(int i)
{
if (i == 0)
PrintList(a);
else
for (int j = 1; j <= n; j++)
if (!mark[j])
{
mark[j] = true; a[i] = j;
searchPermutation(i - 1);
mark[j] = false;
}
}
searchPermutation(n);
2:关于FILO
// 1
#include<stdio.h>
int main()
{
char s, Q[8], x = 'A';
int count = 0, top = 0;
scanf("%c", &s);
while (count < 8)
{
if (top > 0 && Q[top-1] == s)
{
top--;
scanf("%c", &s);
count++;
}
else if (x <= 'H')
{
Q[top] = x;
top++;
x++;
}
else
{
printf("0");
return 0;
}
}
printf("1"); return 0;
}
// another 1
#include<stdio.h>
using namespace std;
int main()
{
char s[8];
scanf("%s", s);
int ok = 1;
int i = 0;
int top = -1;
char line[8];
char c = 'A';
while (i < 8)
{
if (top < 0)
line[++top] = c++;
while (line[top] != s[i])
{
if (c > 'H')
{
ok = 0;
break;
}
line[++top] = c++;
}
if (!ok) break;
while (top >= 0 && i < 8 && line[top] == s[i])
{
--top;
++i;
}
}
if (ok) printf("%d\n", 1);
else printf("%d\n", 0);
return 0;
}
3:Gray码的构造
void graycode(int n, int a[])
{
if(n==1)
{
a[0]=0;
a[1]=1;
return;
}
graycode(n-1,a);
int L = 1<<(n-1); // 得到2^(n-1)
for (int i = 0; i < L; i++) a[L + L - i - 1] = a[i] + L;
}
4:循环队列入队出队
#include <stdio.h>
int main()
{
int m,n;
scanf("%d%d", &m, &n);
getchar();
char que[m];
int front = 0;
int rear = 0;
for(int i = 0; i < n; i++)
{
char a[5];
gets(a);
if(a[0] == '1')
{
que[rear]=a[2];
rear=(rear+1)%m;
}
else
front=(front+1)%m;
printf("%d %d\n", front, rear);
}
while (front != rear)
{
printf("%c",que[front]);
front = (front + 1)%m;
}
return 0;
}
思考:FILO的forbidden pattern
挖坑挖坑