From f8d4e3674662d07632e799613a193d2162fd9337 Mon Sep 17 00:00:00 2001 From: Kim Yang Date: Sun, 16 Aug 2020 14:15:35 +0800 Subject: [PATCH] :truck: add func define --- .../DS_2_StackAndQueue/DS_2_0_SqStack.cpp | 12 +++++++++ .../DS_2_StackAndQueue/DS_2_1_ShStack.cpp | 9 +++++++ .../DS_2_StackAndQueue/DS_2_2_LiStack.cpp | 16 ++++++------ .../DS_2_StackAndQueue/DS_2_3_SqQueue0.cpp | 16 ++++++++---- .../DS_2_StackAndQueue/DS_2_4_SqQueue1.cpp | 12 +++++---- .../DS_2_StackAndQueue/DS_2_5_SqQueue2.cpp | 25 +++++++++++-------- .../DS_2_StackAndQueue/DS_2_6_LiQueue0.cpp | 12 ++++++--- .../DS_2_StackAndQueue/DS_2_7_LiQueue1.cpp | 17 ++++++------- .../DS_2_8_QueueApplication.cpp | 25 +++++++++++-------- 9 files changed, 93 insertions(+), 51 deletions(-) diff --git a/DataStructure/DS_2_StackAndQueue/DS_2_0_SqStack.cpp b/DataStructure/DS_2_StackAndQueue/DS_2_0_SqStack.cpp index 6e134e4..7e9b7ec 100644 --- a/DataStructure/DS_2_StackAndQueue/DS_2_0_SqStack.cpp +++ b/DataStructure/DS_2_StackAndQueue/DS_2_0_SqStack.cpp @@ -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);//读取栈顶元素的第二种实现方式 /**定义模块**/ /**实现模块**/ diff --git a/DataStructure/DS_2_StackAndQueue/DS_2_1_ShStack.cpp b/DataStructure/DS_2_StackAndQueue/DS_2_1_ShStack.cpp index 3f05f26..0353fc4 100644 --- a/DataStructure/DS_2_StackAndQueue/DS_2_1_ShStack.cpp +++ b/DataStructure/DS_2_StackAndQueue/DS_2_1_ShStack.cpp @@ -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 /**定义模块**/ /**实现模块**/ diff --git a/DataStructure/DS_2_StackAndQueue/DS_2_2_LiStack.cpp b/DataStructure/DS_2_StackAndQueue/DS_2_2_LiStack.cpp index 423389d..9f35271 100644 --- a/DataStructure/DS_2_StackAndQueue/DS_2_2_LiStack.cpp +++ b/DataStructure/DS_2_StackAndQueue/DS_2_2_LiStack.cpp @@ -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) { @@ -27,7 +33,6 @@ bool InitStack(LinkStack &LS) { return true; } -//入栈 参考头插法建立单链表 bool Push(LinkStack &LS, int t) { //入站不需要检查 LinkNode *s = (LinkNode *) malloc(sizeof(LinkNode)); @@ -38,8 +43,6 @@ bool Push(LinkStack &LS, int t) { return true; } - -//出栈,并打印出栈顶元素 bool Pop(LinkStack &LS, int &x) { //判断 if (LS->next == NULL)return false;//栈空,这里的条件 @@ -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; } + /**实现模块**/ /**测试模块**/ diff --git a/DataStructure/DS_2_StackAndQueue/DS_2_3_SqQueue0.cpp b/DataStructure/DS_2_StackAndQueue/DS_2_3_SqQueue0.cpp index d2a43a1..c075e83 100644 --- a/DataStructure/DS_2_StackAndQueue/DS_2_3_SqQueue0.cpp +++ b/DataStructure/DS_2_StackAndQueue/DS_2_3_SqQueue0.cpp @@ -14,15 +14,23 @@ 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; @@ -30,7 +38,6 @@ bool QueueEmpty(SqQueue Q) { return true; } -//入队操作 bool EnQueue(SqQueue &Q, int t) { if ((Q.rear + 1) % MaxSize == Q.front)return false;//队满,注意这里的判满条件 //这里的判满条件会造成浪费一个存储空间的问题 @@ -39,7 +46,6 @@ 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]; @@ -47,12 +53,12 @@ bool DeQueue(SqQueue &Q, int &x) { return true; } -//获取队头元素,用x返回 bool GetHead(SqQueue Q, int &x) { if (Q.front == Q.rear)return false; x = Q.data[Q.front]; return true; } + /**实现模块**/ /**测试模块**/ diff --git a/DataStructure/DS_2_StackAndQueue/DS_2_4_SqQueue1.cpp b/DataStructure/DS_2_StackAndQueue/DS_2_4_SqQueue1.cpp index 12d522c..2ff5244 100644 --- a/DataStructure/DS_2_StackAndQueue/DS_2_4_SqQueue1.cpp +++ b/DataStructure/DS_2_StackAndQueue/DS_2_4_SqQueue1.cpp @@ -15,16 +15,21 @@ 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; @@ -32,7 +37,6 @@ bool QueueEmpty(SqQueue Q) { return true; } -//入队操作 bool EnQueue(SqQueue &Q, int t) { if (Q.size == MaxSize)return false;//队满,注意这里的判满条件 Q.data[Q.rear] = t; @@ -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]; @@ -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]; diff --git a/DataStructure/DS_2_StackAndQueue/DS_2_5_SqQueue2.cpp b/DataStructure/DS_2_StackAndQueue/DS_2_5_SqQueue2.cpp index db166cd..da5d0f7 100644 --- a/DataStructure/DS_2_StackAndQueue/DS_2_5_SqQueue2.cpp +++ b/DataStructure/DS_2_StackAndQueue/DS_2_5_SqQueue2.cpp @@ -15,25 +15,31 @@ 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;//通过取余操作让整个队列循环起来 @@ -41,16 +47,15 @@ bool EnQueue(SqQueue &Q,int t){ 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; diff --git a/DataStructure/DS_2_StackAndQueue/DS_2_6_LiQueue0.cpp b/DataStructure/DS_2_StackAndQueue/DS_2_6_LiQueue0.cpp index 6aec862..10c272a 100644 --- a/DataStructure/DS_2_StackAndQueue/DS_2_6_LiQueue0.cpp +++ b/DataStructure/DS_2_StackAndQueue/DS_2_6_LiQueue0.cpp @@ -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);//判空 /**定义模块**/ /**实现模块**/ @@ -26,7 +33,6 @@ void InitQueue(LinkQueue &Q) { Q.front->next = NULL; } -//入队操作 bool EnQueue(LinkQueue &Q, int x) { //判满?链式存储一般不需要判满,除非内存不足 LinkNode *s = (LinkNode *) malloc(sizeof(LinkNode)); @@ -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记录队头元素 @@ -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; } /**实现模块**/ diff --git a/DataStructure/DS_2_StackAndQueue/DS_2_7_LiQueue1.cpp b/DataStructure/DS_2_StackAndQueue/DS_2_7_LiQueue1.cpp index 2e951f9..f5952f1 100644 --- a/DataStructure/DS_2_StackAndQueue/DS_2_7_LiQueue1.cpp +++ b/DataStructure/DS_2_StackAndQueue/DS_2_7_LiQueue1.cpp @@ -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);//判空 /**定义模块**/ /**实现模块**/ @@ -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)); @@ -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记录队头元素 diff --git a/DataStructure/DS_2_StackAndQueue/DS_2_8_QueueApplication.cpp b/DataStructure/DS_2_StackAndQueue/DS_2_8_QueueApplication.cpp index 0152cd1..6eab01f 100644 --- a/DataStructure/DS_2_StackAndQueue/DS_2_8_QueueApplication.cpp +++ b/DataStructure/DS_2_StackAndQueue/DS_2_8_QueueApplication.cpp @@ -3,7 +3,7 @@ // Copyright (c) Kim Yang All rights reserved. // -//栈的应用 +//栈的应用——扩号匹配问题 #include @@ -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; @@ -32,7 +39,6 @@ bool Push(SqStack &S, char t) { return true; } -//出栈,并打印出栈顶元素 bool Pop(SqStack &S, char &x) { //判断 if (S.top == -1)return false;//栈空报错 @@ -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