栈是一种只能从表的一端存取数据且遵循 "先进后出" 原则的线性存储结构。栈是一种只能从表的一端存取数据且遵循 "先进后出" 原则的线性存储结构。
我发现了,基于在之前的学到的许许多多的结构,我发现了其实数据结构学来的目的其实应该与因地制宜的道理相同,根据对于数据的不同操作,我就要选取不同的数据结构来定义使用。就好比如果我要实现括号匹配,那么就应该用栈这样的“先出后进”的数据结构形态,这样才可以完美的适配。再比如,若要对数据进行排序,使用链表只会增加计算机处理代码的复杂度,不利于其良好的运转,与此同时算法复杂,不利于我们去理解和思考。而线性表便完美的解决了这一问题,这也就是为什么如此多数据结构出现的必然,使用数据结构的多样性,对我们以后对数据的处理以及操作有了很大的提升。如果条件允许的情况下,我也愿意去继续进行计算机数据结构的开发,寻找更多的适用的结构,推动计算机代码的发展。
对于多项式的加法,我的代码目前还在研究当中,只能先抄写一下学长的去学习一下,我本人其实对于C++这一语言不太懂,对于我理解来说还是有一些困难的,所以希望大家可以教教我,为我讲解一下其与C语言不同的地方,帮助我理解,感谢在我的帖子底下留言
在学习老师代码的过程中,我不仅仅学到了栈这一数据结构的知识,同时我学到了许许多多的语言规范,在我上学期打代码的时候,我只会使用abcd来定义变量,这样的代码我自己也感觉到其不妥,每次当我出错的时候,我就要从我众多abcd中去寻找到底是哪里出错了,这样给我造成了巨大的麻烦,而到这学期,我要是还像以前你要去定义变量,若最终出错,我会崩溃的。而老师这样的规范就正好帮助了解决了这个难题,成功的实现代码上的突破。
代码 (1)括号匹配#include测试结果 (2)多项式的加法#include #define STACK_MAX_SIZE 10 typedef struct CharStack { int top; int data[STACK_MAX_SIZE]; } *CharStackPtr; void outputStack(CharStackPtr paraStack) { for (int i = 0; i <= paraStack->top; i ++) { printf("%c ", paraStack->data[i]); } printf("rn"); } CharStackPtr charStackInit() { CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(struct CharStack)); resultPtr->top = -1; return resultPtr; } void push(CharStackPtr paraStackPtr, int paraValue) { if (paraStackPtr->top >= STACK_MAX_SIZE - 1) { printf("Cannot push element: stack full.rn"); return; } paraStackPtr->top ++; paraStackPtr->data[paraStackPtr->top] = paraValue; } char pop(CharStackPtr paraStackPtr) { if (paraStackPtr->top < 0) { printf("Cannot pop element: stack empty.rn"); return ' '; } paraStackPtr->top --; return paraStackPtr->data[paraStackPtr->top + 1]; } void pushPopTest() { printf("---- pushPopTest begins. ----rn"); CharStackPtr tempStack = charStackInit(); printf("After initialization, the stack is: "); outputStack(tempStack); for (char ch = 'a'; ch < 'm'; ch ++) { printf("Pushing %c.rn", ch); push(tempStack, ch); outputStack(tempStack); } for (int i = 0; i < 3; i ++) { char cha = pop(tempStack); printf("Pop %c.rn", cha); outputStack(tempStack); } printf("---- pushPopTest ends. ----rn"); } bool bracketMatching(char* paraString, int paraLength) { CharStackPtr tempStack = charStackInit(); push(tempStack, '#'); char tempChar, tempPopedChar; for (int i = 0; i < paraLength; i++) { tempChar = paraString[i]; switch (tempChar) { case '(': case '[': case '{': push(tempStack, tempChar); break; case ')': tempPopedChar = pop(tempStack); if (tempPopedChar != '(') { return false; } break; case ']': tempPopedChar = pop(tempStack); if (tempPopedChar != '[') { return false; } break; case '}': tempPopedChar = pop(tempStack); if (tempPopedChar != '{') { return false; } break; default: break; } } tempPopedChar = pop(tempStack); if (tempPopedChar != '#') { return true; } return true; } void bracketMatchingTest() { printf("rn---- bracketMatchingTest ----rn"); char* tempExpression = "[2 + (1 - 3)] * 4"; bool tempMatch = bracketMatching(tempExpression, 17); printf("Is the expression '%s' bracket matching? %d rn", tempExpression, tempMatch); tempExpression = "( ) )"; tempMatch = bracketMatching(tempExpression, 6); printf("Is the expression '%s' bracket matching? %d rn", tempExpression, tempMatch); tempExpression = "()()(())"; tempMatch = bracketMatching(tempExpression, 8); printf("Is the expression '%s' bracket matching? %d rn", tempExpression, tempMatch); tempExpression = "({}[])"; tempMatch = bracketMatching(tempExpression, 6); printf("Is the expression '%s' bracket matching? %d rn", tempExpression, tempMatch); tempExpression = ")("; tempMatch = bracketMatching(tempExpression, 2); printf("Is the expression '%s' bracket matching? %d rn", tempExpression, tempMatch); printf("---- bracketMatchingTest ----"); } int main() { pushPopTest(); bracketMatchingTest(); return 0; }
#include#include #include #include #include using namespace std; stack num; stack op; void eval() { auto b = num.top(); num.pop(); auto a = num.top(); num.pop(); auto c = op.top(); op.pop(); int x; if (c == '+') x = a + b; else if (c == '-') x = a - b; else if (c == '*') x = a * b; else x = a / b; num.push(x); } int main() { unordered_map pr{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}}; string str; cin >> str; for (int i = 0; i < str.size(); i ++ ) { auto c = str[i]; if (isdigit(c)) { int x = 0, j = i; while (j < str.size() && isdigit(str[j])) x = x * 10 + str[j ++ ] - '0'; i = j - 1; num.push(x); } else if (c == '(') op.push(c); else if (c == ')') { while (op.top() != '(') eval(); op.pop(); } else { while (op.size() && op.top() != '(' && pr[op.top()] >= pr[c]) eval(); op.push(c); } } while (op.size()) eval(); cout << num.top() << endl; return 0; }



