您的位置:首页 > 其它

个人学习记录<2>stack类

2018-03-01 15:25 295 查看
栈(stack):顺序存储结构,最大特点是先进后出(FILO)
栈分类:
1.数组栈(静态栈)
2.链表栈(栈元素是节点)
栈用处:
1.表达式解析 :3*2+5*4
2.递归实现
#include <iostream>
using namespace std;
template<class T>
class Stack;
//栈节点类
template<class T>
class Snode
{
friend class Stack<T>;
public:
Snode(T _date)
{
date=_date;
next=NULL;
}
private:
T date;
Snode * next;
};
//栈类
template<class T>
class Stack
{
public:
Stack()
{
Max=10;
count=0;
top=NULL;
}
bool Is_empty()
{
if(NULL==top)
{
return true;
}
return false;
}
bool Is_full()
{
if(count==Max)
{
return true;
}
return false;
}
void Push(T _value)
{
if(!Is_full())
{
Snode<T> * Newnode=new Snode<T>(_value);
Newnode->next=top;
top=Newnode;
Newnode=NULL;
count++;
}
else
{
cout<<"stack is full..."<<endl;
}
}
void Pop()
{
if(!Is_empty())
{
Snode<T> * p_st_node=top;
top=top->next;
cout<<"pop:"<<p_st_node->date<<endl;
delete(p_st_node);
p_st_node=NULL;
count--;
}
else
{
cout<<"stack is empty"<<endl;
}
}
private:
int Max;
int count;
Snode<T> * top;
};
int main(void)
{
Stack<int> st;
st.Push(1);
st.Push(3);
st.Push(2);
st.Push(4);

st.Pop();
st.Pop();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: