数据结构 栈
来源:互联网 发布:网络安全法的基本特点 编辑:程序博客网 时间:2024/05/21 06:20
// stack.h
#ifndef _STACK_H
#define _STACK_H
#include <stdio.h>
#include<stdlib.h>
#include <tchar.h>
#include <iostream>
#include <stdexcept>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
template <class elem>
class stack
{
private:
elem *top;
elem *base;
int length;
int stacksize;
public:
stack();
bool IsEmpty();
elem GetTop();
void Push(elem e);
void Pop();
int StackLength();
void DestoryStack();
};
template <class elem>
stack<elem>::stack()
{
base=new elem[STACK_INIT_SIZE];
*base=NULL;
if(!base) exit (1);
top=base;
length=0;
stacksize=STACK_INIT_SIZE;
}
template <class elem>
bool stack<elem>::IsEmpty()
{
if(top==base)
{
return true;
}
else
{
return false;
}
}
template <class elem>
elem stack<elem>::GetTop()
{
if(IsEmpty())
{
exit (1);
}
else
{
return *(top-1);
}
}
template <class elem>
void stack<elem>::Push(elem e)
{
if(top-base>=stacksize)
{
if(!(base=(elem*)realloc(base,stacksize+STACKINCREMENT))) exit (1);
stacksize+=STACKINCREMENT;
}
if(IsEmpty()) ++top;
*top=e;
++top;
//++length;
}
template <class elem>
void stack<elem>::Pop()
{
if(IsEmpty()) exit(1);
--top;
*top=NULL;
//--length;
//return *top;
}
template <class elem>
int stack<elem>::StackLength()
{
elem *e;
e=base;
length=0;
while((++e)!=top)
{
//++e;
++length;
}
return length;
}
template <class elem>
void stack<elem>::DestoryStack()
{
delete[] base;
//delete[] top;
}
#endif
// main.cpp
#include "stdafx.h"
#include "stack.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"let enter stack world!"<<'/n';
cout<<"New stack"<<'/n';
stack<int> T;
for(int i=1;i<10;++i)
{
T.Push(i);
cout<<T.GetTop()<<'/t';
}
cout<<'/n'<<"This stack length is"<<'/t'<<T.StackLength()<<'/n';
cout<<"Let pop the top"<<'/n';
for(int j=1;j<10;j++)
{
cout<<T.GetTop()<<'/t';
T.Pop();
}
cout<<'/n'<<"This stack length is"<<'/t'<<T.StackLength()<<'/n';
T.DestoryStack();
system("pause");
return 0;
}
- 数据结构---栈
- 数据结构-栈
- 数据结构 栈
- 数据结构--栈
- 数据结构 栈
- 数据结构(栈)
- 数据结构栈
- 数据结构-栈
- 数据结构-----栈
- 数据结构-栈
- 【数据结构】栈
- 数据结构---->栈
- 数据结构---栈
- 数据结构--栈
- 数据结构----栈
- 数据结构--栈
- 【数据结构】 栈
- 数据结构 -- 栈
- 批量判断QQ是否在线,强迫聊天
- C# 用VB.NET函数库 实现全角半角转换
- 开机启动项的设置!
- 批量修改表、存储过程的所有者
- js 显示输入框输入的内容
- 数据结构 栈
- 现在的辅导员
- GPRS CMS&CME error
- js 限制输入框输入的内容
- IIS 中设置2个项目,其中在桌面的项目不能运行。老报错。 提示:服务器应用程序不可用 您试图在此 Web 服务器上访问的 Web 应用程序当前不可用
- modify the login prompt message
- 毕业5年决定你的命运(转载)
- 资源丰富的博客链接
- access日期和时间函数