数据结构之栈
参考:数据结构(C语言版)-严蔚敏
#include <stdio.h> #include <stdlib.h> ///////////////// 函数结果状态代码 ///////////// #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; ///////////////////////////////////////////////// #define STACK_INIT_SIZE 5 #define STACKINCREMENT 10 typedef int SElemType;//定义元素类型 typedef struct{ SElemType* base; SElemType* top; int stacksize; }SqStack; Status SqStack_InitStack(SqStack& s); Status SqStack_DestroyStack(SqStack& s); Status SqStack_ClearStack(SqStack& s); Status SqStack_IsEmpty(SqStack s);//若栈s是空,返回TRUE,否则返回FALSE int SqStack_GetLength(SqStack s); Status SqStack_GetTop(SqStack s, SElemType& e); Status SqStack_Push(SqStack& s, SElemType e); Status SqStack_Pop(SqStack& s, SElemType* pe); Status SqStack_Traverse(SqStack s, Status (*visit)()); Status SqStack_Display(SqStack s); Status SqStack_InitStack(SqStack& s) { s.base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); if (NULL == s.base) return ERROR; s.top = s.base; s.stacksize = STACK_INIT_SIZE; return OK; } Status SqStack_GetTop(SqStack s, SElemType& e) { if (s.base == s.top) return ERROR; e = *(s.top - 1); return OK; } Status SqStack_Push(SqStack& s, SElemType e) { if (s.top - s.base >= s.stacksize) {//栈满,追加存储空间 s.base = (SElemType*)realloc(s.base, (s.stacksize + STACKINCREMENT)*sizeof(SElemType)); if (!s.base) return ERROR; s.top = s.base + s.stacksize; s.stacksize += STACKINCREMENT; } *s.top = e; s.top++; return OK; } Status SqStack_Pop(SqStack& s, SElemType* pe) { if (s.top == s.base) return ERROR; --s.top; if (nullptr != pe) *pe = *(s.top); return OK; } Status SqStack_IsEmpty(SqStack s) { return s.base == s.top ? TRUE : FALSE; } int SqStack_GetLength(SqStack s) { return s.top - s.base; } Status SqStack_Traverse(SqStack s, Status(*visit)()) { if (s.top == s.base) return ERROR; for (SElemType* p=s.base; p < s.top; p++) { visit(); } } Status SqStack_Display(SqStack s) { if (s.top == s.base) return ERROR; for (SElemType* p = s.base; p < s.top; p++) { printf("%d ", *p); } printf("\n"); return OK; } #endif
**
#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include "sqstack.h" //using namespace std; //将10进制 转 8进制 int conversion(int n) { int ret = 0; SqStack s; SqStack_InitStack(s); while (n) { SqStack_Push(s, n % 8); n = n / 8; } while (!SqStack_IsEmpty(s)) { SElemType e; SqStack_Pop(s, &e); int len = SqStack_GetLength(s); ret += (int)pow(10,len) * e; } return ret; } //括号的匹配 bool match_brackets(const char* str){ if(NULL == str || strlen(str)==0) return false; const char* k1 = "([{<"; const char* k2 = ")]}>"; SqStack s = { 0 }; SqStack_InitStack(s); int nLen = strlen(str); for(int i=0; i<nLen; ++i){ char c = str[i]; if(strchr(k1,c)){ SqStack_Push(s, c); } else if(strchr(k2,c)){ int elem; if(OK == SqStack_Pop(s, &elem)){ const int nPos = strchr(k1,elem) - k1; const int nPos2 = strchr(k2,c) - k2; if(nPos != nPos2){ return false; } }else{ return false; } } else{ //return false; } } if(TRUE == SqStack_IsEmpty(s)) return true; else return false; } int main() { // cout << "Hello World!" << endl; // for(int i=0;i<10;++i){ // for(int j=0; j<10; ++j){ // cout << maze[i][j] << ' '; // } // cout << '\n'; // } // SqStack s = { 0 }; // SqStack_InitStack(s); // SqStack_Push(s, 5); // SqStack_Push(s, 6); // SqStack_Push(s, 7); // SqStack_Pop(s, NULL); // SqStack_Push(s, 8); // SqStack_Push(s, 9); // SqStack_Push(s, 10); // SqStack_Push(s, 11); // SqStack_Push(s, 12); // SqStack_Display(s); // int res = conversion(8); // printf("%d\n", res); const char* s = "{(){}[0)][]}"; std::cout << (match_brackets(s)?"ok":"bad") << '\n'; return 0; }
迷宫求解
SqStack.h
#ifndef SQSTACK_H #define SQSTACK_H #include <stdio.h> #include <stdlib.h> ///////////////// 函数结果状态代码 ///////////// #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVER_FLOW -2 typedef int Status; ///////////////////////////////////////////////// //------------ 元素类型 begin -------------- typedef struct{ int i; int j; }PosType; //定义元素类型 typedef struct{ int ord; //通道块在路径上的"序号" PosType seat; //通道块在迷宫中的“坐标位置” int di; //从此通道块走向下一通道块的“方向” 东1南2西3北4 }SElemType; //------------ 元素类型 end -------------- //----------------------- 栈的顺序存储表示 -------------------- #define STACK_INIT_SIZE 5 #define STACKINCREMENT 10 typedef struct{ SElemType* base; SElemType* top; int stacksize; }SqStack; Status SqStack_InitStack(SqStack& s); Status SqStack_DestroyStack(SqStack& s); Status SqStack_ClearStack(SqStack& s); Status SqStack_IsEmpty(SqStack s);//若栈s是空,返回TRUE,否则返回FALSE int SqStack_GetLength(SqStack s); Status SqStack_GetTop(SqStack s, SElemType& e); Status SqStack_Push(SqStack& s, SElemType e); Status SqStack_Pop(SqStack& s, SElemType* pe); Status SqStack_Traverse(SqStack s, Status (*visit)(SElemType elem)); Status SqStack_Display(SqStack s); Status SqStack_InitStack(SqStack& s) { s.base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); if (NULL == s.base) return ERROR; s.top = s.base; s.stacksize = STACK_INIT_SIZE; return OK; } Status SqStack_GetTop(SqStack s, SElemType& e) { if (s.base == s.top) return ERROR; e = *(s.top - 1); return OK; } Status SqStack_Push(SqStack& s, SElemType e) { if (s.top - s.base >= s.stacksize) {//栈满,追加存储空间 s.base = (SElemType*)realloc(s.base, (s.stacksize + STACKINCREMENT)*sizeof(SElemType)); if (!s.base) return ERROR; s.top = s.base + s.stacksize; s.stacksize += STACKINCREMENT; } *s.top = e; s.top++; return OK; } Status SqStack_Pop(SqStack& s, SElemType* pe) { if (s.top == s.base) return ERROR; --s.top; if (NULL != pe) *pe = *(s.top); return OK; } Status SqStack_IsEmpty(SqStack s) { return s.base == s.top ? TRUE : FALSE; } int SqStack_GetLength(SqStack s) { return s.top - s.base; } Status SqStack_Traverse(SqStack s, Status(*visit)(SElemType elem)) { if (s.top == s.base) return ERROR; for (SElemType* p=s.base; p < s.top; p++) { visit(*p); } return OK; } //Status SqStack_Display(SqStack s) //{ // if (s.top == s.base) // return ERROR; // for (SElemType* p = s.base; p < s.top; p++) // { // printf("%d ", *p); // } // printf("\n"); // return OK; //} #endif // SQSTACK_H
main.cpp
#include <iostream> #include "sqstack.h" using std::cout; //#define ROW 10 //#define COL 10 const int ROW =10; const int COL =10; typedef int MazeType[ROW][COL]; MazeType maze = { {1,1,1,1,1,1,1,1,1,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,0,0,1,1,0,0,1}, {1,0,1,1,1,0,0,0,0,1}, {1,0,0,0,1,0,0,0,0,1}, {1,0,1,0,0,0,1,0,0,1}, {1,0,1,1,1,0,1,1,0,1}, {1,1,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1} }; Status displaySElemType(SElemType elem){ cout << "(" << elem.seat.i << ',' << elem.seat.j << ")\n"; return OK; } bool HasFootPrint(PosType p){ if(2 == maze[p.i][p.j]) return true; return false; } void FootPrint(MazeType maze, PosType p){ maze[p.i][p.j] = 2; } bool Pass(MazeType maze, PosType p){ if(1 == maze[p.i][p.j] || HasFootPrint(p)) return false; return true; } PosType NextPos(PosType p, int di){ int i1 = p.i, j1 = p.j; switch (di) { case 1: //东 j1 = p.j + 1; break; case 2: //南 i1 = p.i + 1; break; case 3: //西 j1 = p.j - 1; break; case 4: //北 i1 = p.i - 1; break; default: break; } if(i1 < 0 ) i1 = 0; else if(i1 > ROW-1) i1 = ROW-1; if(j1 < 0) j1 = 0; else if(j1 > COL-1) j1 = COL-1; PosType r = {i1, j1}; return r; } bool PosTypeEqual(PosType p1, PosType p2){ if(p1.i == p2.i && p1.j == p2.j) return true; return false; } void MarkPrint(MazeType maze, PosType p){ maze[p.i][p.j] = 3;//表示所有方向都尝试了,不能通过 } Status MazePath(MazeType maze, PosType start, PosType end, SqStack& s){ //若迷宫maze中存在从入口start到出口end的通道,则求得一条存放在栈中(从栈底到栈顶),并返回TRUE,否则返回FALSE PosType curPos = start; int curstep = 1; do{ if(Pass(maze, curPos)){//当前位置可以通过,也未曾走过 FootPrint(maze,curPos);//留下足迹 SElemType e = {curstep,curPos,1}; SqStack_Push(s,e); if(curPos.i == end.i && curPos.j==end.j) return TRUE; curPos = NextPos(curPos,1);//下一位置是当前位置的东邻 ++curstep; } else{ if(!SqStack_IsEmpty(s)){ SElemType e; SqStack_Pop(s, &e); while(4 == e.di && !SqStack_IsEmpty(s)){ MarkPrint(maze, e.seat); SqStack_Pop(s, &e); } if(e.di < 4){ e.di++; SqStack_Push(s,e); curPos = NextPos(e.seat, e.di);//换一个方向 } } } }while(!SqStack_IsEmpty(s)); return FALSE; } int main() { for(int i=0;i<ROW;++i){ for(int j=0; j<COL; ++j){ cout << maze[i][j] << ' '; } std::cout << '\n'; } SqStack s; SqStack_InitStack(s); PosType start = {1,1}, end={8,8}; MazePath(maze,start,end,s); SqStack_Traverse(s,displaySElemType); for(int i=0;i<ROW;++i){ for(int j=0; j<COL; ++j){ cout << maze[i][j] << ' '; } std::cout << '\n'; } return 0; } /* 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0 0 1 0 1 1 0 0 1 0 0 0 1 0 1 1 0 0 0 0 1 1 0 0 1 1 0 1 1 1 0 0 0 0 1 1 0 0 0 1 0 0 0 0 1 1 0 1 0 0 0 1 0 0 1 1 0 1 1 1 0 1 1 0 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 (1,1) (1,2) (2,2) (3,2) (3,1) (4,1) (5,1) (5,2) (5,3) (6,3) (6,4) (6,5) (7,5) (8,5) (8,6) (8,7) (8,8) 1 1 1 1 1 1 1 1 1 1 1 2 2 1 3 3 3 1 0 1 1 0 2 1 3 3 3 1 0 1 1 2 2 3 3 1 1 0 0 1 1 2 1 1 1 0 0 0 0 1 1 2 2 2 1 0 0 0 0 1 1 0 1 2 2 2 1 0 0 1 1 0 1 1 1 2 1 1 0 1 1 1 0 0 0 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 */
--------------
常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。
昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。