?? stack.h
字號(hào):
# ifndef STACK_H
# define STACK_H
# include <iostream.h>
# include <stdlib.h>
# include <assert.h> //斷言
# include "Stacknode.h"
template <class Type>
class stack
{
public:
stack():top(NULL){} //構(gòu)造函數(shù)
~stack();
void push(const Type &item); //將數(shù)據(jù)item壓入棧中
Type pop(); //取出棧頂?shù)臄?shù)據(jù),同時(shí)top下移一位
Type gettop(); //取出棧頂?shù)臄?shù)據(jù),top的指向位置不變
void makeempty(); //將棧清空
int isempty() const; //判斷棧是否為空
private:
stacknode<Type> *top; //指向棧頂?shù)闹羔?};
template <class Type>
stack<Type>::~stack()
{
stacknode<Type> *p;
while(top!=NULL) //將棧內(nèi)的結(jié)點(diǎn)逐一刪去
{
p=top;
top=top->link;
delete p;
}
}
template <class Type>
void stack<Type>::push(const Type &item)
{
top=new stacknode<Type> (item,top); //數(shù)據(jù)item壓入棧中
}
template <class Type>
Type stack<Type>::pop()
{
assert(!isempty()); //斷言棧不空
stacknode<Type> *p=top;
Type retvalue =p->data; //取數(shù)據(jù)
top=top->link; //棧頂指針下移一位
delete p;
return retvalue; //返回棧頂?shù)臄?shù)據(jù)
}
template <class Type>
Type stack<Type>::gettop()
{
assert(!isempty());
return top->data; //直接取出棧頂?shù)臄?shù)據(jù)
}
template <class Type>
void stack<Type>::makeempty()
{
top=NULL; //直接將棧頂指針指向棧底
}
template <class Type>
int stack<Type>::isempty() const //判斷棧是否為空
{
return top==NULL;
}
# endif
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -