数据结构与算法之动态数组实现堆栈

#include <cstdio>
#include <iostream>
using namespace std;

typedef int T;
typedef struct tag
{
    int Top, MaxSize;
    T *elememtsp;
}STACK;
void creat(STACK *stack, int size)
{
    stack -> Top = -1;
    stack -> MaxSize = size - 1;
    stack -> elememtsp = new T;
}
int is_empty( STACK stack )
{
    if ( stack.Top == -1 )
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int is_full( STACK stack )
{
    if ( stack.Top == stack.MaxSize )
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int top( STACK stack )
{
    if ( stack.Top == -1 )
    {
        cout<<"the stack is empty! \n";
        return 65535;
    }
    else
    {
        return *(stack.elememtsp + stack.Top);
    }
}
void pop( STACK *stack )
{
    if ( 1 == is_empty(*stack) )
    {
        cout<<"the stack is empty!  \n";
    }
    else
    {
        stack -> Top--;
    }
}
void push( STACK *stack, T value )
{
    if ( 1 == is_full(*stack))
    {
        cout<<"the stack is full!  \n";
    }
    else
    {
        stack -> Top++;
        *(stack -> elememtsp + stack -> Top) = value;
    }
}
void destory( STACK *stack )
{
    stack -> Top = -1;
    stack -> MaxSize = 0;
    delete stack -> elememtsp;
}
void printstack( STACK stack )
{
    int i = 0, count;
    count = stack.Top;
    if ( stack.Top == -1 )
    {
        cout<<"the stack is empty! \n";
    }
    else
    {
        cout<<"the number of the stack is: "<<stack.Top + 1<<'\n';
        for ( i; i <= stack.Top; i++ )
        {
            cout<<"the "<<i<<" number is "<<*(stack.elememtsp + i)<<'\n';
        }
    }
}
//test the functions
int  main()
{
    STACK stack;
    creat(&stack, 512);
    //is_empty(stack);
    pop(&stack);
    for(int i=1991;i<2014;i++)
        push(&stack, i);
    printstack(stack);
    for(int j=0;j<10;j++)
        pop(&stack);
    push(&stack, 1949);
    printstack(stack);
}
/************************************************
运行结果如下:
the stack is empty!
the number of the stack is: 23
the 0 number is 1991
the 1 number is 1992
the 2 number is 1993
the 3 number is 1994
the 4 number is 1995
the 5 number is 1996
the 6 number is 1997
the 7 number is 1998
the 8 number is 1999
the 9 number is 2000
the 10 number is 2001
the 11 number is 2002
the 12 number is 2003
the 13 number is 2004
the 14 number is 2005
the 15 number is 2006
the 16 number is 2007
the 17 number is 2008
the 18 number is 2009
the 19 number is 2010
the 20 number is 2011
the 21 number is 2012
the 22 number is 2013
the number of the stack is: 14
the 0 number is 1991
the 1 number is 1992
the 2 number is 1993
the 3 number is 1994
the 4 number is 1995
the 5 number is 1996
the 6 number is 1997
the 7 number is 1998
the 8 number is 1999
the 9 number is 2000
the 10 number is 2001
the 11 number is 2002
the 12 number is 2003
the 13 number is 1949

Process returned 0 (0x0)   execution time : 0.047 s
Press any key to continue.

*************************************************/


#include <cstdio>
#include <iostream>
using namespace std;

typedef int T;
typedef struct tag
{
    int Top, MaxSize;
    T *elememtsp;
}STACK;
void creat(STACK *stack, int size)
{
    stack -> Top = -1;
    stack -> MaxSize = size - 1;
    stack -> elememtsp = new T;
}
int is_empty( STACK stack )
{
    if ( stack.Top == -1 )
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int is_full( STACK stack )
{
    if ( stack.Top == stack.MaxSize )
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int top( STACK stack )
{
    if ( stack.Top == -1 )
    {
        cout<<"the stack is empty! \n";
        return 65535;
    }
    else
    {
        return *(stack.elememtsp + stack.Top);
    }
}
void pop( STACK *stack )
{
    if ( 1 == is_empty(*stack) )
    {
        cout<<"the stack is empty!  \n";
    }
    else
    {
        stack -> Top--;
    }
}
void push( STACK *stack, T value )
{
    if ( 1 == is_full(*stack))
    {
        cout<<"the stack is full!  \n";
    }
    else
    {
        stack -> Top++;
        *(stack -> elememtsp + stack -> Top) = value;
    }
}
void destory( STACK *stack )
{
    stack -> Top = -1;
    stack -> MaxSize = 0;
    delete stack -> elememtsp;
}
void printstack( STACK stack )
{
    int i = 0, count;
    count = stack.Top;
    if ( stack.Top == -1 )
    {
        cout<<"the stack is empty! \n";
    }
    else
    {
        cout<<"the number of the stack is: "<<stack.Top + 1<<'\n';
        for ( i; i <= stack.Top; i++ )
        {
            cout<<"the "<<i<<" number is "<<*(stack.elememtsp + i)<<'\n';
        }
    }
}
//test the functions
int  main()
{
    STACK stack;
    creat(&stack, 512);
    //is_empty(stack);
    pop(&stack);
    for(int i=1991;i<2014;i++)
        push(&stack, i);
    printstack(stack);
    for(int j=0;j<10;j++)
        pop(&stack);
    push(&stack, 1949);
    printstack(stack);
}
/************************************************
运行结果如下:
the stack is empty!
the number of the stack is: 23
the 0 number is 1991
the 1 number is 1992
the 2 number is 1993
the 3 number is 1994
the 4 number is 1995
the 5 number is 1996
the 6 number is 1997
the 7 number is 1998
the 8 number is 1999
the 9 number is 2000
the 10 number is 2001
the 11 number is 2002
the 12 number is 2003
the 13 number is 2004
the 14 number is 2005
the 15 number is 2006
the 16 number is 2007
the 17 number is 2008
the 18 number is 2009
the 19 number is 2010
the 20 number is 2011
the 21 number is 2012
the 22 number is 2013
the number of the stack is: 14
the 0 number is 1991
the 1 number is 1992
the 2 number is 1993
the 3 number is 1994
the 4 number is 1995
the 5 number is 1996
the 6 number is 1997
the 7 number is 1998
the 8 number is 1999
the 9 number is 2000
the 10 number is 2001
the 11 number is 2002
the 12 number is 2003
the 13 number is 1949

Process returned 0 (0x0)   execution time : 0.047 s
Press any key to continue.

*************************************************/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值