您的位置:首页 > 理论基础 > 数据结构算法

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

2013-06-05 21:44 761 查看
#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.

*************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CC++ 算法 数据结构 C++