数据结构学习笔记2-堆栈

堆栈是一种后进先出(last in first out,LIFO)的线性数据结构。

1.堆栈的抽象类

template <class T>
class Stack
{
public:
	virtual bool IsEmpty() const=0;
	virtual bool IsFull() const=0;
	virtual bool Top(T &x) const=0;
	virtual bool Push(T x)=0;
	virtual bool Pop()=0;
	virtual void Clear()=0;
};

2.堆栈的顺序表示

#include "stack.h"

template <class T>
class SeqStack:public Stack<T>
{
private:
	T *s;
	int top;
	int maxTop;
public:
	SeqStack(int mTop);
	~SeqStack();
	bool IsEmpty() const;
	bool IsFull() const;
	bool Top(T &x) const;
	bool Push(T x);
	bool Pop();
	void Clear();
	void Print();
};

#include "seqstack.h"

template <class T>
SeqStack<T>::SeqStack(int mTop)
{
	maxTop=mTop;
	top=-1;
	s=new T[maxTop];
}

template <class T>
SeqStack<T>::~SeqStack()
{
	delete [] s;
}

template <class T>
bool SeqStack<T>::IsEmpty() const
{
	if(top<0)
	{
		//cout<<"the seqstack is empty"<<endl;
		return true;
	}
	else
	{
		//cout<<"the seqstack is not empty"<<endl;
		return false;
	}
}

template <class T>
bool SeqStack<T>::IsFull() const
{
	if(top>=maxTop-1)
	{
		//cout<<"the seqstack is full"<<endl;
		return true;
	}
	else
	{
		//cout<<"the seqstack is not full"<<endl;
		return false;
	}
}

template <class T>
bool SeqStack<T>::Top(T &x) const
{
	if(this->IsEmpty())
	{
		cout<<"the seqstack is empty"<<endl;
		return false;
	}
	else 
	{
		x=s[top];
		return true;
	}
}

template <class T>
bool SeqStack<T>::Push(T x)
{
	if(this->IsFull())
	{
		cout<<"the seqstack is full"<<endl;
		return false;
	}
	else
	{
		s[++top]=x;
		return true;
	}
}

template <class T>
bool SeqStack<T>::Pop()
{
	if(this->IsEmpty())
	{
		cout<<"the seqstack is empty"<<endl;
		return false;
	}
	else
	{
		top--;
		return true;
	}
}

template <class T>
void SeqStack<T>::Clear()
{
	top=-1;
}

template <class T>
void SeqStack<T>::Print()
{
	for(int j=0;j<top+1;j++)
	{
		cout<<s[j]<<' ';
	}
	cout<<endl;
}

3.堆栈的链接表示

#include "stack.h"

template <class T> class LinkStack;
template <class T>
class Node
{
private:
	Node<T> *link;
	T element;
	friend class LinkStack<T>;
};

template <class T>
class LinkStack:public Stack<T>
{
private:
	Node<T> *top;
public:
	LinkStack();
	~LinkStack();
	bool IsEmpty() const;
	//bool IsFull() const;
	bool Top(T &x) const;
	bool Push(T x);
	bool Pop();
	void Clear();
	void Print();
};

#include "linkstack.h"

template <class T>
LinkStack<T>::LinkStack()
{
	top=NULL;
}

template <class T>
LinkStack<T>::~LinkStack()
{
	Node<T> *p;
	while(top)
	{
		p=top;
		top=top->link;
		delete p;
	}
	//delete top;
}

template <class T>
bool LinkStack<T>::IsEmpty() const
{
	return top==NULL;
}

template <class T>
bool LinkStack<T>::Top(T &x) const
{
	if(top)
	{
		x=top->element;
		return true;
	}
	else
		return false;
}

template <class T>
bool LinkStack<T>::Push(T x)
{
	Node<T> *p=new Node<T>;
	p->element=x;
	p->link=top;
	top=p;
	return true;
}

template <class T>
bool LinkStack<T>::Pop()
{
	if(top)
	{
		Node<T> *p=top;
		top=top->link;
		delete p;
		return true;
	}
	else
		return false;
}

template <class T>
void LinkStack<T>::Clear()
{
	Node<T> *p;
	while(top)
	{
		p=top;
		top=top->link;
		delete p;

	}
}

template <class T>
void LinkStack<T>::Print()
{
	Node<T> *p=top;
	while(p)
	{
		cout<<p->element<<' ';
		p=p->link;
	}
	cout<<endl;
}

【欢迎读者交流批评指正~】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值