⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stack.h

📁 大二半年的数据结构上机实验。包括链表
💻 H
字号:
#ifndef  _STACK_H_
#define  _STACK_H_
#include<iostream.h>
#include<stdlib.h>
template <class T>
class Stack{
public:
  Stack(){};
  void StackCreat();
  bool IsEmpty() const;
  bool IsFull() const;
  const T & Top() const;
  void MakeEmpty() {top=-1;}
  void Pop();
  void Push(const T & x);
  T Topandpop();
  void Printlist();
  
private:
  int MaxSize;
  int top;
  T *theArray;
};
#endif

template <class T>
void Stack<T>::StackCreat()
{int i;
 cout<<"请输入要建立的顺序栈的长度"<<endl;
 cin>>i;
 MaxSize=i;
 theArray=new T[MaxSize]; 
 top=-1;
}

template <class T>
bool Stack<T>::IsEmpty() const
{
	return top==-1;
}

template <class T>
bool Stack<T>::IsFull() const
{
   return top==MaxSize-1;
}

template <class T>
void Stack<T>::Push(const T & a)
{
  if(IsFull())  
   {cout<<"the stack is full"<<endl;
    exit(0);
   }
  theArray[++top]=a;
}

template <class T>
void Stack<T>::Pop()
{
  if(IsEmpty()) 
  {cout<<"the stack is empty"<<endl;
   exit(0);
  }
  top--;
}

template <class T>
T  Stack<T>::Topandpop()
{
  if(IsEmpty()) 
   {cout<<"the stack is empty"<<endl;
     exit(0);
   }
  return theArray[top--];
}
  
template <class T>
const T & Stack<T>::Top() const
{
  if(IsEmpty()) throw Underflow;
  return theArray[top];
}

template<class T>
void Stack<T>::Printlist()
{int i;
 i=top;
 while(i!=-1)
  {cout<<theArray[i]<<endl;
     i--;
  }
}

    
    
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -