栈与栈的应用(括号匹配)

1 栈

栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表。它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来)。栈具有记忆作用,对栈的插入与删除操作中,不需要改变栈底指针。

代码

栈的创建

#include <stdio.h>
#include <malloc.h>

#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]);
    }
    printf("\r\n");
}

栈的初始化

CharStackPtr charStackInit() {
    CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(CharStack));
    resultPtr->top = -1;

    return resultPtr;
}

进栈

void push(CharStackPtr paraStackPtr, int paraValue) {
    // Step 1. Space check.
    if (paraStackPtr->top >= STACK_MAX_SIZE - 1) {
        printf("Cannot push element: stack full.\r\n");
        return;
    }//Of if

    // Step 2. Update the top.
    paraStackPtr->top ++;

    // Step 3. Push element.
    paraStackPtr->data[paraStackPtr->top] = paraValue;
}

出栈

char pop(CharStackPtr paraStackPtr) {
    // Step 1. Space check.
    if (paraStackPtr->top < 0) {
        printf("Cannot pop element: stack empty.\r\n");
        return '\0';
    }//Of if

    // Step 2. Update the top.
    paraStackPtr->top --;

    // Step 3. Push element.
    return paraStackPtr->data[paraStackPtr->top + 1];
}// Of pop

进栈出栈测试

/**
 * Test the push function.
 */
void pushPopTest() {
    printf("---- pushPopTest begins. ----\r\n");

    // Initialize.
    CharStackPtr tempStack = charStackInit();
    printf("After initialization, the stack is: ");
    outputStack(tempStack);

    // Pop.
    for (char ch = 'a'; ch < 'm'; ch ++) {
        printf("Pushing %c.\r\n", ch);
        push(tempStack, ch);
        outputStack(tempStack);
    }//Of for i

    // Pop.
    for (int i = 0; i < 3; i ++) {
        char ch;
        ch = pop(tempStack);
        printf("Pop %c.\r\n", ch);
        outputStack(tempStack);
    }//Of for i

    printf("---- pushPopTest ends. ----\r\n");
}// Of pushPopTest

函数入口

void main() {
    pushPopTest();
}

测试结果

 

 

2 栈的应用———括号匹配

括号匹配可以说是栈最经典的应用了,通过括号匹配可以认识到栈的神奇。

 上代码

创建栈

#include <stdio.h>
#include <malloc.h>
//#include <stdbool.h>

#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("\r\n");
}

栈的初始化

CharStackPtr charStackInit() {
    CharStackPtr resultPtr = (CharStackPtr)malloc(sizeof(struct CharStack));
    resultPtr->top = -1;

    return resultPtr;
}

进栈

void push(CharStackPtr paraStackPtr, int paraValue) {
    // Step 1. Space check.
    if (paraStackPtr->top >= STACK_MAX_SIZE - 1) {
        printf("Cannot push element: stack full.\r\n");
        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.\r\n");
        return '\0';
    }//Of if

    // Step 2. Update the top.
    paraStackPtr->top --;

    // Step 3. Push element.
    return paraStackPtr->data[paraStackPtr->top + 1];
}// Of pop

 栈 push 和 pop 测试

void pushPopTest() {
    printf("---- pushPopTest begins. ----\r\n");

    // Initialize.
    CharStackPtr tempStack = charStackInit();
    printf("After initialization, the stack is: ");
    outputStack(tempStack);

    // Pop.
    for (char ch = 'a'; ch < 'm'; ch ++) {
        printf("Pushing %c.\r\n", ch);
        push(tempStack, ch);
        outputStack(tempStack);
    }//Of for i

    // Pop.
    for (int i = 0; i < 3; i ++) {
        char ch;
        ch = pop(tempStack);
        printf("Pop %c.\r\n", ch);
        outputStack(tempStack);
    }//Of for i

    printf("---- pushPopTest ends. ----\r\n");
}// Of pushPopTest

 

括号匹配函数

bool bracketMatching(char* paraString, int paraLength) {
  
    CharStackPtr tempStack = charStackInit();
    push(tempStack, '#');//让#成为栈底比-1更加方便,因为有时-1会被判定为ture
    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;
            } // 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 false;
    } // Of if

    return true;
}// Of bracketMatching

综合测试

 void bracketMatchingTest() {
    char* tempExpression = "[2 + (1 - 3)] * 4";
    bool tempMatch = bracketMatching(tempExpression, 17);
    printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);


    tempExpression = "( )  )";
    tempMatch = bracketMatching(tempExpression, 6);
    printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);

    tempExpression = "()()(())";
    tempMatch = bracketMatching(tempExpression, 8);
    printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);

    tempExpression = "({}[])";
    tempMatch = bracketMatching(tempExpression, 6);
    printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);


    tempExpression = ")(";
    tempMatch = bracketMatching(tempExpression, 2);
    printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);
}// Of bracketMatchingTest

函数入口

void main() {
    // pushPopTest();
    bracketMatchingTest();
}// Of main

测试结果

 

 

 


 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值