資料結構 順序棧進行進位制轉換
阿新 • • 發佈:2019-01-29
SeqStack_main,cpp
#include"SeqStack.h"//引入宣告 template<class DataType> SeqStack<DataType>::SeqStack() { top=-1; //此時棧為空,初始化棧 } template<class DataType> void SeqStack<DataType>::Push(DataType x)//插入一個數x { if(top==StackSize-1) throw"上溢"; top++; data[top]=x; } template<class DataType> DataType SeqStack<DataType>::Pop() { DataType x; if(top==-1) throw"下溢"; x=data[top--]; return x; } template<class DataType> DataType SeqStack<DataType>::GetTop() { if(top!=-1) return data[top]; }//取棧頂元素 不修改棧頂指標 template<class DataType> int SeqStack<DataType>::Empty()//判空操作 { if(top==-1) return 1; else return 0;//棧非空 }
SeqStack.cpp
#include"SeqStack.h"//引入宣告 template<class DataType> SeqStack<DataType>::SeqStack() { top=-1; //此時棧為空,初始化棧 } template<class DataType> void SeqStack<DataType>::Push(DataType x)//插入一個數x { if(top==StackSize-1) throw"上溢"; top++; data[top]=x; } template<class DataType> DataType SeqStack<DataType>::Pop() { DataType x; if(top==-1) throw"下溢"; x=data[top--]; return x; } template<class DataType> DataType SeqStack<DataType>::GetTop() { if(top!=-1) return data[top]; }//取棧頂元素 不修改棧頂指標 template<class DataType> int SeqStack<DataType>::Empty()//判空操作 { if(top==-1) return 1; else return 0;//棧非空 }
SeqStack.h
#ifndef SeqStack_H #define SeqStack_H const int StackSize=10; template<class DataType>//定義模板類 class SeqStack { public: SeqStack(); ~SeqStack(){ } void Push(DataType x);//將元素x入棧 DataType Pop();//將元素彈出 DataType GetTop();//取棧頂元素(並不刪除 int Empty();//判斷棧是否為空 private: DataType data[StackSize];//存放元素的陣列 int top;//棧頂指標,指示棧頂元素在陣列中的下標 } ; #endif