顺序栈链栈基本操作及应用-数据结构类C语言

目录

一、功能函数定义文件(func.cpp)

二、主函数调用文件(main.cpp)

三、头文件声明文件(before.h)

四、运行环境

一、功能函数定义文件(func.cpp)

#include 
#include 
#include "before.h"

using namespace std;

//顺序栈的相关功能函数定义
status InitStack(SqStack &S,int maxsize)
{
    S.base = (ElemType *)malloc(sizeof(ElemType)*maxsize);
    if(!S.base) return ERR;      //创建失败 空间不足

    S.top = S.base;              //空栈
    S.numb = maxsize;            //栈的最大容量
    return OK;
}//建栈

void PrintStack_SqStack(SqStack S)
{
    int cnt = 0;
    cout << "--当前顺序栈为:" << endl;
    cout << "--栈底=>";
    while(*S.base && S.top!=S.base)
    {
        cnt ++;
        cout << *(S.base++) << "  ";
        if(cnt%7 == 0) cout << endl << "-------";
    }
    cout << endl;
}//打印顺序栈

status Push_SqStack(SqStack &S,ElemType e)
{
    if(S.top-S.base >= S.numb) return OVERFLOW;  //栈满

    *S.top = e;
    S.top ++;
    return OK;
}//入栈

status Pop_SqStack(SqStack &S,ElemType &e)
{
    if(S.top == S.base) return ERR;  //空栈

    e = *(--S.top);
    return OK;
}//出栈

status GetTop_SqStack(SqStack S)
{
    if(S.top == S.base) return ERR; //空栈

    return *(S.top-1);
}//取栈顶元素

void DestroySqStack(SqStack &S)
{
    if(S.base)
    {
        free(S.base);
        S.numb = 0;
        S.base = S.top = NULL;
    }
}//销毁顺序栈

void ClearSqStack(SqStack &S)
{
    if(S.base) S.top = S.base;
}//清空顺序栈

status StackEmpty_SqStack(SqStack S)
{
    if(S.top == S.base) return OK;
    else return ERR;
}//判断顺序栈是否为空

status StackLength_SqStack(SqStack S)
{
    return (S.top-S.base);
}//顺序栈的长度

void Conversion_SqStack(SqStack &M,int con,int maxsize)
{
    InitStack(M,maxsize);
    int num;
    cout << "请输入需要转换的原始十进制数值:";
    cin >> num;
    int number = num;
    do
    {
        Push_SqStack(M,num%con);
        num /= con;
    }while(num != 0);
    cout << "10进制数" << number << "转换为" << con << "进制数为:";
    do
    {
        int e;
        Pop_SqStack(M,e);
        if(e < 10) cout << e;
        else if(e >= 10)
        {
            char ch = 'A' + e - 10;
            cout << ch;
        }
    }while(StackEmpty_SqStack(M) == ERR);
    cout << endl;
}//将顺序栈中的元素进行进制转换

status Compare_SqStack()
{
    SqStack C;
    int maxsize;                      //最多存储的括号数量
    int prime = OK,judge_1 = OK,judge_2 = OK;//记录括号是否匹配、满栈判断、空栈判断
    char ch;                          //存储每次输入的单个字符
    ElemType e;                       //存储输入的单个字符的对应ASCII码
    cout << "请输入需要查看括号匹配的字符串中的括号最多数量(实际入栈为其一半):";
    cin >> maxsize;
    InitStack(C,maxsize);
    cout << "请输入你需要查看括号是否匹配的字符串(以‘#’结束):" << endl;
    while((ch = getchar()) != '#' && prime == OK && judge_1 == OK)
    {
        switch(ch)
        {
            case '(':
            case '[':
            case '{':
                e = ch-'0';
                judge_1 = Push_SqStack(C,e);       //满栈
                break;
            case ')':
                judge_2 = Pop_SqStack(C,e);    //空栈
                if(judge_2 == ERR || e != '('-'0') prime = ERR;
                break;
            case ']':
                judge_2 = Pop_SqStack(C,e);    //空栈
                if(judge_2 == ERR || e != '['-'0') prime = ERR;
                break;
            case '}':
                judge_2 = Pop_SqStack(C,e);    //空栈
                if(judge_2 == ERR || e != '{'-'0') prime = ERR;
                break;
            default:
                break;
        }
    }
    if(prime == OK && ch =='#' && StackEmpty_SqStack(C) == OK && judge_1 == OK) return OK;
    else if(judge_1 == OVERFLOW) return OVERFLOW;
    else return ERR;
}//顺序栈的括号匹配判断

//链栈相关功能函数定义
void InitStack_LinkStack(LinkStack &S)
{
    S = NULL;
}//初始化链栈

void PrintLinkStack(LinkStack S)
{
    StackNode *p = S;
    int numb = 0;
    cout << "当前链栈为:" << endl;
    cout << "栈顶=>";
    while(p)
    {
        cout << p->data << "  ";
        numb ++;
        p = p->next;
        if(numb%7 == 0) cout << endl << "-------";
    }
    cout << endl;
}//输出链栈

void Push_LinkStack(LinkStack &S,ElemType e)
{
    StackNode *s = (StackNode *)malloc(sizeof(StackNode));
    s->data = e;
    s->next = S;
    S = s;
}//入栈

void Pop_LinkStack(LinkStack &S,ElemType &e)
{
    StackNode *p;
    p = S;
    e = S->data;
    S = S->next;
    free(p);
}//出栈

status GetTop_LinkStack(LinkStack S)
{
    if(S == nullptr) return ERR;      //空栈无栈顶元素
    return S->data;
}//取栈顶元素

void DestroyLinkStack(LinkStack &S)
{
    StackNode *p = S;
    while(p)
    {
        S = S->next;
        free(p);
        p = S;
    }
}//销毁链栈

void ClearLinkStack(LinkStack &S)
{
    StackNode *p = S->next;
    while(p)
    {
        S->next = p->next;
        free(p);
        p = S->next;
    }
}//清空链栈

status StackEmpty_LinkStack(LinkStack S)
{
    if(S->next == NULL) return OK;
    else return ERR;
}//判断链栈是否为空

status StackLength_LinkStack(int &num)
{
    return num;
}//返回链栈的长度

void Conversion_LinkStack(LinkStack &M,int con)
{
    InitStack_LinkStack(M);
    int num;
    cout << "请输入需要转换的原始十进制数值:";
    cin >> num;
    int number = num;
    do
    {
        Push_LinkStack(M,num%con);
        num /= con;
    }while(num != 0);
    cout << "10进制数" << number << "转换为" << con << "进制数为:";
    int e;
    while(M != NULL)
    {
        e = -1;
        Pop_LinkStack(M,e);
        if(e < 10) cout << e;
        else if(e >= 10)
        {
            char ch = 'A' + e -10;
            cout << ch;
        }
    }
    cout << endl;
}//链栈转换进制

status Compare_LinkStack()
{
    LinkStack C;
    InitStack_LinkStack(C);
    char ch;
    ElemType e;
    int prime = OK,judge_2 = OK;
    cout << "请输入需要查看括号是否匹配的字符串(以‘#’结束):" << endl;
    while((ch=getchar()) != '#' && prime == OK)
    {
        switch (ch)
        {
            case '(':
            case '[':
            case '{':
                e = ch-'0';
                Push_LinkStack(C,e);
                break;
            case ')':
                judge_2 = StackEmpty_LinkStack(C);
                Pop_LinkStack(C,e);
                if(e != '('-'0' || judge_2 == ERR) prime = ERR;
                break;
            case ']':
                judge_2 = StackEmpty_LinkStack(C);
                Pop_LinkStack(C,e);
                if(e != '['-'0' || judge_2 == ERR) prime = ERR;
                break;
            case '}':
                judge_2 = StackEmpty_LinkStack(C);
                Pop_LinkStack(C,e);
                if(e != '{'-'0' || judge_2 == ERR) prime = ERR;
                break;
            default:
                break;
        }
    }
    if(ch == '#' && prime == OK && C == NULL) return OK;
    else return ERR;
}//链栈来进行字符串括号匹配的判断

//菜单打印
void Menu()
{
    printf("\t\t\t*****************以下为程序菜单******************************\n");
    printf("\t\t\t\t1、顺序栈                      2、链栈                     \n");
    printf("\t\t\t\t0、退出程序                                                \n");
    printf("\t\t\t************************************************************\n");
}

void Menu_SqStack()
{
    printf("\t\t\t*****************以下为顺序栈的菜单**************************\n");
    printf("\t\t\t\t1、建栈                         2、入栈                    \n");
    printf("\t\t\t\t3、出栈                         4、打印顺序栈               \n");
    printf("\t\t\t\t5、取栈顶元素                    6、进制转换                \n");
    printf("\t\t\t\t7、销毁顺序栈                    8、清空顺序栈               \n");
    printf("\t\t\t\t9、检查顺序栈是否为空             10、顺序栈长度              \n");
    printf("\t\t\t\t11、字符串括号检查                0、退出操作系统            \n");
    printf("\t\t\tTips:请完成操作1和2之后再进行其余操作,否则程序会崩溃!          \n");
    printf("\t\t\t************************************************************\n");
}

void Menu_LinkStack()
{
    printf("\t\t\t*****************以下为链栈的菜单****************************\n");
    printf("\t\t\t\t1、建栈                         2、入栈                    \n");
    printf("\t\t\t\t3、出栈                         4、打印链栈                 \n");
    printf("\t\t\t\t5、取栈顶元素                    6、进制转换                \n");
    printf("\t\t\t\t7、销毁链栈                      8、清空链栈                \n");
    printf("\t\t\t\t9、检查链栈是否为空               10、链栈长度               \n");
    printf("\t\t\t\t11、字符串括号检查                0、退出操作系统            \n");
    printf("\t\t\tTips:请完成操作1和2之后再进行其余操作,否则程序会崩溃!          \n");
    printf("\t\t\t************************************************************\n");
}
//
// Created by somewon on 2022/10/1.
//

二、主函数调用文件(main.cpp)

#include 
#include 
#include "before.h"

using namespace std;

int main() {
    int CH,ch;
    int fine;
    ElemType e,ee;
    do {
        Menu();
        int des = ERR;     //记录栈是否被销毁
        cout << "请输入你的选择:";
        cin >> CH;
        if(CH == 1)        //顺序栈
        {
            SqStack S,M;
            int maxsize,con;
            do {
                Menu_SqStack();
                cout << "请输入你的选择:";
                cin >> ch;
                switch(ch)
                {
                    case 0:
                        goto ENDD;
                    case 1:
                        cout << "请输入顺序栈的最大容量:";
                        cin >> maxsize;
                        fine = InitStack(S,maxsize);
                        if(fine == OK)
                        {
                            cout << "建栈成功!" << endl;
                            des = ERR;
                        }
                        else cout << "建栈失败!" << endl;
                        break;
                    case 2:
                        cout << "请输入一个数据元素:" << endl;
                        cin >> e;
                        fine = Push_SqStack(S,e);
                        if(fine == OVERFLOW) cout << "入栈失败!该顺序栈已满!" << endl;
                        else if(fine == OK) cout << "当前元素入栈成功!" << endl;
                        PrintStack_SqStack(S);
                        break;
                    case 3:
                        fine = Pop_SqStack(S,e);
                        if(fine == ERR) cout << "空栈无法出栈元素!" << endl;
                        else
                        {
                            cout << e << endl;
                            PrintStack_SqStack(S);
                        }
                        break;
                    case 4:
                        PrintStack_SqStack(S);
                        break;
                    case 5:
                        fine = GetTop_SqStack(S);
                        if(fine == ERR) cout << "该顺序栈为空栈,无栈顶元素!" << endl;
                        else cout << "当前顺序栈的栈顶元素为:" << GetTop_SqStack(S) << endl;
                        break;
                    case 6:
                        cout << "请输入需要转换的进制(输入2-16的整数)为:";
                        cin >> con;
                        maxsize = 1000;
                        Conversion_SqStack(M,con,maxsize);
                        break;
                    case 7:
                        DestroySqStack(S);
                        cout << "顺序栈销毁成功!" << endl;
                        des = OK;
                        break;
                    case 8:
                        ClearSqStack(S);
                        cout << "顺序栈清空成功!" << endl;
                        break;
                    case 9:
                        if(des == OK) cout << "该顺序栈已被销毁!" << endl;
                        else
                        {
                            fine = StackEmpty_SqStack(S);
                            if(fine == OK) cout << "该顺序栈为空!" << endl;
                            else cout << "该顺序栈不为空!" << endl;
                        }
                        break;
                    case 10:
                        cout << "该顺序栈的长度为:" << StackLength_SqStack(S) << endl;
                        break;
                    case 11:
                        fine = Compare_SqStack();
                        if(fine == OK) cout << "输入的字符串括号匹配!" << endl;
                        else if(fine == OVERFLOW) cout << "栈满,无法进行判断!" << endl;
                        else cout << "输入的字符串括号不匹配!" << endl;
                        fflush(stdin);//清空缓冲区不必要的字符,防止带来不必要的类似死循环的错误
                        break;
                    default:
                        cout << "无该操作!" << endl;
                }
            }while(ch);
        }
        else if(CH == 2)  //链栈
        {
            LinkStack S,M;
            int num = 0;  //记录链表的长度
            int con;      //进制的存储
            do {
                Menu_LinkStack();
                int n;
                cout << "请输入你的选择:";
                cin >> ch;
                switch(ch)
                {
                    case 0:
                        goto ENDD;
                    case 1:
                        InitStack_LinkStack(S);
                        cout << "初始化栈成功!" << endl;
                        des = ERR;
                        break;
                    case 2:
                        cout << "请输入一个入栈元素:" << endl;
                        cin >> e;
                        Push_LinkStack(S,e);
                        num ++;
                        cout << "成功建立链栈!" << endl;
                        des = ERR;
                        PrintLinkStack(S);
                        break;
                    case 3:
                        if(S == NULL) cout << "空栈无法出栈元素!" << endl;
                        e = -1;
                        Pop_LinkStack(S,e);
                        num --;
                        cout << e << endl;
                        PrintLinkStack(S);
                        break;
                    case 4:
                        PrintLinkStack(S);
                        break;
                    case 5:
                        fine = GetTop_LinkStack(S);
                        if(fine == ERR) cout << "空栈无栈顶元素!" << endl;
                        else cout << "此时链栈的栈顶元素为:" << fine << endl;
                        break;
                    case 6:
                        cout << "请输入需要转换的进制(输入2-16的整数)为:";
                        cin >> con;
                        Conversion_LinkStack(M,con);
                        break;
                    case 7:
                        DestroyLinkStack(S);
                        cout << "销毁链栈成功!" << endl;
                        des = OK;
                        break;
                    case 8:
                        ClearLinkStack(S);
                        cout << "清空链栈成功!" << endl;
                        num = 0;
                        break;
                    case 9:
                        if(des == OK) cout << "该链栈已被销毁!" << endl;
                        else
                        {
                            fine = StackEmpty_LinkStack(S);
                            if(fine == OK) cout << "该链栈为空!" << endl;
                            else cout << "该链栈不为空!" << endl;
                        }
                        break;
                    case 10:
                        cout << "该链栈的元素个数为:" << StackLength_LinkStack(num) << endl;
                        break;
                    case 11:
                        fine = Compare_LinkStack();
                        if(fine == OK) cout << "输入的字符串括号匹配!" << endl;
                        else cout << "输入的字符串括号不匹配!" << endl;
                        fflush(stdin);//清空缓冲区不必要的字符
                        break;
                    default:
                        cout << "无该操作!" << endl;
                }
            }while(ch);
        }
        else if(CH == 0) goto END;
        else cout << "无该操作!\n" << endl;

        ENDD:
        printf("当前程序运行结束!\n");
    }while(CH);
    END:
    cout << "程序运行结束!欢迎您下次使用嘿哈!" << endl;
    return 0;
}
//
//Created by somewon on 2022/10/1.
//

三、头文件声明文件(before.h)

#ifndef _BEFORE_H
#define _BEFORE_H

#define OK 1
#define ERR 0
#define OVERFLOW (-1)

typedef int ElemType;
typedef int status;

typedef struct{
    ElemType *base;   //栈底指针
    ElemType *top;    //栈顶指针
    int numb;         //顺序栈个数
}SqStack;

//顺序栈相关功能函数声明
status InitStack(SqStack &S,int maxsize);     //建立空栈
void PrintStack_SqStack(SqStack S);           //打印顺序栈
status Push_SqStack(SqStack &S,ElemType e);   //入栈
status Pop_SqStack(SqStack &S,ElemType &e);   //出栈
status GetTop_SqStack(SqStack S);             //取栈顶元素
void DestroySqStack(SqStack &S);              //销毁顺序栈
void ClearSqStack(SqStack &S);                //清空顺序栈
status StackEmpty_SqStack(SqStack S);         //判断顺序栈是否为空
status StackLength_SqStack(SqStack S);        //顺序栈长度
void Conversion_SqStack(SqStack &M,int con,int maxsize);//数制转换
status Compare_SqStack();                     //括号匹配判断

typedef struct StackNode
{
    ElemType data;
    struct StackNode *next;
}StackNode,*LinkStack;

//链栈相关功能函数声明
void InitStack_LinkStack(LinkStack &S);       //初始化栈
void PrintLinkStack(LinkStack S);             //打印链栈
void Push_LinkStack(LinkStack &S,ElemType e); //入栈
void Pop_LinkStack(LinkStack &S,ElemType &e); //出栈
status GetTop_LinkStack(LinkStack S);         //取栈顶元素
void DestroyLinkStack(LinkStack &S);          //销毁链栈
void ClearLinkStack(LinkStack &S);            //清空链栈
status StackEmpty_LinkStack(LinkStack S);     //判断链栈是否为空
status StackLength_LinkStack(int &num);       //链栈长度
void Conversion_LinkStack(LinkStack &M,int con); //转换进制
status Compare_LinkStack();                   //字符串中的括号匹配判断

//相关菜单打印函数声明
void Menu();                                  //程序菜单
void Menu_SqStack();                          //顺序栈菜单
void Menu_LinkStack();                        //链栈菜单

#endif
//
// Created by somewon on 2022/10/1.
//

四、运行环境

Clion

Tips:注意若为dev编译环境,请将nullptr换为NULL;

你可能感兴趣的:(数据结构基本算法代码,c语言,c++,数据结构,链表,算法)