> For the complete documentation index, see [llms.txt](https://aye10032.gitbook.io/data-structure/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aye10032.gitbook.io/data-structure/3-zhan-he-dui-lie.md).

# 3、栈和队列

## 3.1 栈

### 1、构造

顺序实现

```
typedef struct
{
    ElemType data[MaxSize];
    int top;
} Stack;
```

链式实现

```c
typedef struct Linknode
{
    ElemType data;
    struct Linknode *next;
} *LiStack;
```

### 2、计算

当有n个不同的元素入栈，出栈序列的个数为：

$$
\frac{1}{n+1} C\_{2n}^{n}
$$

给定二叉树的**前序序列**作为入栈队列，则所有的出栈为所有可能的**中序遍历**

## 3.2 队列

### 1、构造

```c
typedef struct
{
    ElemType data[MaxSize];
    int front, rear; //队头和队尾指针
} SqQueue;
```

队头出、队尾入

### 2、中缀转后缀

* **操作数**：直接加入表达式
* **界限符**
  * “(”：直接入栈
  * “)”：依次弹出运算符，直到“(”为止
* **运算符**
  * 依次弹出栈中优先级大于等于当前运算符的所有运算符
  * 碰到“(”或栈空停止


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aye10032.gitbook.io/data-structure/3-zhan-he-dui-lie.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
