大二新开数据结构课了,我把我写的代码粘上来
//main.cpp
我的代码会参考网上很多大神的代码,但绝不是抄袭,还有..........起名字是个很大的问题,我英语水平不高啊,所以各位不要纠结于各个函数的名字啊
我感觉只要能写出前边的线性表,栈没有什么难的啊。。。只是pop push 嘛
上代码,错误处请指正!!!
// sqstack.h
//sequence stack
#include<iostream>
using namespace std;
template<typename T>
class SeqStack
{
public:
SeqStack();
~SeqStack();
bool IsEmpty();
bool IsFull();
bool Push( const T &elem);
T Pop();
int GetSize();
void Clear();
private:
T *pBase;
T *pTop;
int Size;
int MaxSize; //存有数据的最大尺寸,实际为MaxSize+1
static const int DEFAULTSIZE = 10;
};
template<typename T>
SeqStack<T>::SeqStack():Size(0),MaxSize(DEFAULTSIZE)
{
pBase = new T[ MaxSize + 1 ];
pTop = pBase;
}
template<typename T>
SeqStack<T>::~SeqStack()
{
delete[] pBase;
}
template<typename T>
bool SeqStack<T>::IsEmpty()
{
if( 0 == Size ){
return true;
}
return false;
}
template<typename T>
bool SeqStack<T>::IsFull()
{
if( Size == MaxSize){
return true;
}
return false;
}
template<typename T>
bool SeqStack<T>::Push(const T &elem)
{
if( IsFull() ){
return false;
}
++pTop;
*pTop = elem;
++Size;
return true;
}
template<typename T>
T SeqStack<T>::Pop() //默认为非空
{
T q;
q = *pTop;
--pTop;
--Size;
return q;
}
template<typename T>
int SeqStack<T>::GetSize()
{
return Size;
}
template<typename T>
void SeqStack<T>::Clear()
{
pTop = pBase;
Size = 0;
}
//main.cpp
//sequence stack
#include"head.h"
int main()
{
SeqStack<int> stack;
stack.Push( 44 );
stack.Push( 22 );
stack.Push( 77 );
stack.Push( 66 );
stack.Push( 55 );
cout<< stack.GetSize()<<endl;
cout<< stack.Pop()<<endl;
cout<< stack.Pop()<<endl;
stack.Clear();
cout<< stack.GetSize()<<endl;
return 0;
}