zoukankan      html  css  js  c++  java
  • 数据结构实验二:栈和队列的基本操作和应用

    栈——进制的转换

    #include <iostream.h>
    #include <malloc.h>
    #include <stdlib.h>
    typedef struct{
        int * base
        int *top;
        int stacksize;
    }Stack;
    int InitStack(Stack &S)
    {
        S.base = (int *) malloc(STACK_INIT_SIZE*sizeof(int));
        if (!S.base) exit(OVERFLOW);
        S.top = S.base;
        S.stacksize = STACK_INIT_SIZE;
        return 1;
    }
    int Push(Stack &S, int e )
    {
        if (S.top-S.base >= S.stacksize)
        {
            S.base = (int *) realloc(S.base, (S.stacksize + T) *sizeof(int));
            if (!S.base) exit(OVERFLOW);
            S.top = S.base + S.stacksize;
            S.stacksize += T;
        }
        *S.top++ = e;
        return 1;
    }
    int Pop(Stack &S, int &e)
    {
        if (S.top == S.basereturn 0;
        e = *--S.top;
        return 1;
    }
    int StackEmpty(Stack S)
    {
        if (S.top == S.base)
        return 1;
        else return 0;
    }
    void conversion(int N, int r)
    {
        Stack S;
        int e; 
        InitStack(S);
        while(N)
        {
            Push(S, N%r);
            N = N/r;
        }
    }
    while(!StackEmpty(S)){  
      Pop(S, e);
        cout << e;
    }
    void main()
    {
        int N, r;
        cin >> N;
        cin >> r;
        cout << N << "由十进制转换为" << r << "进制后的值为:";
        conversion(N, r);
        cout << endl;
    }

    括号匹配检验
    #include<iostream.h>
    #include<string.h>
    typedef struct node{
        elemtype data;
        struct node *next;
    }stack;
    stack *top;
    void push(elemtype n){
        stack *p;
        p=new stack;
        p->data=n;
        p->next=top->next;
        top->next=p;
    }
    void pop(){
        stack *q;
        q=top->next;
        top->next=top->next->next;
        delete q;
    }
    elemtype gettop(){
        return top->next->data;
    }
    void judge(char a[]){
        int i,tag=0,len=strlen(a);//tag==1 
        for(i=0;i<len;i++)
        if(a[i]=='('||a[i]=='['||a[i]=='{')
            {push(a[i]);continue;}
            if(top->next==NULL)
            {
                cout<<"No"<<endl;
                break;
                tag=1;
            }
            if(a[i]!=')'&&a[i]!=']'&&a[i]!='}')
            continue;
        if(a[i]-gettop()==1||a[i]-gettop()==2)
            pop();
            else
            {
                cout<<"No"<<endl;
                break;
                tag=1;
            }
        }
        if(tag==0&&i==len)
        {
            if(top->next==NULL)
            cout<<"Yes"<<endl;
            else
            cout<<"No"<<endl;
        }
        top->next=NULL;
    }
    int main()
    {
        top=new stack;
        top->next=NULL;
        char a[100];
        int n;
        cin>>n; 
        while(n--){
            cin>>a;
            judge(a);
        }
        return 0;

  • 相关阅读:
    mac 下webstorm调节字体大小
    js等于符号的详解
    js运算符相关要点
    js字符串相关要点
    js变量的相关要点
    JS中的let变量
    测试浏览器是否支持JavaScript脚本
    document.write的时机
    Valid Parentheses
    Remove Nth Node From End of List
  • 原文地址:https://www.cnblogs.com/ma6174/p/2313307.html
Copyright © 2011-2022 走看看