#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
p
#include <stdio.h> #include <string> #include "SqStack.h" //将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 dealSymbol(std::string str){ std::string k1 = "(["; std::string k2 = ")]"; SqStack s; SqStack_InitStack(s); for (int i = 0; i < str.size(); i++) { char ch = str.at(i); if (k1.find(ch) != std::string::npos) { } } SqStack_Push(s, s[]) } int main() { //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); printf("love\n"); return 0; }
--------------
标签:Status,SElemType,return,SqStack,top,之栈,base,数据结构 From: https://www.cnblogs.com/htj10/p/16824716.html