csd1301/tam_0125
eg: stack.cpp //顺序表式堆栈、
#include
using namespace std;
class Stack{ //堆栈类
public:
//构造函数
Stack(){}
//析构函数
~Stack(){}
//压入数据用push()
void push (int data){}
//弹出数据用pop()
void pop (int data){}
//判空empty()
bool empty(void){}
pravate:
class OverFlow:public exception{};//抛出异常类,
class UnderFlow:public exception{};
int* m_data; //数据指针
size_t m_size; //堆栈的容量大小
size_t m_top;//堆栈的栈顶
};
int main(void){
try{}
catch(){}
return 0;
}
eg: lstack.cpp //链表式的堆栈;
#inclide
using namespace std;
class Stack{
public:
Stack(){}
~Stack(){}
//压入数据
void push(int data){}
//弹出数据
int pop(void){}
//判空
bool empty(){}
private:
//堆栈下益(异常处理)
class UnderFlow:public exception{};
//节点类
class Node{};
Node* m_top;
};
int main(){
try{}
catch{}
return 0;
}
eg: queue.cpp //(顺序表式队列)
#include
using namespace std;
class queue{
public:
//构造函数初始化分配内存
queue(size_t size = 5):m_data(new int[size]),m_size(size),m_rear(0),m_front(0),m_count(0){}
//析构函数释放内存空间
~queue(void){
if(m_data){
delete[] m_data;
m_data = NULL;
}
}
//压入数据
void push(int data){}
//弹出数据
int pop(void){}
bool empty(void){}
private:
class OverFlow: public exception{
const char* what(void) const throw(){return "队列上溢";}
};//上溢异常
class UnderFlow: public exception{
const char* what(void) const throw(){return "队列下益"}
};//下益异常
int* m_data; //数组
size_t size; //容量
size_t m_rear; //后端(压入)
size_t m_front; //前端(弹出)
size_t m_count; //计数
};
int main(void){
try{}
catch(){}
return 0;
}
eg: lqueue.cpp //链表式队列
#include
using namespace std;
class Queue{
public:
//构造函数
Queue{};
//析构函数
~Queue{};
//压入
void push(int data){}
//弹出
int pop(void){}
//判空
bool empty(){}
private:
//异常处理类
class UnderFlow: public exception{};
//结点类
class Node{};
Node* m_front; //前端
Node* m_rear; //后端
};
int main(void){
try{} //关键字try检测异常代码块的
catch{} //关键字catch用来填写异常代码
return 0;
}
eg: list.cpp 链表
#include
using namespace std;
class List{
public:
//构造
List(){}
//析构释放剩余结点
~List(){}
//追加
void append(int data){}
//插入
void insert(int data){}
//删除/移除
void remove(size_t index){}
//正遍历
void forward(void){}
//反遍历
void backward(void){}
//伪随机访问
int& operator[] (size_t index){}
//获取元素数量
size_t size(void) const{}
private:
//链表越界
class OverBound:public exception{};
//结点类
class Node{
public:
Node(){}
int m_data;
Node* m_prev; //前指针
Node* m_next; //后指针
};
Node* m_head;
Node* m_tail;
};
int main(void){
try{}
catch{}
return 0;
}