今天简单的实现了一下顺序栈,栈的核心函数是push(),top(),pop()。首先是我自己编写的栈,后面是STL标准库调用栈函数。
sqstack.h
//栈的简单实现lifo
#ifndef SQSTACK_H_
#define SQSTACK_H_
const int DEFAULT_SIZE=10;
#include <iostream>
using std::cout;
using std::endl;
template <typename T>
class Sqstack
{
protected:
int count;
int maxSize;
T *elem;
//辅助
void Init(int size);
bool Full();
public:
Sqstack(int size=DEFAULT_SIZE);
virtual ~Sqstack();
void Clear();
int Length() const;
bool IsEmpty();
//void GetElem(int position, T &e);
//Sqstack<T> &InsertElem(int position, T &e);
//Sqstack<T> &DeleteElem(int position, T e);
//Sqstack<T> &SetElem(int position, T &e);
T top();
void push(const T e);
T pop();
Sqstack(const Sqstack<T> ©);
Sqstack<T> &operator =(const Sqstack<T> ©);
};
template<typename T>
void Sqstack<T>::Init(int size)
{
maxSize=size;
if(elem!=NULL)
{
Clear();
}
elem=new T[maxSize];
count=0;
}
template <typename T>
bool Sqstack<T>::Full()
{
return Length()==maxSize;
}
template<typename T>
Sqstack<T>::Sqstack(int size)
{
elem=NULL;
Init(size);
}
template <typename T>
Sqstack<T>::~Sqstack()
{
Clear();
delete []elem;
}
template <typename T>
void Sqstack<T>::Clear()
{
count=0;
}
template <typename T>
int Sqstack<T>::Length() const
{
return count;
}
template <typename T>
bool Sqstack<T>::IsEmpty()
{
return count==0;
}
template <typename T>
T Sqstack<T>::top()
{
if(IsEmpty())
{
return NULL;
}
else
return elem[count-1];
}
template <typename T>
void Sqstack<T>::push(const T e)
{
if(Full())
{
cout<<"栈已满,无法压栈!"<<endl;
}
else
{
elem[count++]=e;
}
}
template <typename T>
T Sqstack<T>::pop()
{
if(IsEmpty())
{
cout<<"栈为空,弹不出来!";
return -1;
}
else
{
T e;
e=elem[count-1];
--count;
return e;
}
}
template <typename T>
Sqstack<T>::Sqstack(const Sqstack<T> ©)
{
Init(copy.maxSize);
for(int curPosition=0; curPosition<copy.count; curPosition++)
{
elem[curPosition]=copy.elem[curPosition];
count++;
}
}
template <typename T>
Sqstack<T> &Sqstack<T>::operator =(const Sqstack<T> ©)
{
if(this!=copy)
{
Init(copy.maxSize);
for(int curPosition=0; curPosition<copy.count; curPosition++)
{
elem[curPosition]=copy.elem[curPosition];
count++;
}
}
}
template <typename T>
std::ostream &operator<<(std::ostream &os, Sqstack<T> &Stack)
{
int Len=Stack.Length();
for(int curPosition=0; curPosition<Len; curPosition++)
{
cout<<Stack.pop()<<" ";
}
cout<<endl;
return os;
}
#endif
stacklist.cpp
// Stacklist.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "sqstack.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Sqstack<int> Stack;
Stack.push(1);
Stack.push(2);
Stack.push(3);
int a=Stack.top();
cout<<"取栈顶值为:"<<a<<endl;
//int e=Stack.pop();
//cout<<"弹出值为:"<<e<<endl;
//cout<<Stack;
Sqstack<int> copy(Stack);
Sqstack<int> copy1=copy;
cout<<"倒叙输出栈:"<<copy;
cout<<"倒叙输出栈:"<<copy1;
system("pause");
return 0;
}
接下来是调用STL标准库中的STACK实现:
// stack.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <stack>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
stack<int> s1;
for(int i=0; i<5; i++)
{
s1.push(i);
}
cout<<"s1.top is: "<<s1.top()<<endl;
cout<<"倒叙输出为:";
while(s1.size())
{
cout<<s1.top()<<" ";
s1.pop();//无返回值,仅仅是弹出数据
}
cout<<endl;
system("pause");
return 0;
}
结果略。