栈--链式存储

基类:

/*
*FileName: Stack.h
*Creater: QianChenglong
*Date: 2011/10/22
*Comments: the abstract base class of stack
*/
#ifndef STACK_H
#define STACK_H

template
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
#include "Stack.h"

template class LinkedStack;//预引用
template std::ostream& operator<<(std::ostream& out,LinkedStack& stack);

template
class LinkedStack:public Stack{//链栈类型定义
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<< (std::ostream& out,LinkedStack& stack);
};

template
struct LinkedStack::ListNode{//链栈节点类型定义
T data;
ListNode* next;

ListNode();
ListNode(const T& _data);
};

#include "LinkedStack.cpp"
#endif


类定义:

#ifndef LINKEDSTACK_CPP
#define LINKEDSTACK_CPP

#include
#include"LinkedStack.h"

///
template
LinkedStack::ListNode::ListNode()
:next(0)
{}


template
LinkedStack::ListNode::ListNode(T const & _data)
:data(_data)
{}


template
LinkedStack::LinkedStack()//默认构造函数
:top(0)
{}

//
template
void LinkedStack::clear(){//清栈
ListNode* work;
while(top){//当栈顶指针不为空时,执行
work=top;//栈顶指针赋给work
top=work->next;//新的栈顶为原来栈顶的下一个节点
delete work;//释放原来栈顶空间
}
}

//
template
bool LinkedStack::push(const T& elem){//利用头插法实现链栈;
ListNode* newNode=new ListNode(elem);//创建新节点
if(!newNode) return 0;
newNode->next=top;//新节点的next指向原来的栈顶节点
top=newNode;//新节点成为栈顶
return 1;
}

///
template
bool LinkedStack::pop(T& elem){//出栈
if(isEmpty())//若栈空,则失败
return 0;
elem=top->data;
top=top->next;//禅山新栈顶节点
return 1;
}

///
template
T LinkedStack::pop(){//出栈
if(isEmpty())
return top->data;//若为空,返回头结点内容;
ListNode* work=top;
top=top->next;
return work->data;
}


template
bool LinkedStack::getTop(T& elem) const{//取栈顶
if(isEmpty())
return 0;
elem=top->data;
return 1;
}


template
T LinkedStack::getTop() const{//取栈顶
if(isEmpty())
return top->data;
return top->data;
}

//
template
int LinkedStack::length() const{//求栈长
ListNode* work=top;
int length=0;
while(work){
++length;
work=work->next;
}
return length;
}

/
template
bool LinkedStack::isEmpty() const{//判空
return top==0;
}

//
template
void LinkedStack::outPut() const{//从栈顶到栈底的元素
ListNode* work=top;
while(work){
std::cout<data<<' ';
work=work->next;
}
std::cout< }

//
template//显式的给模板函数指定类型实参
std::ostream& operator<< (std::ostream& out,LinkedStack& stack){//重载<<
typename LinkedStack::ListNode* work=stack.top;
while(work){
out<data<<' ';
work=work->next;
}
out< return out;
}

/
template
LinkedStack::~LinkedStack(){//析构函数
clear();
}
#endif


功能测试:

#include
#include "LinkedStack.h"

int main()
{
LinkedStack Sa;
char elem;
std::cout< while(std::cin>>elem)
Sa.push(elem);
Sa.outPut();
std::cout< Sa.getTop(elem);
std::cout< Sa.pop(elem);
std::cout< Sa.clear();
Sa.pop(elem);

return 0;
}


你可能感兴趣的:(栈--链式存储)