mirror of
https://github.com/CodePanda66/CSPostgraduate-408.git
synced 2023-05-21 21:49:33 +08:00
🎉 Init
This commit is contained in:
194
DataStructure/DS_2_StackAndQueue/DS_2_0_SqStack.cpp
Normal file
194
DataStructure/DS_2_StackAndQueue/DS_2_0_SqStack.cpp
Normal file
@@ -0,0 +1,194 @@
|
||||
//
|
||||
// Created by kim on 2020/6/21.
|
||||
//
|
||||
|
||||
//顺序栈的实现
|
||||
#include <stdio.h>
|
||||
|
||||
# define MaxSize 10
|
||||
typedef struct {
|
||||
int data[MaxSize];
|
||||
int top;
|
||||
} SqStack;
|
||||
|
||||
//初始化
|
||||
void InitStack(SqStack &S) {
|
||||
S.top = -1;//这种初始化的方式,栈顶指针始终指向栈顶元素
|
||||
}
|
||||
|
||||
//初始化1
|
||||
void InitStack1(SqStack &S) {
|
||||
S.top = 0;//这种初始化方式,栈顶指针始终会指向栈顶元素的下一空元素
|
||||
}
|
||||
|
||||
//入栈
|
||||
bool Push(SqStack &S, int t) {
|
||||
if (S.top == MaxSize - 1)return false;//栈满
|
||||
S.data[++S.top] = t;
|
||||
// 等价下面两个语句
|
||||
// S.top+=1;//先将栈顶指针指向下一个位置
|
||||
// S.data[S.top]=t;//再填充元素
|
||||
return true;
|
||||
}
|
||||
|
||||
//入栈,初始化1
|
||||
bool Push1(SqStack &S, int t) {
|
||||
if (S.top == MaxSize)return false;//栈满,注意初始化条件不一样判空的条件也不一样了
|
||||
S.data[S.top++] = t;
|
||||
// 等价下面两个语句
|
||||
// S.data[S.top]=t;
|
||||
// S.top+=1;
|
||||
//注意初始化方式不同,上边的顺序也不一样了,先填充元素,再移动指针
|
||||
return true;
|
||||
}
|
||||
|
||||
//出栈,并打印出栈顶元素
|
||||
bool Pop(SqStack &S, int &x) {
|
||||
//判断
|
||||
if (S.top == -1)return false;//栈空报错
|
||||
x = S.data[S.top--];
|
||||
// 等价于下面
|
||||
// x=S.data[S.top];//先取元素
|
||||
// S.top -=1;//再改指针
|
||||
return true;
|
||||
}
|
||||
|
||||
//出栈,并打印出栈顶元素,初始化1
|
||||
bool Pop1(SqStack &S, int &x) {
|
||||
//判断
|
||||
if (S.top == 0)return false;//栈空报错,注意判空条件
|
||||
x = S.data[--S.top];
|
||||
// 等价于下面
|
||||
// S.top -=1;
|
||||
// x=S.data[S.top];
|
||||
//注意初始化方式不同,上边的顺序也不一样了,先将指针指向当前栈顶元素,再将其取出
|
||||
return true;
|
||||
}
|
||||
|
||||
//读取栈顶元素
|
||||
bool GetTop(SqStack S, int &x) {
|
||||
if (S.top == -1)return false;
|
||||
x = S.data[S.top];
|
||||
return true;
|
||||
}
|
||||
//读取栈顶元素,初始化1
|
||||
bool GetTop1(SqStack S, int &x) {
|
||||
if (S.top == 0)return false;
|
||||
x = S.data[S.top-1];//注意按初始化1的方式,这里指针减1才是栈顶元素的位置,
|
||||
// 同时注意不能使用--S.top,因为这里是读取,不可修改原栈,所以不可和出栈一样
|
||||
//但即时这里你错误使用了--S.top,也不会有问题,因为此处的S是值传递,非引用传递,所以你修改的也只是复制之后的S,不会影响原栈S,即时这样也不建议使用--S.top,如果老师较真的话,可以扣分哒
|
||||
return true;
|
||||
}
|
||||
|
||||
//读取栈顶元素的第二种实现方式
|
||||
//利用返回值返回栈顶元素,若成功就返回栈顶元素,失败就返回-1;
|
||||
//缺点:如果栈顶元素就是-1,容易造成误判,正因它的缺点,如果考试考到了它,写这种方式应该是会被扣部分分数的
|
||||
int GetTopOther(SqStack S) {
|
||||
if (S.top == -1)return -1;
|
||||
return S.data[S.top];
|
||||
}
|
||||
|
||||
int GetTopOther1(SqStack S) {
|
||||
if (S.top == 0)return -1;
|
||||
return S.data[S.top-1];
|
||||
}
|
||||
|
||||
|
||||
//打印整个栈
|
||||
void PrintStack(SqStack S){
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top>=0){//注意判空的条件
|
||||
printf("S[%d]=%d\n",S.top,S.data[S.top--]);
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
//打印整个栈,初始化方式1
|
||||
void PrintStack1(SqStack S){
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top>0){//注意判空的条件
|
||||
printf("S1[%d]=%d\n",S.top-1,S.data[--S.top]);//初始化方式1得先移动指针再获取元素
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
|
||||
|
||||
void testStack() {
|
||||
printf("开始测试\n");
|
||||
SqStack S;
|
||||
printf("测试第一种初始化方式\n");
|
||||
InitStack(S);
|
||||
if (Push(S,1)){
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push(S,2)){
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack(S);
|
||||
int x;
|
||||
if (Pop(S, x)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack(S);
|
||||
int x1;
|
||||
if (GetTop(S,x1)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x1);
|
||||
}else{
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
}
|
||||
int x4=GetTopOther(S);
|
||||
if (x4!=-1){
|
||||
printf("第二种读取栈顶元素的方式成功了,栈顶元素为:%d\n",x4);
|
||||
} else{
|
||||
printf("第二种读取栈顶元素的方式失败了\n");
|
||||
}
|
||||
|
||||
|
||||
printf("测试第二种初始化方式\n");
|
||||
SqStack S1;
|
||||
InitStack1(S1);
|
||||
if (Push1(S1,1)){
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push1(S1,2)){
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack1(S1);
|
||||
int x2;
|
||||
if (Pop1(S1, x2)){
|
||||
printf("出栈成功,弹出的元素为[%d]\n",x2);
|
||||
} else{
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack1(S1);
|
||||
int x3;
|
||||
if (GetTop1(S1,x3)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x3);
|
||||
}else{
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
}
|
||||
int x5=GetTopOther1(S1);
|
||||
if (x5!=-1){
|
||||
printf("第二种读取栈顶元素的方式成功了,栈顶元素为:%d\n",x5);
|
||||
} else{
|
||||
printf("第二种读取栈顶元素的方式失败了\n");
|
||||
}
|
||||
|
||||
printf("测试完毕了!\n");
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
printf("Hello, SqStack!");
|
||||
testStack();
|
||||
return 0;
|
||||
}
|
||||
160
DataStructure/DS_2_StackAndQueue/DS_2_1_ShStack.cpp
Normal file
160
DataStructure/DS_2_StackAndQueue/DS_2_1_ShStack.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
//
|
||||
// Created by kim on 2020/6/28.
|
||||
//
|
||||
|
||||
//共享顺序栈的实现
|
||||
//简单来说就是两个栈共享一片存储空间,提高顺序栈的对存储空间的使用率
|
||||
#include <stdio.h>
|
||||
|
||||
# define MaxSize 10
|
||||
typedef struct {
|
||||
int data[MaxSize];
|
||||
int top0;
|
||||
int top1;
|
||||
} ShStack;
|
||||
//从结构体的定义就可以看出来,两个共享栈的根源就在于定义两个指针
|
||||
|
||||
//初始化
|
||||
void InitStack(ShStack &S) {
|
||||
S.top0 = -1;//这种初始化的方式,栈顶指针始终指向栈顶元素
|
||||
S.top1 =MaxSize;//这里的MaxSize就是所谓的第二个栈的栈底
|
||||
//可以根据顺序栈的第二种初试化方式,思考一下这种共享顺序栈的第二种初始化方式
|
||||
//S.top0=0
|
||||
//S.top1=MaxSize-1
|
||||
}
|
||||
|
||||
|
||||
//入栈0
|
||||
bool Push0(ShStack &S, int t) {
|
||||
if (S.top0 +1== S.top1)return false;//注意共享栈满的条件
|
||||
S.data[++S.top0] = t;//仔细品味一下这个++S.top
|
||||
return true;
|
||||
}
|
||||
//入栈1
|
||||
bool Push1(ShStack &S, int t) {
|
||||
if (S.top0 +1== S.top1)return false;//注意共享栈满的条件
|
||||
S.data[--S.top1] = t;//仔细品味一下这个--S.top,想想为什么?
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//出栈,并打印出栈顶元素
|
||||
bool Pop0(ShStack &S, int &x) {
|
||||
//判断
|
||||
if (S.top0 == -1)return false;//栈空报错
|
||||
x = S.data[S.top0--];
|
||||
// 等价于下面
|
||||
// x=S.data[S.top];//先取元素
|
||||
// S.top -=1;//再改指针
|
||||
return true;
|
||||
}
|
||||
//出栈1
|
||||
bool Pop1(ShStack &S, int &x) {
|
||||
//判断
|
||||
if (S.top1 == MaxSize)return false;//注意一下它的栈空报错条件
|
||||
x = S.data[S.top1++];//注意这个栈修改指针是++
|
||||
// 等价于下面
|
||||
// x=S.data[S.top];//先取元素
|
||||
// S.top +=1;//再改指针
|
||||
return true;
|
||||
}
|
||||
|
||||
//读取栈顶元素,栈0
|
||||
bool GetTop0(ShStack S, int &x) {
|
||||
if (S.top0 == -1)return false;
|
||||
x = S.data[S.top0];
|
||||
return true;
|
||||
}
|
||||
//栈1
|
||||
bool GetTop1(ShStack S, int &x) {
|
||||
if (S.top1 == MaxSize)return false;
|
||||
x = S.data[S.top1];
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//打印整个栈,栈0
|
||||
void PrintStack0(ShStack S){
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top0>-1){//注意判空的条件
|
||||
printf("S[%d]=%d\n",S.top0,S.data[S.top0--]);
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
//打印整个栈
|
||||
void PrintStack1(ShStack S){
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top1<MaxSize){//注意判空的条件
|
||||
printf("S[%d]=%d\n",S.top1,S.data[S.top1++]);
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
|
||||
void testShStack() {
|
||||
printf("开始测试\n");
|
||||
ShStack S;
|
||||
InitStack(S);
|
||||
printf("测试第一个栈\n");
|
||||
if (Push0(S,1)){
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push0(S,2)){
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack0(S);
|
||||
int x;
|
||||
if (Pop0(S, x)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack0(S);
|
||||
int x1;
|
||||
if (GetTop0(S,x1)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x1);
|
||||
}else{
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
}
|
||||
|
||||
|
||||
printf("测试第一个栈\n");
|
||||
if (Push1(S,10)){
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push1(S,9)){
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack1(S);
|
||||
int x3;
|
||||
if (Pop1(S, x3)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x3);
|
||||
} else{
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack1(S);
|
||||
int x4;
|
||||
if (GetTop1(S,x4)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x4);
|
||||
}else{
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
}
|
||||
|
||||
|
||||
printf("测试完毕了!\n");
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
printf("Hello, ShStack!");
|
||||
testShStack();
|
||||
return 0;
|
||||
}
|
||||
159
DataStructure/DS_2_StackAndQueue/DS_2_2_LiStack.cpp
Normal file
159
DataStructure/DS_2_StackAndQueue/DS_2_2_LiStack.cpp
Normal file
@@ -0,0 +1,159 @@
|
||||
//
|
||||
// Created by kim on 2020/6/28.
|
||||
//
|
||||
|
||||
//链栈的实现
|
||||
#include <stdio.h>
|
||||
|
||||
# define MaxSize 10
|
||||
typedef struct {
|
||||
int data[MaxSize];
|
||||
int top0;
|
||||
int top1;
|
||||
} ShStack;
|
||||
//从结构体的定义就可以看出来,两个共享栈的根源就在于定义两个指针
|
||||
|
||||
//初始化
|
||||
void InitStack(ShStack &S) {
|
||||
S.top0 = -1;//这种初始化的方式,栈顶指针始终指向栈顶元素
|
||||
S.top1 =MaxSize;//这里的MaxSize就是所谓的第二个栈的栈底
|
||||
//可以根据顺序栈的第二种初试化方式,思考一下这种共享顺序栈的第二种初始化方式
|
||||
//S.top0=0
|
||||
//S.top1=MaxSize-1
|
||||
}
|
||||
|
||||
|
||||
//入栈0
|
||||
bool Push0(ShStack &S, int t) {
|
||||
if (S.top0 +1== S.top1)return false;//注意共享栈满的条件
|
||||
S.data[++S.top0] = t;//仔细品味一下这个++S.top
|
||||
return true;
|
||||
}
|
||||
//入栈1
|
||||
bool Push1(ShStack &S, int t) {
|
||||
if (S.top0 +1== S.top1)return false;//注意共享栈满的条件
|
||||
S.data[--S.top1] = t;//仔细品味一下这个--S.top,想想为什么?
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//出栈,并打印出栈顶元素
|
||||
bool Pop0(ShStack &S, int &x) {
|
||||
//判断
|
||||
if (S.top0 == -1)return false;//栈空报错
|
||||
x = S.data[S.top0--];
|
||||
// 等价于下面
|
||||
// x=S.data[S.top];//先取元素
|
||||
// S.top -=1;//再改指针
|
||||
return true;
|
||||
}
|
||||
//出栈1
|
||||
bool Pop1(ShStack &S, int &x) {
|
||||
//判断
|
||||
if (S.top1 == MaxSize)return false;//注意一下它的栈空报错条件
|
||||
x = S.data[S.top1++];//注意这个栈修改指针是++
|
||||
// 等价于下面
|
||||
// x=S.data[S.top];//先取元素
|
||||
// S.top +=1;//再改指针
|
||||
return true;
|
||||
}
|
||||
|
||||
//读取栈顶元素,栈0
|
||||
bool GetTop0(ShStack S, int &x) {
|
||||
if (S.top0 == -1)return false;
|
||||
x = S.data[S.top0];
|
||||
return true;
|
||||
}
|
||||
//栈1
|
||||
bool GetTop1(ShStack S, int &x) {
|
||||
if (S.top1 == MaxSize)return false;
|
||||
x = S.data[S.top1];
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//打印整个栈,栈0
|
||||
void PrintStack0(ShStack S){
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top0>-1){//注意判空的条件
|
||||
printf("S[%d]=%d\n",S.top0,S.data[S.top0--]);
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
//打印整个栈
|
||||
void PrintStack1(ShStack S){
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top1<MaxSize){//注意判空的条件
|
||||
printf("S[%d]=%d\n",S.top1,S.data[S.top1++]);
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
|
||||
void testShStack() {
|
||||
printf("开始测试\n");
|
||||
ShStack S;
|
||||
InitStack(S);
|
||||
printf("测试第一个栈\n");
|
||||
if (Push0(S,1)){
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push0(S,2)){
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack0(S);
|
||||
int x;
|
||||
if (Pop0(S, x)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack0(S);
|
||||
int x1;
|
||||
if (GetTop0(S,x1)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x1);
|
||||
}else{
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
}
|
||||
|
||||
|
||||
printf("测试第一个栈\n");
|
||||
if (Push1(S,10)){
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push1(S,9)){
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack1(S);
|
||||
int x3;
|
||||
if (Pop1(S, x3)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x3);
|
||||
} else{
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack1(S);
|
||||
int x4;
|
||||
if (GetTop1(S,x4)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x4);
|
||||
}else{
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
}
|
||||
|
||||
|
||||
printf("测试完毕了!\n");
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
printf("Hello, ShStack!");
|
||||
testShStack();
|
||||
return 0;
|
||||
}
|
||||
24
DataStructure/DS_2_StackAndQueue/Doc_2_0_栈.md
Normal file
24
DataStructure/DS_2_StackAndQueue/Doc_2_0_栈.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# 栈
|
||||
|
||||

|
||||
|
||||
## 栈的定义
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
## 栈的基本操作
|
||||
|
||||

|
||||
|
||||
### 出栈顺序
|
||||
|
||||

|
||||
|
||||
## 总结
|
||||
|
||||

|
||||
|
||||
41
DataStructure/DS_2_StackAndQueue/Doc_2_1_顺序栈.md
Normal file
41
DataStructure/DS_2_StackAndQueue/Doc_2_1_顺序栈.md
Normal file
@@ -0,0 +1,41 @@
|
||||
# 顺序栈
|
||||
|
||||

|
||||
|
||||
## 顺序栈的定义
|
||||
|
||||

|
||||
|
||||
## 基本操作
|
||||
|
||||
### 初始化操作
|
||||
|
||||

|
||||
|
||||
### 进栈操作
|
||||
|
||||

|
||||
|
||||
### 出栈操作
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
### 读取栈顶元素
|
||||
|
||||

|
||||
|
||||
## 另一种基本操作的实现方式:
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
## 共享栈
|
||||
|
||||

|
||||
|
||||
## 总结
|
||||
|
||||

|
||||
18
DataStructure/DS_2_StackAndQueue/Doc_2_2_链栈.md
Normal file
18
DataStructure/DS_2_StackAndQueue/Doc_2_2_链栈.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# 链栈
|
||||
|
||||

|
||||
|
||||
## 复习单链表
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
## 链栈的定义
|
||||
|
||||

|
||||
|
||||
## 总结
|
||||
|
||||

|
||||
|
||||
19
DataStructure/DS_2_StackAndQueue/Doc_2_3_队列.md
Normal file
19
DataStructure/DS_2_StackAndQueue/Doc_2_3_队列.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# 队列
|
||||
|
||||

|
||||
|
||||
## 队列的定义
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
## 队列的基本操作
|
||||
|
||||

|
||||
|
||||
## 总结
|
||||
|
||||

|
||||
53
DataStructure/DS_2_StackAndQueue/Doc_2_4_顺序队列.md
Normal file
53
DataStructure/DS_2_StackAndQueue/Doc_2_4_顺序队列.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# 顺序队列
|
||||
|
||||

|
||||
|
||||
## 顺序队列的定义
|
||||
|
||||

|
||||
|
||||
### 顺序队列的初始化
|
||||
|
||||

|
||||
|
||||
### 入队操作
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
### 循环队列
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
### 出队
|
||||
|
||||

|
||||
|
||||
### 判空/判满
|
||||
|
||||
方案一:牺牲一个存储空间
|
||||
|
||||

|
||||
|
||||
方案二:利用size变量记录队列长度
|
||||
|
||||

|
||||
|
||||
方案三:利用tag记录最后一次操作
|
||||
|
||||

|
||||
|
||||
## 其它实现方式
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
## 总结
|
||||
|
||||

|
||||
44
DataStructure/DS_2_StackAndQueue/Doc_2_5_链式队列.md
Normal file
44
DataStructure/DS_2_StackAndQueue/Doc_2_5_链式队列.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# 链队列
|
||||
|
||||

|
||||
|
||||
## 链队列的定义
|
||||
|
||||

|
||||
|
||||
## 链队列的基本操作
|
||||
|
||||
### 链队列的初始化
|
||||
|
||||

|
||||
|
||||
### 入队
|
||||
|
||||
带头节点
|
||||
|
||||

|
||||
|
||||
不带头节点
|
||||
|
||||

|
||||
|
||||
### 出队
|
||||
|
||||
带有头节点
|
||||
|
||||

|
||||
|
||||
不带头节点
|
||||
|
||||

|
||||
|
||||
### 判满
|
||||
|
||||

|
||||
|
||||
## 总结
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
34
DataStructure/DS_2_StackAndQueue/Doc_2_6_双端队列.md
Normal file
34
DataStructure/DS_2_StackAndQueue/Doc_2_6_双端队列.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# 双端队列
|
||||
|
||||

|
||||
|
||||
## 双端队列的定义
|
||||
|
||||

|
||||
|
||||
## 考点:判断输出序列的合法性
|
||||
|
||||
### 对于栈的输出序列的合法性
|
||||
|
||||

|
||||
|
||||
绿色为合法,红色为非法
|
||||
|
||||
### 对于输入受限的双端队列的输出序列的合法性
|
||||
|
||||

|
||||
|
||||
绿色为合法,红色为非法,带下划线是在栈中不合法,但在输入受限的双端队列中合法的。
|
||||
|
||||
### 对于输出受限的双端队列
|
||||
|
||||

|
||||
|
||||
绿色为合法,红色为非法,带下划线是在栈中不合法,但在输出受限的双端队列中合法
|
||||
|
||||
这种输出受限的双端队列,看序号较大的元素输出的位置,这意味着,在它输出之前,比它小的元素的相对位置是确定的,接下来就是考虑有什么插入的方法能实现这种相对位置关系!
|
||||
|
||||
## 总结
|
||||
|
||||
## 
|
||||
|
||||
148
DataStructure/DS_2_StackAndQueue/Doc_2_7_栈的应用.md
Normal file
148
DataStructure/DS_2_StackAndQueue/Doc_2_7_栈的应用.md
Normal file
@@ -0,0 +1,148 @@
|
||||
# 栈的应用
|
||||
|
||||
## 括号匹配问题
|
||||
|
||||

|
||||
|
||||
### 实际过程
|
||||
|
||||

|
||||
|
||||
####正好匹配
|
||||
|
||||

|
||||
|
||||
####左右不匹配
|
||||
|
||||

|
||||
|
||||
#### 右括号单身
|
||||
|
||||

|
||||
|
||||
#### 左括号单身
|
||||
|
||||

|
||||
|
||||
#### 整个流程
|
||||
|
||||

|
||||
|
||||
### 算法实现
|
||||
|
||||

|
||||
|
||||
### 总结
|
||||
|
||||

|
||||
|
||||
## 表达式求值
|
||||
|
||||

|
||||
|
||||
### 算数表达式是什么?
|
||||
|
||||
由三个部分组成(操作数,运算符,界限符)
|
||||
|
||||

|
||||
|
||||
### 前/后缀表达式的诞生
|
||||
|
||||

|
||||
|
||||
### 中/后/前缀表达式的区别
|
||||
|
||||

|
||||
|
||||
#### 中转后的过程:
|
||||
|
||||

|
||||
|
||||
**上图中,后缀表达式的算术符的先后次序对应中缀表达式的生效的先后次序**,但是这是一定的吗?
|
||||
|
||||

|
||||
|
||||
左优先原则,可保证运算顺序唯一性,以确定机算算法输出结果的唯一性!!
|
||||
|
||||

|
||||
|
||||
#### 机算算法实现
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
### 中转前的过程
|
||||
|
||||

|
||||
|
||||
中转后和中转前的区别:
|
||||
|
||||

|
||||
|
||||
#### 中转前的机算过程:
|
||||
|
||||

|
||||
|
||||
### 总结
|
||||
|
||||

|
||||
|
||||
"左优先"/“右优先”原则和左/右操作数不是专业说法,仅供理解!
|
||||
|
||||
## 表达式求值——具体代码实现
|
||||
|
||||

|
||||
|
||||
### 中转后机算
|
||||
|
||||
手算过程:
|
||||
|
||||

|
||||
|
||||
机算过程:
|
||||
|
||||

|
||||
|
||||
### 中缀表达式的计算
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
CPU只能执行单个的加减乘除运算,上边这么搞的意义就是为了将高级程序语言编译成简单的机器码,让CPU去执行!
|
||||
|
||||
### 总结
|
||||
|
||||

|
||||
|
||||
## 栈在递归中的应用
|
||||
|
||||
递归的过程就是函数调用的过程
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
### 适合用“递归”算法解决的问题
|
||||
|
||||

|
||||
|
||||
求阶乘:
|
||||
|
||||

|
||||
|
||||
**使用递归时,需要注意调用栈溢出!**
|
||||
|
||||

|
||||
|
||||
**可以自定义栈将递归算法改造成非递归算法!**
|
||||
|
||||
求斐波那契数列
|
||||
|
||||

|
||||
|
||||
### 总结
|
||||
|
||||

|
||||
|
||||
26
DataStructure/DS_2_StackAndQueue/Doc_2_8_队列的应用.md
Normal file
26
DataStructure/DS_2_StackAndQueue/Doc_2_8_队列的应用.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# 队列的应用
|
||||
|
||||
## 树的层次遍历
|
||||
|
||||
提示:不知道树这种数据结构的同学,可以考虑先跳过本文内容,等了解树之后,再来康康~
|
||||
|
||||

|
||||
|
||||
详细实现内容在树的章节实现!
|
||||
|
||||
## 图的广度优先遍历
|
||||
|
||||
遍历是指找出所有元素节点
|
||||
|
||||

|
||||
|
||||
## 队列在操作系统中的应用
|
||||
|
||||
**CPU资源分配问题**
|
||||
|
||||

|
||||
|
||||
**打印数据缓冲区**
|
||||
|
||||

|
||||
|
||||
14
DataStructure/DS_2_StackAndQueue/README.md
Normal file
14
DataStructure/DS_2_StackAndQueue/README.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# 栈和队列
|
||||
|
||||
## 笔记目录
|
||||
|
||||
* [栈](Doc_2_0_栈.md)
|
||||
* [顺序栈](Doc_2_1_顺序栈.md)
|
||||
* [链栈](Doc_2_2_链栈.md)
|
||||
* [队列](Doc_2_3_队列.md)
|
||||
* [顺序队列](Doc_2_4_顺序队列.md)
|
||||
* [链式队列](Doc_2_5_链式队列.md)
|
||||
* [双端队列](Doc_2_6_双端队列.md)
|
||||
* [栈的应用](Doc_2_7_栈的应用.md)
|
||||
* [队列的应用](Doc_2_8_队列的应用.md)
|
||||
|
||||
Reference in New Issue
Block a user