1.基础知识:堆栈可以实现很多的应用,递归的问题转化成非递归形式,在本质上也是堆栈的问题.它是一种"FILO"操作的数据结构,一般也有两种存储方式:数组跟链表实现形式,这里我给出了链表形式的堆栈模板,里面包括了基本的堆栈所有的操作,还有两个比较著名的应用例子,时间仓促,精力比较有限,关于迷宫老鼠还没有实现.尽请各位原谅,哈哈,我的目标是复习一下堆栈的基本操作及实现,已经达到,就不作进一步细化了.有兴趣的可以去试着写写.
2.源代码实现
///
// Book name : C++ structure and algorithm
// FileName : Stack.cpp
// Version : 1.0
// Author : Yangfei
// Date : 2010-04-08 21:33:55
// Comment : 堆栈类实现,应用:中缀到后缀表达式的转换,括号匹配,堆栈汉诺塔,
// 火车车厢重排,开关盒布线,离线等价类问题,迷宫老鼠
///
//链表形式的堆栈代码实现
template <class T>
class Stack
{
private:
CNode<T>* top;
int count;
public:
Stack():top(NULL),count(0){}
void push(const T data);
int pop();
int GetCount() const;
bool IsEmpty();
bool IsFull();
//输出堆栈中的当前元素
void Printstack();
T Top() const;
};
template <class T>
void Stack<T>::Printstack()
{
CNode<T>* tempNode=top;
while(tempNode!=NULL)
{
cout<<tempNode->data<<" ";
tempNode=tempNode->next;
}
}
template <class T>
void Stack<T>::push(const T data)
{
CNode<T>* newNode=new CNode<T>();
newNode->data=data;
newNode->next=NULL;
count++;
if(top==NULL)
top=newNode;
else
{
newNode->next=top;
top=newNode;
}
}
//pop方法要删除栈顶元素
template <class T>
int Stack<T>::pop()
{
CNode<T>* tempTop;
if(top==NULL)
return 0;
else
{
//cout<<top->data<<endl;
tempTop=top;
top=tempTop->next;
tempTop->next=NULL;
delete tempTop;
count--;
return 1;
}
}
template <class T>
bool Stack<T>::IsEmpty()
{
if(top==NULL)
return true;
return false;
}
template <class T>
bool Stack<T>::IsFull()
{
//检查堆栈是否已满
try
{
CNode<T>* newNode=new CNode<T>();
delete newNode;
return false;
}
catch(CNode<T>* newNode)
{
cout<<"Out of Memorry!"<<endl;
return true;
}
}
template <class T>
T Stack<T>::Top() const
{
assert(top!=NULL);
return top->data;
}
template <class T>
int Stack<T>::GetCount() const
{
return count;
}
//堆栈的应用:括号匹配,堆栈求解汉诺塔,火车车厢重排,开关盒布线,离线等价类问题,迷宫老鼠
//1.括号匹配问题
void parenthesesMatch()
{
Stack<char>* stack=new Stack<char>();
char ch;
while((ch=getchar())!='/n')
{
if(ch=='('||ch=='[')
stack->push(ch);
else if(ch==')'&&!stack->IsEmpty()&&stack->Top()=='(')
{
stack->pop();
}
else if(ch==']'&&!stack->IsEmpty()&&stack->Top()=='[')
stack->pop();
}
if(stack->IsEmpty())
cout<<"括号匹配!"<<endl;
else
cout<<"括号不匹配!"<<endl;
//for(int i=1;i<=stack->GetCount();i++)
// cout<<stack->Top()<<endl;
}
//2.使用堆栈求解汉诺塔,输出每次调动盘子后,塔的状态
//定义三个堆栈以每次存储塔的当前状态
class tower
{
friend void Hanio(int n);
private:
Stack<int>* sta[3];
public:
tower();
void Initstack0(int);
void hanio_stack(int,int,int,int);
void move(int x,int n,int y);
void show_status(int x,int n,int y);
};
tower::tower()
{
sta[0]=new Stack<int>();
sta[1]=new Stack<int>();
sta[2]=new Stack<int>();
}
void tower::Initstack0(int n)
{
for(int i=n;i>=1;i--)
sta[0]->push(i);
}
void tower::show_status(int x,int n,int y)
{
sta[x-1]->pop();
sta[y-1]->push(n);
for(int i=0;i<3;i++)
{
cout<<"stack["<<i<<"]:";
sta[i]->Printstack();
cout<<endl;
}
}
void tower::move(int x,int n,int y)
{
cout<<"将圆盘"<<n<<"从塔"<<x<<"移动到塔"<<y<<endl;
show_status(x,n,y);
}
void tower::hanio_stack(int n,int x,int y,int z)
{
if(n==1)
move(x,1,y);
else
{
hanio_stack(n-1,x,z,y);
move(x,n,y);
hanio_stack(n-1,z,y,x);
}
}
//友元函数调用类函数
void Hanio(int n)
{
tower tower1;
tower1.Initstack0(n);
tower1.hanio_stack(n,1,2,3);
}
int main()
{
//堆栈测试1.括号匹配
Stack<int> stack;
stack.push(1);
stack.push(2);
cout<<stack.Top()<<endl;
stack.pop();
stack.pop();
parenthesesMatch();
Hanio(3);
return 0;
}