《数据结构》应用实例

最近发现上课学习了一些有趣的数据结构算法应用实例,想着把它们记录下来以便日后复习,遂有此文,写得不好请见谅

栈的应用:

1.进制转换,输入m,n代表数和进制,要求实现转换

     这个题目实际上是应用了进制转换的规则特点:除基取余,应用短除法将得到的结果自底向上拼接成的数即是n进制数,相当于顺序输入逆序输出,符合栈的特性,因此可以用栈来写这个题目

#include<iostream>
#include<cstring>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define INIT_SIZE 100
#define DLC 10
typedef int Status,ElemType;
using namespace std;

typedef struct Stack
{
    ElemType *base;
    ElemType *top;
    int _size;
}Stack;

Status initStack(Stack &S)
{
    S.base = (ElemType *)malloc(INIT_SIZE*sizeof(ElemType));
    if(!S.base) exit(OVERFLOW);
    S.top = S.base;
    S._size = INIT_SIZE;
    return OK;
}

inline int getLength(Stack S)
{
    return S.top-S.base;
}

inline void clearStack(Stack &S)
{
    S.top = S.base;
    S._size = INIT_SIZE;
}

Status push(Stack &S,ElemType e)
{
    int len = getLength(S);
    if(len >= S._size)
    {
        S.base = (ElemType *)realloc(S.base,(S._size+DLC)*sizeof(ElemType));
        if(!S.base) exit(OVERFLOW);
        S._size += DLC;
    }
    *S.top++ = e;
}

Status top(Stack S,ElemType &e)
{
    if(!getLength(S))   return ERROR;
    e = *(S.top-1);
    return OK;
}

Status pop(Stack &S)
{
    if(!getLength(S))   return ERROR;
    S.top--;
    return OK;
}

int main()
{
    int m,n;
    Stack S;
    ElemType e;
    initStack(S);
    cout<<"Enter m and n,indicating the number and the system:"<<endl;
    while(cin>>m>>n)
    {
        clearStack(S);
        while(m)
        {
            push(S,m%n);
            m /= n;
        }
        while(getLength(S))
        {
            top(S,e);
            cout<<e;
            pop(S);
        }
        cout<<endl;
    }
    return 0;
}

2.括号配对,数据结构的经典题目,给你一串由括号组成的字符串,问其中所有括号是否满足两两配对

    这题的思路简单说一下,遍历字符串,如遇左括号,则入栈,遇到右括号,则比较该右括号是否与栈顶配对,是即出栈,继续查找下一个字符,否则括号不匹配,提前结束遍历,最后结束遍历以后,若栈为空,则括号配对,否则不配对

#include<iostream>
#include<cstdlib>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define INIT_SIZE 100
#define DLC 10
typedef int Status;
typedef char ElemType;
using namespace std;
 
typedef struct Stack
{
    ElemType *base;
    ElemType *top;
    int _size;
}Stack;
 
Status initStack(Stack &S)
{
    S.base = (ElemType *)malloc(INIT_SIZE*sizeof(ElemType));
    if(!S.base) exit(OVERFLOW);
    S.top = S.base;
    S._size = INIT_SIZE;
    return OK;
}
 
inline int getLength(Stack S)
{
    return S.top-S.base;
}
 
inline void clearStack(Stack &S)
{
    S.top = S.base;
    S._size = INIT_SIZE;
}
 
Status push(Stack &S,ElemType e)
{
    int len = getLength(S);
    if(len >= S._size)
    {
        S.base = (ElemType *)realloc(S.base,(S._size+DLC)*sizeof(ElemType));
        if(!S.base) exit(OVERFLOW);
        S._size += DLC;
    }
    *S.top++ = e;
    return OK;
}
 
Status top(Stack S,ElemType &e)
{
    if(!getLength(S))   return ERROR;
    e = *(S.top-1);
    return OK;
}
 
Status pop(Stack &S)
{
    if(!getLength(S))   return ERROR;
    S.top--;
    return OK;
}
 
int main()
{
    int n;
    cin>>n;
    Stack S;
    ElemType *e,tmp,E[10000];
    initStack(S);
    while(n--)
    {
        e = E;
        cin>>E;
        clearStack(S);
        while(*(e) != '\0')
        {
            tmp = '@';  //解决程序中的bug;
            if(*(e) == '(' || *(e) == '[')
                push(S,*(e));
            else if(*(e) == ')')
            {
                top(S,tmp);
                if(tmp == '(')  pop(S);
                else break;
            }
            else if(*(e) == ']')
            {
                top(S,tmp);
                if(tmp == '[')  pop(S);
                else break;
            }
            e++;
        }
        if(getLength(S) || *e != '\0')    cout<<"No"<<endl;
        else cout<<"Yes"<<endl;
    }
    return 0;
}
 

 

  • 9
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值