c语言实现栈的字符串括号匹配

我特意发这篇博客呢,是因为我做这道题时,花费了很多的时间,不过现在也值了,古时候有个叫什么米蒂的,花费很多天,只为写好有个字,今天由我这小白,为了栈,也是醉了,花了七八个小时才搞懂。
首先呢,要注意每一个函数的返回值,因为这是函数之间的相互联系。就好比我下面的create函数,必须要有stack * stk,获取内存空间,另外,最重要的就是搞懂数据类型咯,好了,废话少说,上代码。

#include <iostream>
#include <cstring>
//#include "stack_.h"
#ifndef stack__h
#define stack__h

#include <stdio.h>
#include <stdlib.h>

typedef char T; // 数据元素的数据类型

struct Stack{
    T* data;   // 数据元素存储空间的开始地址
    int top;   // 栈表的当前位置
    int max;   // 栈表的最大长度
};


Stack* Stack_Create(int maxlen);
// 创建栈

void Stack_Free(Stack* stk);
// 释放栈

void Stack_MakeEmpty(Stack* stk);
// 置为空栈

bool Stack_IsEmpty(Stack* stk);
// 判断栈是否空

bool Stack_IsFull(Stack* stk);
// 判断栈是否满

T Stack_Top(Stack* stk);
// 获取当前栈顶元素

T Stack_Push(Stack* stk, T e);
// 将元素e压入栈顶
// 返回栈顶点元素

T Stack_Pop(Stack* stk);
// 将栈顶元素出栈
// 返回栈顶元素

void Stack_Print(Stack* stk);
// 打印栈顶到栈低的元素

void Bracket_Match(T* str, int len);
//  利用stack栈判断括号是否匹配
//  输入参数:字符串序列,字符串长度
//  若匹配输出YES,否则输出NO,末尾换行

#endif /* stack__h */


//
//  stack_.cpp
//  Bracket_Match
//
//  Created by ljpc on 2018/4/18.
//  Copyright ? 2018年 ljpc. All rights reserved.
//

//#include "stack_.h"
#include<string.h>

// 栈表操作实现文件
//////////////////////////////////////////////////////////////


Stack* Stack_Create(int maxlen)
// 创建栈
{
    Stack* stk = (Stack*)malloc(sizeof(Stack));
    stk->data = (T*)malloc(sizeof(T)*maxlen);
    stk->max = maxlen;
    stk->top = -1;
    return stk;
}

void Stack_Free(Stack* stk)
// 释放栈
{
    free(stk->data);
    free(stk);
}

void Stack_MakeEmpty(Stack* stk)
// 置为空栈
{
    stk->top = -1;
}

bool Stack_IsEmpty(Stack* stk)
// 判断栈是否空
{
    return -1 == stk->top;
}

bool Stack_IsFull(Stack* stk)
// 判断栈是否满
{
    return stk->top == stk->max-1;
}

T Stack_Top(Stack* stk)
// 获取当前栈顶元素
{
    return stk->data[stk->top];
}

T Stack_Push(Stack* stk, T e)
// 将元素e压入栈顶
// 返回栈顶点元素
{
    if(Stack_IsFull(stk)) {
        printf("Stack_IsFull(): stack full error when push element to the stack!\n");
        Stack_Free(stk);
        exit(0);
    }
    else{
    //	printf("      1      ");
        stk->top += 1;
        stk->data[stk->top] = e;
        return Stack_Top(stk);
    }
}

T Stack_Pop(Stack* stk)
// 将栈顶元素出栈
// 返回栈顶元素
{
    if(Stack_IsEmpty(stk)) {
        printf("Stack_IsEmpty(): stack empty error when pop element of the stack top!\n");
        Stack_Free(stk);
        exit(0);
    }
    else{
        T topE = Stack_Top(stk);
        stk->top -= 1;
        return topE;
    }
}

void Stack_Print(Stack* stk)
// 打印栈顶到栈低的元素
{
	char str1[100];
	memset(str1,0,sizeof(str1));
	int n = 0,t,j;
    if (Stack_IsEmpty(stk)) {
        printf("The stack is empty.\n");
        return;
    }
    
    //printf("The stack contains: ");
    for (int i=stk->top; i>=0; i--) {
       printf("%d",stk->data[i]);
    }
    printf("\n");
    
}

void Bracket_Match(T* str, int len)
//  利用stack栈判断括号是否匹配
//  输入参数:字符串序列,字符串长度
//  若匹配输出YES,否则输出NO,末尾换行
{
    // 请在这里补充代码,完成本关任务
    /********** Begin *********/
	Stack* stk = NULL;
   // int maxlen = 100;
    stk = Stack_Create(len);
    int i, j, n = 0, m = 0, temp = 1;
    char str1[100],str2;
   for(i=0;i<len;i++)
    {
    	switch(str[i])
    	{
    		case '(':
    		case '{':
    		case '[':
    				Stack_Push(stk,str[i]);
    				break;
    		case ')':
    			//if(!Stack_IsEmpty(stk))
    			//Stack_Print(stk);
    			//printf(" 1");
    			if(Stack_Top(stk)=='(')
    			 Stack_Pop(stk);
    				break;
    		case '}':
    			//if(!Stack_IsEmpty(stk))
    			//Stack_Print(stk);
    			//printf(" 2");
    			if(Stack_Top(stk)=='{')
    				Stack_Pop(stk);
    				break;
    		case ']':
    		//	if(!Stack_IsEmpty(stk))
    		//	Stack_Print(stk);
    		//	printf(" 3");
    			if(Stack_Top(stk)=='[')
    			 Stack_Pop(stk);
    				break;
    		default:
    				break;
    	}
    }
    if(!Stack_IsEmpty(stk))
    printf("No");
    else
    printf("YES");
    printf("\n"); 
    /********** End **********/
}
// [([{{[()]}}(])])
//({([][])[]({}){}()(((())()))(){[][[[]]]}(){}(){}()[]()[()]})
int main(int argc, const char * argv[]) {
    // insert code here...
    // std::cout << "Hello, World!\n";
    int len;
    T* str;
    scanf("%d", &len);
    str = (T*)malloc(sizeof(T)*len);
    scanf("%s", str);
    Bracket_Match(str, len);
    return 0;
}
发布了20 篇原创文章 · 获赞 1 · 访问量 2489

猜你喜欢

转载自blog.csdn.net/m0_46198325/article/details/105134891