data structure--Stack(基于数组实现)

来源:互联网 发布:头像源码怎么用 编辑:程序博客网 时间:2024/06/06 00:18
#include<iostream>using namespace std;const int maxstack=10;template <class T>class ArrayStack{public:ArrayStack();T& top();void pop();void push(T&);bool IsEmpty()  const;bool full() const;void clear();int size() const;private:    int current;T array[maxstack];};template <class T>ArrayStack<T>::ArrayStack()/*Pre: None  Post: The stack is initialized to be empty.*/{  current=-1;}template <class T>void ArrayStack<T>::push(T &item)/*Pre:None  Post:If the stack is not full,item is added to the top of the ArrayStack. Else,the stack is left unchanged.*/{if(current<maxstack-1)   array[++current]=item;}template <class T>void ArrayStack<T>::pop()/*Pre:None  Post:If the stack is not empty,the top of the ArrayStack is removed. Else,the stack is left unchanged.*/{if(!IsEmpty())   current--;}template <class T>T& ArrayStack<T>::top()/*Pre:None  Post:If the stack is not empty,the top of the ArrayStack is returned.*/{     if(!IsEmpty())   return array[current]; }template <class T>bool ArrayStack<T>::IsEmpty() const/*Pre:None  Post:If the stack is empty,true is returned.Otherwise,false is returned*/{     if(current==-1)      return 1;   else     return 0;}template <class T>bool ArrayStack<T>::full() const/*Pre:None  Post:If the stack is full,true is returned.Otherwise,false is returned*/{return current==9;}template <class T>int ArrayStack<T>::size() const/*Pre:None  Post:Return the number of entries in the stack.*/{return (current+1);}template <class T>void ArrayStack<T>::clear()/*Pre:None  Post:Reset the stack to be empty*/{current=-1;}

0 0
原创粉丝点击