- 说明
- 基于顺序表实现的顺序栈
- 基于链表实现的链式栈
一、说明:
本文中基于顺序表实现的顺序栈中top指的是栈中第一个空闲位置,基于链表实现的链式栈中top指向链式栈中第一个结点(栈顶)的指针,无头结点。
二、基于顺序表实现的顺序栈
Stack.h
#include<iostream>
using namespace std;
#ifndef _Stack
#define _Stack
namespace wangzhe
{
template<typename E>
class Stack
{
private:
void operator = (const Stack&) {}
Stack(const Stack&) {}
public:
Stack() {}
virtual ~Stack() {}
virtual void clear() = 0;//清空栈
virtual void push(const E& it) = 0;//压栈
virtual E pop() = 0;//出栈
virtual const E& topValue() const = 0;//返回栈顶元素
virtual int length() const = 0;//返回元素个数
};
}
#endif
AStack.h
#include<iostream>
using namespace std;
#include"Stack.h"
#ifndef _AStack
#define _AStack
namespace wangzhe
{
template<typename E>
class AStack:public Stack<E>
{
private:
int maxSize;//最大容量
int top;//栈顶指针,这里指的是栈中第一个空闲位置
E *listArray;//表
public:
AStack(int size);
~AStack();
void clear();
void push(const E& it);
E pop();
const E& topValue() const;
int length() const;
};
}
#endif
AStack.cpp
#include<iostream>
using namespace std;
#include"AStack.h"
namespace wangzhe
{
template<typename E>
AStack<E>::AStack(int size)
{
maxSize=size;
top=0;
listArray=new E[size];
}
template<typename E>
AStack<E>::~AStack()
{
delete [] listArray;
}
template<typename E>
void AStack<E>::clear()
{
top=0;
}
template<typename E>
void AStack<E>::push(const E& it)
{
if(top==maxSize)
{
cout<<"Stack is full"<<endl;
return;
}
listArray[top++]=it;
}
template<typename E>
E AStack<E>::pop()
{
if(top==0)
{
cout<<"Stack is empty"<<endl;
return -1;//用-1作为出错的返回值,并不严谨
}
return listArray[--top];
}
template<typename E>
const E& AStack<E>::topValue() const
{
if(top==0)
{
cout<<"Stack is empty"<<endl;
return -1;//用-1作为出错的返回值,并不严谨
}
return listArray[top-1];
}
template<typename E>
int AStack<E>::length() const
{
return top;
}
}
main.cpp
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#include"AStack.h"
#include"AStack.cpp"
using namespace wangzhe;
int main(int argc, char** argv)
{
AStack<int> a(10010);
a.push(2);
a.push(3);
a.push(4);
cout<<a.topValue()<<endl;
a.pop();
cout<<a.topValue()<<endl;
cout<<a.pop()<<endl;
return 0;
}
三、基于链表实现的链式栈
Link.h
#include <iostream>
using namespace std;
#ifndef _Link
#define _Link
namespace wangzhe
{
template<typename E>
class Link
{
public:
E element;
Link *next;
Link(const E& elemval,Link* nextval =NULL)
{
element=elemval;
next=nextval;
}
Link(Link* nextval=NULL)
{
next=nextval;
}
};
}
#endif
Stack.h
同顺序栈
LStack.h
#include<iostream>
using namespace std;
#include"Link.h"
#include"Stack.h"
#ifndef _LStack
#define _LStack
namespace wangzhe
{
template<typename E>
class LStack:public Stack<E>
{
private:
Link<E>* top;
int size;
public:
LStack(int size);
~LStack();
void clear();
void push(const E& it);
E pop();
const E& topValue() const;
int length() const;
};
}
#endif
LStack.cpp
#include<iostream>
using namespace std;
#include"LStack.h"
namespace wangzhe
{
template<typename E>
LStack<E>::LStack(int size)
{
top=NULL;
size=0;
}
template<typename E>
LStack<E>::~LStack()
{
clear();
}
template<typename E>
void LStack<E>::clear()
{
while(top!=NULL)
{
Link<E>* temp=top;
top=top->next;
delete temp;
}
size=0;
}
template<typename E>
void LStack<E>::push(const E& it)
{
top=new Link<E>(it,top);
size++;
}
template<typename E>
E LStack<E>::pop()
{
if(top==NULL)
{
cout<<"Stack is empty"<<endl;
return -1;//用-1作为出错的返回值,并不严谨
}
E it=top->element;
Link<E>* temp=top->next;
delete top;
top=temp;
size--;
return it;
}
template<typename E>
const E& LStack<E>::topValue() const
{
if(top==NULL)
{
cout<<"Stack is empty"<<endl;
return -1;//用-1作为出错的返回值,并不严谨
}
return top->element;
}
template<typename E>
int LStack<E>::length() const
{
return size;
}
}
main.cpp
同顺序栈