数据结构——栈的应用

任务一

进行栈的模拟将十进制换成八进制输出

代码
#ifndef stack_h
#define stack_h
#include<stdio.h>
#include<stdlib.h>

typedef int 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 Decimal_Conversion_Octal(T e);//利用栈实现进制转换
#endif
#include<iostream>
#include"hanshu.h"
using namespace std;
int main(){
    T e;
    cin>>e;
    Decimal_Conversion_Octal(e);
    return 0;
}
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){
    if(Stack_IsFull(stk)){
        cout<<"stack is full"<<endl;
        Stack_Free(stk);
        exit(0);//正常运行并退出程序
    }
    else{
        stk->top += 1;
        stk->data[stk->top] = e;
        return Stack_Top(stk);
    }
}
T Stack_Pop(Stack* stk){
    if(Stack_IsEmpty(stk)){
        cout<<"stack is empty"<<endl;
        Stack_Free(stk);
        exit(0);
    }
    else{
        T topE = Stack_Top(stk);
        stk->top -= 1;
        return topE;
    }
}
void Stack_Print(Stack* stk){
    if(Stack_IsEmpty(stk)){
        cout<<"stack is empty"<<endl;
        return ;
    }
    for(int i = stk->top;i >= 0;i--){
        cout<<stk->data[i];
    }
    cout<<endl;
}
void Decimal_Conversion_Octal(T e){
    Stack *L = Stack_Create(110);
    while(e){
        Stack_Push(L,e%8);
        e /= 8;
    }
    Stack_Print(L);
}
exit(0) and exit(1)

e x i t ( 0 ) exit(0) exit(0):正常运行程序并退出程序;
e x i t ( 1 ) exit(1) exit(1):非正常运行导致退出程序;

任务二

判断字符串括号是否匹配。

思路

我们判断括号匹配利用栈的先进后出和优先级,后放入的左括号的优先级比先放入的左括号优先级更高。

代码
#ifndef stack_h
#define stack_h
#include<stdio.h>
#include<stdlib.h>
typedef int 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_Math(T* str, int len);
//利用stack栈判断括号是否匹配,匹配输出yes,否则输出no
#endif
#include<iostream>
#include"hanshu.h"
using namespace std;
int main(){
    int len;
    T* str;
    cin>>len;
    str = (T*)malloc(sizeof(T)*len);
    cin>>str;
    Bracket_Math(str,len);
    return 0;
}
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){
    if(Stack_IsFull(stk)){
        cout<<"stack is full"<<endl;
        Stack_Free(stk);
        exit(0);//正常运行并退出程序
    }
    else{
        stk->top += 1;
        stk->data[stk->top] = e;
        return Stack_Top(stk);
    }
}
T Stack_Pop(Stack* stk){
    if(Stack_IsEmpty(stk)){
        cout<<"stack is empty"<<endl;
        Stack_Free(stk);
        exit(0);
    }
    else{
        T topE = Stack_Top(stk);
        stk->top -= 1;
        return topE;
    }
}
void Stack_Print(Stack* stk){
    if(Stack_IsEmpty(stk)){
        cout<<"stack is empty"<<endl;
        return ;
    }
    for(int i = stk->top;i >= 0;i--){
        cout<<stk->data[i];
    }
    cout<<endl;
}
void Bracket_Math(T* str, int len){
    bool flag = true;
    int i = 0;
    Stack *ch = Stack_Create(len);
    while(str[i]!='\0'&&flag){
        if(str[i]=='['||str[i]=='('||str[i]=='{'){
            Stack_Push(ch,str[i]);
        }
        else if(str[i]==']'){
            if(!Stack_IsEmpty(ch)&&Stack_Top(ch)=='['){
                Stack_Pop(ch);
            }
            else{
                flag = false;
            }
        }
        else if(str[i]==')'){
            if(!Stack_IsEmpty(ch)&&Stack_Top(ch)=='('){
                Stack_Pop(ch);
            }
            else{
                flag = false;
            }
        }
        else if(str[i]=='}'){
            if(!Stack_IsEmpty(ch)&&Stack_Top(ch)=='{'){
                Stack_Pop(ch);
            }
            else{
                flag = false;
            }
        }
        i++;
    }
    if(flag&&Stack_IsEmpty(ch)){
        cout<<"Yes"<<endl;
    }
    else{
        cout<<"No"<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值