Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
🚚 add func define
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim Yang committed Aug 16, 2020
1 parent f7b45ed commit f8d4e36
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 51 deletions.
12 changes: 12 additions & 0 deletions DataStructure/DS_2_StackAndQueue/DS_2_0_SqStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ typedef struct {
int top;
} SqStack;

//函数声明
void InitStack(SqStack &S);//初始化
bool Push(SqStack &S, int t);//入栈
bool Pop(SqStack &S, int &x);//出栈,并打印出栈顶元素
bool GetTop(SqStack S, int &x);//读取栈顶元素
int GetTopOther(SqStack S);//读取栈顶元素的第二种实现方式

void InitStack1(SqStack &S);//初始化1
bool Push1(SqStack &S, int t);//入栈,初始化1
bool Pop1(SqStack &S, int &x);//出栈,并打印出栈顶元素,初始化1
bool GetTop1(SqStack S, int &x);//读取栈顶元素,初始化1
int GetTopOther1(SqStack S);//读取栈顶元素的第二种实现方式
/**定义模块**/

/**实现模块**/
Expand Down
9 changes: 9 additions & 0 deletions DataStructure/DS_2_StackAndQueue/DS_2_1_ShStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ typedef struct {
int top1;
} ShStack;
//从结构体的定义就可以看出来,两个共享栈的根源就在于定义两个指针

//函数声明
void InitStack(ShStack &S);//初始化
bool Push0(ShStack &S, int t);//入栈0
bool Push1(ShStack &S, int t);//入栈1
bool Pop0(ShStack &S, int &x);//出栈,并打印出栈顶元素
bool Pop1(ShStack &S, int &x);//出栈1
bool GetTop0(ShStack S, int &x);//读取栈顶元素,栈0
bool GetTop1(ShStack S, int &x);//栈1
/**定义模块**/

/**实现模块**/
Expand Down
16 changes: 9 additions & 7 deletions DataStructure/DS_2_StackAndQueue/DS_2_2_LiStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ typedef struct LinkNode {
int data;
struct LinkNode *next;
} *LinkStack;
//从结构体的定义就可以看出来,两个共享栈的根源就在于定义两个指针

//函数声明
bool InitStack(LinkStack &LS);//初始化
bool Push(LinkStack &LS, int t);//入栈 参考头插法建立单链表
bool Pop(LinkStack &LS, int &x);//出栈,并打印出栈顶元素
bool GetTop(LinkStack LS, int &x);//读取栈顶元素,栈

/**定义模块**/

/**实现模块**/
//初始化

bool InitStack(LinkStack &LS) {
LS = (LinkNode *) malloc(sizeof(LinkNode));//分配一个头节点
if (LS == NULL) {
Expand All @@ -27,7 +33,6 @@ bool InitStack(LinkStack &LS) {
return true;
}

//入栈 参考头插法建立单链表
bool Push(LinkStack &LS, int t) {
//入站不需要检查
LinkNode *s = (LinkNode *) malloc(sizeof(LinkNode));
Expand All @@ -38,8 +43,6 @@ bool Push(LinkStack &LS, int t) {
return true;
}


//出栈,并打印出栈顶元素
bool Pop(LinkStack &LS, int &x) {
//判断
if (LS->next == NULL)return false;//栈空,这里的条件
Expand All @@ -51,13 +54,12 @@ bool Pop(LinkStack &LS, int &x) {
return true;
}


//读取栈顶元素,栈
bool GetTop(LinkStack LS, int &x) {
if (LS == NULL)return false;
x = LS->next->data;
return true;
}

/**实现模块**/

/**测试模块**/
Expand Down
16 changes: 11 additions & 5 deletions DataStructure/DS_2_StackAndQueue/DS_2_3_SqQueue0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,30 @@ typedef struct {
int data[MaxSize];//
int front, rear;//对头指针和队尾指针
} SqQueue;

//函数声明

void InitQueue(SqQueue &Q);//初始化
bool QueueEmpty(SqQueue Q);//判空
bool EnQueue(SqQueue &Q, int t);//入队操作
bool DeQueue(SqQueue &Q, int &x);//出队操作
bool GetHead(SqQueue Q, int &x);//获取队头元素,用x返回

/**定义模块**/

/**实现模块**/
//初始化

void InitQueue(SqQueue &Q) {
Q.rear = Q.front = 0;//初始化时,队头队尾都指向0
}

//判空
bool QueueEmpty(SqQueue Q) {
if (Q.front == Q.rear)
return true;
else
return true;
}

//入队操作
bool EnQueue(SqQueue &Q, int t) {
if ((Q.rear + 1) % MaxSize == Q.front)return false;//队满,注意这里的判满条件
//这里的判满条件会造成浪费一个存储空间的问题
Expand All @@ -39,20 +46,19 @@ bool EnQueue(SqQueue &Q, int t) {
return true;
}

//出队操作
bool DeQueue(SqQueue &Q, int &x) {
if (Q.rear == Q.front)return false;//队空
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}

//获取队头元素,用x返回
bool GetHead(SqQueue Q, int &x) {
if (Q.front == Q.rear)return false;
x = Q.data[Q.front];
return true;
}

/**实现模块**/

/**测试模块**/
Expand Down
12 changes: 7 additions & 5 deletions DataStructure/DS_2_StackAndQueue/DS_2_4_SqQueue1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@ typedef struct {
int front, rear;//对头指针和队尾指针
int size;//利用size变量记录队列长度,并用作判满的条件!有了size就不会浪费一个存储空间
} SqQueue;

//函数声明
void InitQueue(SqQueue &Q);//初始化
bool QueueEmpty(SqQueue Q);//判空
bool EnQueue(SqQueue &Q, int t);//入队操作
bool DeQueue(SqQueue &Q, int &x);//出队操作
bool GetHead(SqQueue Q, int &x);//获取队头元素,用x返回
/**定义模块**/

/**实现模块**/
//初始化
void InitQueue(SqQueue &Q) {
Q.rear = Q.front = 0;//初始化时,队头队尾都指向0
Q.size = 0;//初试长度
}

//判空
bool QueueEmpty(SqQueue Q) {
if (Q.size == 0)//有了size,条件不一样了
return true;
else
return true;
}

//入队操作
bool EnQueue(SqQueue &Q, int t) {
if (Q.size == MaxSize)return false;//队满,注意这里的判满条件
Q.data[Q.rear] = t;
Expand All @@ -41,7 +45,6 @@ bool EnQueue(SqQueue &Q, int t) {
return true;
}

//出队操作
bool DeQueue(SqQueue &Q, int &x) {
if (Q.size == 0)return false;//队空
x = Q.data[Q.front];
Expand All @@ -50,7 +53,6 @@ bool DeQueue(SqQueue &Q, int &x) {
return true;
}

//获取队头元素,用x返回
bool GetHead(SqQueue Q, int &x) {
if (Q.size == 0)return false;
x = Q.data[Q.front];
Expand Down
25 changes: 15 additions & 10 deletions DataStructure/DS_2_StackAndQueue/DS_2_5_SqQueue2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,47 @@ typedef struct {
int front,rear;//对头指针和队尾指针
int tag;//利用tag变量记录最后一次操作是什么,0为删除,1为插入,并用作判满的条件!有了tag就不会浪费一个存储空间
}SqQueue;

//函数声明
void InitQueue(SqQueue &Q);//初始化
bool QueueEmpty(SqQueue Q);//判空
bool EnQueue(SqQueue &Q,int t);//入队操作
bool DeQueue(SqQueue &Q,int &x);//出队操作
bool GetHead(SqQueue Q,int &x);//获取队头元素,用x返回
/**定义模块**/

/**实现模块**/
//初始化
void InitQueue(SqQueue &Q){


void InitQueue(SqQueue &Q) {
Q.rear=Q.front=0;//初始化时,队头队尾都指向0
Q.tag=0;//初始化最后一次的操作状态
}

//判空
bool QueueEmpty(SqQueue Q){
bool QueueEmpty(SqQueue Q) {
if(Q.front==Q.rear&&Q.tag==0)//有了tag,条件不一样了
return true;
else
return true;
}

//入队操作
bool EnQueue(SqQueue &Q,int t){
bool EnQueue(SqQueue &Q, int t) {
if(Q.front==Q.rear&&Q.tag==1)return false;//队满,注意这里的判满条件
Q.data[Q.rear]=t;
Q.rear=(Q.rear+1)%MaxSize;//通过取余操作让整个队列循环起来
Q.tag=1;
return true;
}

//出队操作
bool DeQueue(SqQueue &Q,int &x){
bool DeQueue(SqQueue &Q, int &x) {
if(Q.rear==Q.front&&Q.tag==0)return false;//队空
x=Q.data[Q.front];
Q.front=(Q.front+1)%MaxSize;
Q.tag==0;
return true;
}
//获取队头元素,用x返回
bool GetHead(SqQueue Q,int &x){

bool GetHead(SqQueue Q, int &x) {
if (Q.rear==Q.front&&Q.tag==0)return false;
x=Q.data[Q.front];
return true;
Expand Down
12 changes: 9 additions & 3 deletions DataStructure/DS_2_StackAndQueue/DS_2_6_LiQueue0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ typedef struct LinkNode {
typedef struct {
LinkNode *front, *rear;
} LinkQueue;

//函数声明
void InitQueue(LinkQueue &Q);//初始化
bool EnQueue(LinkQueue &Q, int x);//入队操作
bool DeQueue(LinkQueue &Q, int &x);//出队
bool GetHead(LinkQueue Q, int &x);//获取头元素
bool QueueEmpty(LinkQueue Q);//判空
/**定义模块**/

/**实现模块**/
Expand All @@ -26,7 +33,6 @@ void InitQueue(LinkQueue &Q) {
Q.front->next = NULL;
}

//入队操作
bool EnQueue(LinkQueue &Q, int x) {
//判满?链式存储一般不需要判满,除非内存不足
LinkNode *s = (LinkNode *) malloc(sizeof(LinkNode));
Expand All @@ -38,7 +44,6 @@ bool EnQueue(LinkQueue &Q, int x) {
return true;
}

//出队
bool DeQueue(LinkQueue &Q, int &x) {
if (Q.front == Q.rear)return false;//队空
LinkNode *p = Q.front->next;//用指针p记录队头元素
Expand All @@ -55,7 +60,8 @@ bool GetHead(LinkQueue Q, int &x) {
x = Q.front->next->data;//用x变量返回队头元素
return true;
}
bool QueueEmpty(LinkQueue Q){

bool QueueEmpty(LinkQueue Q) {
return Q.front==Q.rear? true: false;
}
/**实现模块**/
Expand Down
17 changes: 7 additions & 10 deletions DataStructure/DS_2_StackAndQueue/DS_2_7_LiQueue1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ typedef struct LinkNode {
typedef struct {
LinkNode *front, *rear;
} LinkQueue;

//函数声明
void InitQueue(LinkQueue &Q);//初始化
bool EnQueue(LinkQueue &Q, int x);//入队操作
bool DeQueue(LinkQueue &Q, int &x);//出队
bool GetHead(LinkQueue Q, int &x);//获取队头元素
bool QueueEmpty(LinkQueue Q);//判空
/**定义模块**/

/**实现模块**/
Expand All @@ -26,15 +33,6 @@ void InitQueue(LinkQueue &Q) {
//不带头点,初始化时,front 、rear 指向NULL
}

//判空
bool IsEmpty(LinkQueue Q) {
if (Q.front == Q.rear)
return true;
else
return false;
}

//入队操作
bool EnQueue(LinkQueue &Q, int x) {
//判满?链式存储一般不需要判满,除非内存不足
LinkNode *s = (LinkNode *) malloc(sizeof(LinkNode));
Expand All @@ -52,7 +50,6 @@ bool EnQueue(LinkQueue &Q, int x) {
return true;
}

//出队
bool DeQueue(LinkQueue &Q, int &x) {
if (Q.front == NULL&&Q.rear==NULL)return false;//队空
LinkNode *p = Q.front;//用指针p记录队头元素
Expand Down
25 changes: 14 additions & 11 deletions DataStructure/DS_2_StackAndQueue/DS_2_8_QueueApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright (c) Kim Yang All rights reserved.
//

//栈的应用
//栈的应用——扩号匹配问题

#include <stdio.h>

Expand All @@ -13,16 +13,23 @@ typedef struct {
char data[MaxSize];
int top;
} SqStack;

//函数声明
//以下都是基础操作定义以及实现的方式和前面一样
void InitStack(SqStack &S);//初始化
bool Push(SqStack &S, char t);//入栈
bool Pop(SqStack &S, char &x);//出栈,并打印出栈顶元素
bool StackEmpty (SqStack S);//判栈空
//以上都是基础操作定义以及实现的方式和前面一样
//括号匹配问题
bool bracketCheck(char str[],int length);
/**定义模块**/

/**实现模块**/
/**以下都是基础操作定义以及实现的方式和前面一样**/
//初始化
void InitStack(SqStack &S) {
S.top = -1;//这种初始化的方式,栈顶指针始终指向栈顶元素
}

//入栈
bool Push(SqStack &S, char t) {
if (S.top == MaxSize - 1)return false;//栈满
S.data[++S.top] = t;
Expand All @@ -32,7 +39,6 @@ bool Push(SqStack &S, char t) {
return true;
}

//出栈,并打印出栈顶元素
bool Pop(SqStack &S, char &x) {
//判断
if (S.top == -1)return false;//栈空报错
Expand All @@ -43,15 +49,11 @@ bool Pop(SqStack &S, char &x) {
return true;
}


//读取栈顶元素
bool StackEmpty (SqStack S) {
bool StackEmpty(SqStack S) {
return S.top==-1;
}


/**以上都是基础操作定义以及实现的方式和前面一样**/
bool bracketCheck(char str[],int length){
bool bracketCheck(char *str, int length) {
SqStack S;
InitStack(S);
for (int i = 0; i <length ; i++) {
Expand All @@ -72,6 +74,7 @@ bool bracketCheck(char str[],int length){
}
return StackEmpty(S);//最后检查栈,若空匹配成功,非空匹配失败
}

/**实现模块**/

/**测试模块**/
Expand Down

0 comments on commit f8d4e36

Please sign in to comment.