zoukankan      html  css  js  c++  java
  • 7 线性表-栈-顺序存储的拓展-迎面增长的存储方式

    描述:
    设有两个栈,S1和S2,都采用顺序栈的存储方式。
    两个栈共享一个存储区:【0,……maxsize-1】
    目的:可以尽量利用空间,减少溢出的可能,采用栈顶相向、迎面增长的存储方式

    PS:要注意判断栈满栈空的检查

    #include<stdio.h>
    #include<stdlib.h>
    #include<iostream>
    using namespace std;
    #define maxsize 100
    
    /*栈结构的定义*/
    typedef struct
    {
        int stackk[maxsize];
        int top[2];//两个栈顶指针top[0]指向stackk左端,top[1]指向stackk右端
        int length[2];
    } stk;
    /*入栈*/
    /*i表示两个不同的栈,0表示左边的栈,1表示右边的栈
    左边的栈每入栈一个元素指针向右移动,右边的栈每入栈一个元素指针向左移动
    入栈操作前要判断栈是否为满
    */
    void initStackk(stk *S)
    {
        S->length[0]=0;
        S->length[1]=0;
        S->top[0]=-1;
        S->top[1]=maxsize;
    }
    int Push(int i,int x,stk *S)
    {
        if(i!=0&&i!=1)
        {
            cout<<"wrong stack number"<<endl;
            return 0;
        }
        else if(S->top[0]==(S->top[1]-1))
        {
            cout<<"the stack is filled with data";
            return 0;
        }
        if(i==0)
        {
            S->stackk[++S->top[0]]=x;
            S->length[i]++;
        }
        else
        {
            S->stackk[--S->top[1]]=x;
            S->length[i]++;
        }
        cout<<"Successful."<<endl;
        return 1;
    }
    int Pop(int i,int &x,stk *S)
    {
        if(i!=0&&i!=1)
        {
            cout<<"wrong stack number"<<endl;
            return 0;
        }
        if(S->top[i]==-1||S->top[i]==maxsize)
        {
            cout<<"Stack"<<i<<" is null.";
            return 0;
        }
        if(i==0)
        {
            x=S->stackk[S->top[i]--];
            S->length[i]--;
        }
        else
        {
            x=S->stackk[S->top[i]++];
            S->length[i]--;
        }
        cout<<"Successful."<<endl;
        return 1;
    }
    void showtheStack(stk *S)
    {
        cout<<"Stack0 : ";
        for(int i=0; i<S->length[0]; i++)
            cout<<S->stackk[i]<<" ";
        cout<<endl;
        cout<<"Stack1 : ";
        for(int i=maxsize-1,j=0; j<S->length[1]; j++,i--)
        {
            cout<<S->stackk[i]<<" ";
        }
        cout<<endl;
    }
    int main()
    {
        stk *S=(stk*)malloc(sizeof(stk));
        initStackk(S);
        cout<<"test function 'Push' by input 5 data."<<endl;
        int w=5;
        while(w--)
        {
            cout<<"input a data and which stack that you want to save";
            int a,b;
            cin>>a>>b;
    
            if(!Push(b,a,S))
            {
                w++;
            }
        }
        showtheStack(S);
        cout<<"test function 'Pop' by input the stack number."<<endl;
        w=3;
        while(w--)
        {
            int out=-1;
            cout<<"input a stack number (0/1):";
            int in;
            cin>>in;
            if(!Pop(in,out,S))
            {
                w++;
            }
            if(out>0)
            {
                cout<<"you Pop the data "<<out<<" successful from stack"<<in<<"."<<endl;
            }
        }
        showtheStack(S);
        return 0;
    }
  • 相关阅读:
    nignx重启启动关闭
    Tomcat日志配置
    误删除了mssql的表。 使用命令:drop table xxxx
    使用redis-cli定时执行指定命令
    在crontab中动态写日志
    循环日期的shell
    CountDownLatch、CyclicBarrier、Semaphore 区别
    log4j 知识点
    slf4j-api、slf4j-log4j12、log4j 之间是什么关系?
    HTTP协议中的长连接、短连接、长轮询、短轮询
  • 原文地址:https://www.cnblogs.com/AKsnoopy/p/7270155.html
Copyright © 2011-2022 走看看