数据结构与算法之动态数组实现堆栈
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.
*************************************************/
#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.
*************************************************/
相关文章推荐
- 数据结构与算法-----堆栈-使用数组(顺序结构)实现
- 用数组实现js中的堆栈 或者队列数据结构
- 【数据结构与算法】数组应用2:矩阵转置(Java实现)
- 数据结构与算法-----堆栈-使用链表(链式结构)实现
- 数据结构之动态数组实现
- [数据结构与算法] : 栈的数组实现
- 【数据结构与算法基础】以数组实现的循环队列 / Circular Queue implemented by array
- 算法(第四版) 能够动态调整数组大小的队列数据结构
- 【数据结构与算法】(一) c 语言实现数组的简单操作
- 【数据结构与算法】数组应用3:稀疏矩阵压缩(Java实现)
- 数据结构与算法学习笔记——链表部分实现(数组形式)
- 数据结构数组相关算法和螺旋,蛇形,拉丁矩阵的实现
- 数据结构与算法-----队列-使用数组(顺序结构)实现
- 【数据结构与算法】数组应用1:矩阵乘法(Java实现)
- Python 数据结构与算法——二叉搜索树的实现
- 数据结构与算法之动态规划
- clojure 数据结构与算法 第一篇 lz77算法的实现
- 【数据结构作业一】写出顺序表的结构体类型定义及查找、插入、删除算法,并以顺序表作存储结构,实现线性表的插入、删除
- 数据结构基础之数组实现线性表各种操作(二)
- 【数据结构与算法】二叉排序树C实现(含完整源码)