基类:
/*
*FileName: Stack.h
*Creater: QianChenglong
*Date: 2011/10/22
*Comments: the abstract base class of stack
*/
#ifndef STACK_H
#define STACK_H
template<typename T>
class Stack
{
public:
Stack() {}
~Stack() {}
virtual bool push(const T& elem) =0;
virtual bool pop(T& elem) =0;
virtual T pop() =0;
virtual bool getTop(T& elem) const=0;
virtual T getTop() const=0;
virtual int length() const=0;
virtual bool isEmpty() const=0;
virtual void clear() =0;
virtual void outPut() const=0;
};
#endif
类定义:
#pragma once
#ifndef LINKEDSTACK_H
#define LINKEDSTACK_H
#include <iostream>
#include "Stack.h"
template<typename T> class LinkedStack;//预引用
template<typename T> std::ostream& operator<<(std::ostream& out,LinkedStack<T>& stack);
template<typename T>
class LinkedStack:public Stack<T>{//链栈类型定义
private:
struct ListNode;
ListNode* top;//栈顶指针
public:
LinkedStack();//进行初始化工作
~LinkedStack();//销毁栈
bool push(const T& elem);//elem进栈
bool pop(T& elem);//出栈,elem返回出栈的元素的值
T pop();//出栈,函数返回出栈元素的值
bool getTop(T& elem) const;//取栈顶,elem返回栈顶值
T getTop() const;//去栈顶,函数返回栈顶值
int length() const;//求栈长
bool isEmpty() const;//判空
void clear();//清栈
void outPut() const;//从栈顶到栈底输出元素
friend std::ostream& operator<< <T> (std::ostream& out,LinkedStack<T>& stack);
};
template<typename T>
struct LinkedStack<T>::ListNode{//链栈节点类型定义
T data;
ListNode* next;
ListNode();
ListNode(const T& _data);
};
#include "LinkedStack.cpp"
#endif
类定义:
#ifndef LINKEDSTACK_CPP
#define LINKEDSTACK_CPP
#include<iostream>
#include"LinkedStack.h"
///
template<typename T>
LinkedStack<T>::ListNode::ListNode()
:next(0)
{}
template<typename T>
LinkedStack<T>::ListNode::ListNode(T const & _data)
:data(_data)
{}
template<typename T>
LinkedStack<T>::LinkedStack()//默认构造函数
:top(0)
{}
//
template<typename T>
void LinkedStack<T>::clear(){//清栈
ListNode* work;
while(top){//当栈顶指针不为空时,执行
work=top;//栈顶指针赋给work
top=work->next;//新的栈顶为原来栈顶的下一个节点
delete work;//释放原来栈顶空间
}
}
//
template<typename T>
bool LinkedStack<T>::push(const T& elem){//利用头插法实现链栈;
ListNode* newNode=new ListNode(elem);//创建新节点
if(!newNode) return 0;
newNode->next=top;//新节点的next指向原来的栈顶节点
top=newNode;//新节点成为栈顶
return 1;
}
///
template<typename T>
bool LinkedStack<T>::pop(T& elem){//出栈
if(isEmpty())//若栈空,则失败
return 0;
elem=top->data;
top=top->next;//禅山新栈顶节点
return 1;
}
///
template<typename T>
T LinkedStack<T>::pop(){//出栈
if(isEmpty())
return top->data;//若为空,返回头结点内容;
ListNode* work=top;
top=top->next;
return work->data;
}
template<typename T>
bool LinkedStack<T>::getTop(T& elem) const{//取栈顶
if(isEmpty())
return 0;
elem=top->data;
return 1;
}
template<typename T>
T LinkedStack<T>::getTop() const{//取栈顶
if(isEmpty())
return top->data;
return top->data;
}
//
template<typename T>
int LinkedStack<T>::length() const{//求栈长
ListNode* work=top;
int length=0;
while(work){
++length;
work=work->next;
}
return length;
}
/
template<typename T>
bool LinkedStack<T>::isEmpty() const{//判空
return top==0;
}
//
template<typename T>
void LinkedStack<T>::outPut() const{//从栈顶到栈底的元素
ListNode* work=top;
while(work){
std::cout<<work->data<<' ';
work=work->next;
}
std::cout<<std::endl;
}
//
template<typename T>//显式的给模板函数指定类型实参
std::ostream& operator<< <T>(std::ostream& out,LinkedStack<T>& stack){//重载<<
typename LinkedStack<T>::ListNode* work=stack.top;
while(work){
out<<work->data<<' ';
work=work->next;
}
out<<std::endl;
return out;
}
/
template<typename T>
LinkedStack<T>::~LinkedStack(){//析构函数
clear();
}
#endif
功能测试:
#include<iostream>
#include "LinkedStack.h"
int main()
{
LinkedStack<char> Sa;
char elem;
std::cout<<Sa.length()<<' '<<Sa.isEmpty()<<std::endl;
while(std::cin>>elem)
Sa.push(elem);
Sa.outPut();
std::cout<<Sa.length()<<' '<<Sa.isEmpty()<<std::endl;
Sa.getTop(elem);
std::cout<<elem<<std::endl;
Sa.pop(elem);
std::cout<<Sa;
Sa.clear();
Sa.pop(elem);
return 0;
}