完成栈

This commit is contained in:
lifei
2020-12-01 13:24:18 +08:00
parent 5e741276ab
commit 7be599dd81
5 changed files with 520 additions and 2 deletions

View File

@@ -55,6 +55,26 @@ $$
=42
$$
### 1.3. 顺序栈
### 1.3. [顺序栈](stack/README.md)
### 1.4. 链式栈
顺序存储,用静态数据实现,并需要记录栈顶指针。
基本操作
- 初始化(创)
- 进栈(增)
- 出栈(删)
- 获取栈顶元素(查询)
两种实现
- 初始化时 `top=-1`
- 初始化时 `top=0`
共享栈
- 两个栈共享同一片内存空间,两个栈从两边往中间增长。
- 初始化:`S.top0 = -1;``S.top1 = MaxSize;`
- 栈满条件:`S.top0 + 1 == S.top1;`
### 1.4. [链栈](stack/README.md)

235
ch3/stack/README.md Normal file
View File

@@ -0,0 +1,235 @@
# 栈
## 1. 顺序栈
### 1.1. 定义
用顺序存储方式实现的栈。
```cpp
#define MaxSize 10
typedef struct
{
ElemType data[MaxSize];
int top; // 栈顶指针
} SqStack;
```
$MaxSize*sizeof(ElemType)+4B$
### 1.2. 初始化栈
```cpp
// 初始化栈
void InitStack(SqStack &S)
{
S.top = -1;
}
```
```cpp
// 判断栈空
bool StackEmpty(SqStack S)
{
return S.top == -1;
}
```
```cpp
// 判断栈满
bool StackFull(SqStack S)
{
return S.top == MaxSize - 1;
}
```
### 1.3. 进栈
```cpp
// 进栈
bool Push(SqStack &S, int x)
{
if (StackFull(S))
{
return false;
}
// S.top++;
// S.data[S.top] = x;
S.data[++S.top] = x;
return true;
}
```
### 1.4. 出栈
```cpp
// 出栈,数据还残留在内存中,只是逻辑上被删除了
bool Pop(SqStack &S, int &x)
{
if (StackEmpty(S))
{
return false;
}
// x = S.data[S.top];
// S.top--;
x = S.data[S.top--];
return true;
}
```
### 1.5. 获取栈顶元素
```cpp
// 读取栈顶元素
bool GetTop(SqStack &S, int &x)
{
if (StackEmpty(S))
{
return false;
}
x = S.data[S.top];
return true;
}
```
### 1.6. 另一种初始化栈的方式
```cpp
// 初始化栈
void InitStack(SqStack &S)
{
S.top = 0;
}
```
```cpp
// 判断栈空
bool StackEmpty(SqStack S)
{
return S.top == 0;
}
```
```cpp
// 判断栈满
bool StackFull(SqStack S)
{
return S.top == MaxSize;
}
```
### 1.7. 顺序栈的缺点
栈的大小不可改变。
### 1.8. 共享栈
两个栈共享同一片内存空间,两个栈从两边往中间增长。
```cpp
#define MaxSize 10
typedef struct
{
ElemType data[MaxSize];
int top0;
int top1;
} ShStack;
```
```cpp
// 初始化共享栈
void InitStack(ShStack &S)
{
S.top0 = -1;
S.top1 = MaxSize;
}
```
```cpp
// 判断栈满
bool StackFull(ShStack S)
{
return S.top0 + 1 == S.top1;
}
```
## 2. 链栈
```cpp
typedef struct LNode
{
int data;
struct LNode *next;
} LNode, *LinkStack;
```
### 2.1. 初始化
```cpp
// 初始化一个链栈,带头结点
bool InitStack(LinkStack &S)
{
S = (LNode *)malloc(sizeof(LNode));
if (S == NULL)
{
return false;
}
S->next = NULL;
return true;
}
```
```cpp
// 判断栈空
bool StackEmpty(LinkStack S)
{
return S->next == NULL;
}
```
### 2.2. 进栈
```cpp
// 进栈
bool Push(LinkStack &S, int x)
{
LNode *s = (LNode *)malloc(sizeof(LNode));
s->data = x;
s->next = S->next;
S->next = s;
return true;
}
```
### 2.3. 出栈
```cpp
// 出栈
bool Pop(LinkStack &S, int &x)
{
if (StackEmpty(S))
{
return false;
}
LNode *q = S->next;
x = q->data;
S->next = q->next;
free(q);
return true;
}
```
### 2.4. 获取栈顶元素
```cpp
// 读取栈顶元素
bool GetTop(LinkStack &S, int &x)
{
if (StackEmpty(S))
{
return false;
}
x = S->next->data;
return true;
}
```

89
ch3/stack/link-with.cpp Normal file
View File

@@ -0,0 +1,89 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;
} LNode, *LinkStack;
// 初始化一个链栈,带头结点
bool InitStack(LinkStack &S)
{
S = (LNode *)malloc(sizeof(LNode));
if (S == NULL)
{
return false;
}
S->next = NULL;
return true;
}
// 判断栈空
bool StackEmpty(LinkStack S)
{
return S->next == NULL;
}
// 进栈
bool Push(LinkStack &S, int x)
{
LNode *s = (LNode *)malloc(sizeof(LNode));
s->data = x;
s->next = S->next;
S->next = s;
return true;
}
// 出栈
bool Pop(LinkStack &S, int &x)
{
if (StackEmpty(S))
{
return false;
}
LNode *q = S->next;
x = q->data;
S->next = q->next;
free(q);
return true;
}
// 读取栈顶元素
bool GetTop(LinkStack &S, int &x)
{
if (StackEmpty(S))
{
return false;
}
x = S->next->data;
return true;
}
int main()
{
LinkStack S;
InitStack(S);
Push(S, 1);
Push(S, 2);
Push(S, 3);
Push(S, 4);
Push(S, 5);
// 打印操作
LNode *i = S->next;
while (i != NULL)
{
printf("%d\n", i->data);
i = i->next;
}
int x;
Pop(S, x);
// printf("%d\n", x);
// 打印操作
i = S->next;
while (i != NULL)
{
printf("%d\n", i->data);
i = i->next;
}
return 0;
}

89
ch3/stack/sequence.cpp Normal file
View File

@@ -0,0 +1,89 @@
#include <stdio.h>
#define MaxSize 10
typedef struct
{
int data[MaxSize];
int top; // 栈顶指针
} SqStack;
// 初始化栈
void InitStack(SqStack &S)
{
S.top = -1;
}
// 判断栈空
bool StackEmpty(SqStack S)
{
return S.top == -1;
}
// 判断栈满
bool StackFull(SqStack S)
{
return S.top == MaxSize - 1;
}
// 进栈
bool Push(SqStack &S, int x)
{
if (StackFull(S))
{
return false;
}
// S.top++;
// S.data[S.top] = x;
S.data[++S.top] = x;
return true;
}
// 出栈,数据还残留在内存中,只是逻辑上被删除了
bool Pop(SqStack &S, int &x)
{
if (StackEmpty(S))
{
return false;
}
// x = S.data[S.top];
// S.top--;
x = S.data[S.top--];
return true;
}
// 读取栈顶元素
bool GetTop(SqStack &S, int &x)
{
if (StackEmpty(S))
{
return false;
}
x = S.data[S.top];
return true;
}
int main()
{
SqStack S;
InitStack(S);
Push(S, 1);
Push(S, 2);
Push(S, 3);
Push(S, 4);
Push(S, 5);
// 打印操作
int i = S.top;
while (i > -1)
{
printf("%d\n", S.data[i--]);
}
int x;
Pop(S, x);
// printf("%d\n", x);
// 打印操作
i = S.top;
while (i > -1)
{
printf("%d\n", S.data[i--]);
}
return 0;
}

85
ch3/stack/sequence2.cpp Normal file
View File

@@ -0,0 +1,85 @@
#include <stdio.h>
#define MaxSize 10
typedef struct
{
int data[MaxSize];
int top; // 栈顶指针
} SqStack;
// 初始化栈
void InitStack(SqStack &S)
{
S.top = 0;
}
// 判断栈空
bool StackEmpty(SqStack S)
{
return S.top == 0;
}
// 判断栈满
bool StackFull(SqStack S)
{
return S.top == MaxSize;
}
// 进栈
bool Push(SqStack &S, int x)
{
if (StackFull(S))
{
return false;
}
S.data[S.top++] = x;
return true;
}
// 出栈,数据还残留在内存中,只是逻辑上被删除了
bool Pop(SqStack &S, int &x)
{
if (StackEmpty(S))
{
return false;
}
x = S.data[--S.top];
return true;
}
// 读取栈顶元素
bool GetTop(SqStack &S, int &x)
{
if (StackEmpty(S))
{
return false;
}
x = S.data[S.top - 1];
return true;
}
int main()
{
SqStack S;
InitStack(S);
Push(S, 1);
Push(S, 2);
Push(S, 3);
Push(S, 4);
Push(S, 5);
// 打印操作
int i = S.top - 1;
while (i > -1)
{
printf("%d\n", S.data[i--]);
}
int x;
Pop(S, x);
// printf("%d\n", x);
// 打印操作
i = S.top - 1;
while (i > -1)
{
printf("%d\n", S.data[i--]);
}
return 0;
}