《数据结构》应用实例

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

栈的应用:

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<cstring>
#include<cstdio>
#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;
}

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,tmp,E[1000];
    initStack(S);
    cout<<"Enter brackets,containing only '(','[','<',')',']','>':"<<endl;
    while(cin>>E)
    {
        e = E;
        clearStack(S);
        while(*(e) != '\0')
        {
            if(*(e) == '(' || *(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;
            }
            else if(*(e) == '>')
            {
                top(S,tmp);
                if(tmp == '<')  pop(S);
                else break;
            }
            e++;
        }
        if(getLength(S))    cout<<"Not Matched!"<<endl;
        else cout<<"Matched!"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/AAMahone/article/details/82751427