栈是一种操作受限的线性表只允许从一端插入和删除数据。栈有两种存储方式,即线性存储和链接存储(链表)。栈的一个最重要的特征就是栈的插入和删除只能在栈顶进行,所以每次删除的元素都是最后进栈的元素,故栈也被称为后进先出(LIFO)表。每个栈都有一个栈顶指针,它初始值为-1,且总是指向最后一个入栈的元素,栈有两种处理方式,即进栈(push)和出栈(pop),因为在进栈只需要移动一个变量存储空间,所以它的时间复杂度为O(1),但是对于出栈分两种情况,栈未满时,时间复杂度也为O(1),但是当栈满时,需要重新分配内存,并移动栈内所有数据,所以此时的时间复杂度为O(n)。以下举例栈结构的两种实现方式,线性存储和链接存储。
代码如下(示例):
#include#include #define STACK_MAX_SIZE 10 typedef struct CharStack { int top; int data[STACK_MAX_SIZE]; //The maximum length is fixed. } *CharStackPtr; void outputStack(CharStackPtr paraStack) { for (int i = 0; i <= paraStack->top; i ++) { printf("%c ", paraStack->data[i]); }// Of for i printf("rn"); }// Of outputStack CharStackPtr charStackInit() { CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(struct CharStack)); resultPtr->top = -1; return resultPtr; }//Of charStackInit void push(CharStackPtr paraStackPtr, int paraValue) { // Step 1. Space check. if (paraStackPtr->top >= STACK_MAX_SIZE - 1) { printf("Cannot push element: stack full.rn"); return; }//Of if // Step 2. Update the top. paraStackPtr->top ++; // Step 3. Push element. paraStackPtr->data[paraStackPtr->top] = paraValue; }// Of push char pop(CharStackPtr paraStackPtr) { // Step 1. Space check. if (paraStackPtr->top < 0) { printf("Cannot pop element: stack empty.rn"); return ' '; }//Of if // Step 2. Update the top. paraStackPtr->top --; // Step 3. Push element. return paraStackPtr->data[paraStackPtr->top + 1]; }// Of pop void pushPopTest() { char ch; printf("---- pushPopTest begins. ----rn"); // Initialize. CharStackPtr tempStack = charStackInit(); printf("After initialization, the stack is: "); outputStack(tempStack); // Pop. for (char ch = 'a'; ch < 'm'; ch ++) { printf("Pushing %c.rn", ch); push(tempStack, ch); outputStack(tempStack); }//Of for i // Pop. for (int i = 0; i < 3; i ++) { ch = pop(tempStack); printf("Pop %c.rn", ch); outputStack(tempStack); }//Of for i printf("---- pushPopTest ends. ----rn"); }// Of pushPopTest bool bracketMatching(char* paraString, int paraLength) { // Step 1. Initialize the stack through pushing a '#' at the bottom. CharStackPtr tempStack = charStackInit(); push(tempStack, '#'); char tempChar, tempPopedChar; // Step 2. Process the string. 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; } // Of if break; case ']': tempPopedChar = pop(tempStack); if (tempPopedChar != '[') { return false; } // Of if break; case '}': tempPopedChar = pop(tempStack); if (tempPopedChar != '{') { return false; } // Of if break; default: // Do nothing. break; }// Of switch } // Of for i tempPopedChar = pop(tempStack); if (tempPopedChar != '#') { return true; } // Of if return true; }// Of bracketMatching void bracketMatchingTest() { char* tempExpression = "[5*(7+2)/2]"; 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); }// Of bracketMatchingTest int main() { // pushPopTest(); bracketMatchingTest(); }// Of main
代码测试:
测试中出现的bug:
(1):在我的编译器里面找不到ch这个变量,所以我只能在函数后面再一次定义char ch;
(2):在最后那个main函数里,老师用的是void定义main函数。在我的编译器提醒的是main函数只能用int编译,我也不知道为什么,如果有知道的可以提出。谢谢
栈的进栈
括号的进栈;



