mirror of
https://github.com/CodePanda66/CSPostgraduate-408.git
synced 2023-05-21 21:49:33 +08:00
✨ Add LinkStack
This commit is contained in:
@@ -5,7 +5,7 @@ set(CMAKE_CXX_STANDARD 14)
|
||||
|
||||
# 遍历项目根目录下所有的 .cpp 文件
|
||||
# 排除掉leetcode 下的代码,其不需要在Clion 中编译运行!
|
||||
file (GLOB_RECURSE files ./*.cpp ./*/*.c ./*/*/*.cpp)
|
||||
file (GLOB_RECURSE files ./*.cpp ./*/*.cpp ./*/*/*.cpp)
|
||||
foreach (file ${files})
|
||||
string(REGEX REPLACE ".+/(.+)\\..*" "\\1" exe ${file})
|
||||
add_executable (${exe} ${file})
|
||||
|
||||
@@ -122,7 +122,7 @@ void testShStack() {
|
||||
}
|
||||
|
||||
|
||||
printf("测试第一个栈\n");
|
||||
printf("测试第二个栈\n");
|
||||
if (Push1(S,10)){
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
|
||||
@@ -4,156 +4,120 @@
|
||||
|
||||
//链栈的实现
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
# define MaxSize 10
|
||||
typedef struct {
|
||||
int data[MaxSize];
|
||||
int top0;
|
||||
int top1;
|
||||
} ShStack;
|
||||
typedef struct LinkNode{
|
||||
int data;
|
||||
struct LinkNode *next;
|
||||
} *LinkStack;
|
||||
//从结构体的定义就可以看出来,两个共享栈的根源就在于定义两个指针
|
||||
|
||||
//初始化
|
||||
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,想想为什么?
|
||||
bool InitStack(LinkStack &LS) {
|
||||
LS=(LinkNode *)malloc(sizeof(LinkNode));//分配一个头节点
|
||||
if(LS==NULL){
|
||||
return false;
|
||||
}
|
||||
LS->next=NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
//入栈 参考头插法建立单链表
|
||||
bool Push(LinkStack &LS, int t) {
|
||||
//入站不需要检查
|
||||
LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
|
||||
if (s==NULL)return false;
|
||||
s->data=t;
|
||||
s->next=LS->next;
|
||||
LS->next=s;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//出栈,并打印出栈顶元素
|
||||
bool Pop0(ShStack &S, int &x) {
|
||||
bool Pop(LinkStack &LS, 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];
|
||||
if (LS->next==NULL)return false;//栈空,这里的条件
|
||||
LinkNode *q ;
|
||||
q=LS->next;
|
||||
LS->next=q->next;
|
||||
x=q->data;
|
||||
free(q);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//打印整个栈,栈0
|
||||
void PrintStack0(ShStack S){
|
||||
//读取栈顶元素,栈
|
||||
bool GetTop(LinkStack LS, int &x) {
|
||||
if (LS==NULL)return false;
|
||||
x=LS->next->data;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//打印整个栈,栈
|
||||
void PrintStack(LinkStack LS){
|
||||
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++]);
|
||||
int i=0;int x;
|
||||
LinkNode *p=LS->next;
|
||||
while (p!=NULL){//注意判空的条件
|
||||
printf("S[%d]=%d\n",i,p->data);
|
||||
p=p->next;
|
||||
i++;
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
|
||||
void testShStack() {
|
||||
void testLinkStack() {
|
||||
printf("开始测试\n");
|
||||
ShStack S;
|
||||
LinkStack S;
|
||||
InitStack(S);
|
||||
printf("测试第一个栈\n");
|
||||
if (Push0(S,1)){
|
||||
if (Push(S,1)){
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push0(S,2)){
|
||||
if (Push(S,2)){
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack0(S);
|
||||
PrintStack(S);
|
||||
int x;
|
||||
if (Pop0(S, x)){
|
||||
if (Pop(S, x)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack0(S);
|
||||
|
||||
PrintStack(S);
|
||||
int x1;
|
||||
if (GetTop0(S,x1)){
|
||||
if (GetTop(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);
|
||||
if (Pop(S, x)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack1(S);
|
||||
int x4;
|
||||
if (GetTop1(S,x4)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x4);
|
||||
}else{
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
if (Pop(S, x)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
|
||||
|
||||
printf("测试完毕了!\n");
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
printf("Hello, ShStack!");
|
||||
testShStack();
|
||||
printf("Hello, LinkNode!");
|
||||
testLinkStack();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user