参考答案——顺序栈类的实现
sq_Stack.h头文件
#ifndef sq_Stack_H
#define sq_Stack_H
#include <iostream>
using namespace std;
template<class T>
class sq_Stack
{
private:
int m_volume;
int m_topIdx;
T * m_stack;
public:
sq_Stack(int);
~sq_Stack();
void prt_sq_Stack();
int flag_sq_Stack();
void ins_sq_Stack(T);
T del_sq_Stack();
T read_sq_Stack();
};
template<class T>
sq_Stack<T>::sq_Stack(int m)
{
cout << "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" << endl;
if (m < 1)
{
cout << "由于输入的栈容量值 m < 1,将栈容量设置为1。" << endl;
m = 1;
}
m_volume = m;
m_stack = new T[m_volume];
m_topIdx = 0;
cout << "Construct a sq_Stack with volume " << m_volume << ". " << endl;
cout << "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" << endl;
cout << endl;
return;
}
template<class T>
sq_Stack<T>::~sq_Stack()
{
m_volume = 0;
m_topIdx = 0;
delete [] m_stack;
m_stack = NULL;
cout << "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD" << endl;
cout << "Destruct the sq_Stack." << endl;
cout << "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD" << endl;
cout << endl;
return;
}
template<class T>
void sq_Stack<T>::prt_sq_Stack()
{
cout << "=========================================================" << endl;
cout << "print the sq_LList with m_topIdx = " << m_topIdx << endl;
for (int i = m_topIdx; i > 0; i--)
cout << m_stack[i - 1] << endl;
cout << "=========================================================" << endl;
cout << endl;
return;
}
template<class T>
int sq_Stack<T>::flag_sq_Stack()
{
if (m_topIdx == m_volume) return(-1);
if (m_topIdx == 0) return(0);
return(1);
}
template<class T>
void sq_Stack<T>::ins_sq_Stack(T x)
{
cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
if (m_topIdx == m_volume)
{
cout << "Stack Overflow!" << endl;
}
else
{
m_topIdx++;
m_stack[m_topIdx-1] = x;
cout << "Push the element " << x << " at top index " << m_topIdx << ". " << endl;
}
cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
cout << endl;
return;
}
template<class T>
T sq_Stack<T>::del_sq_Stack()
{
cout << "---------------------------------------------------------" << endl;
if (m_topIdx == 0)
{
cout << "Stack underflow!" << endl;
cout << "---------------------------------------------------------" << endl;
cout << endl;
return (0);
}
T y;
y = m_stack[m_topIdx - 1];
m_topIdx--;
cout << "Pop the element "<< y << " at top index " << m_topIdx + 1 << ". " << endl;
cout << "---------------------------------------------------------" << endl;
cout << endl;
return (y);
}
template<class T>
T sq_Stack<T>::read_sq_Stack()
{
if (m_topIdx == 0)
{
cout << "Stack underflow!" << endl;
return (0);
}
return (m_stack[m_topIdx-1]);
}
#endif
mani.cpp测试主函数
#include "sq_Stack.h"
int main()
{
sq_Stack<int> s(10);
s.ins_sq_Stack(50);
s.ins_sq_Stack(60);
s.ins_sq_Stack(70);
s.ins_sq_Stack(80);
s.ins_sq_Stack(90);
s.ins_sq_Stack(100);
cout << "输出栈顶指针与栈中元素: " << endl;
s.prt_sq_Stack();
cout << "栈顶元素: " << s.read_sq_Stack() << endl << endl;
cout << "输出退栈元素: " << endl;
s.del_sq_Stack();
s.del_sq_Stack();
s.del_sq_Stack();
cout << "再次输出栈顶指针与栈中元素:" << endl;
s.prt_sq_Stack();
return 0;
}
运行结果
参考答案——顺序队列类的实现
sq_Queue.h头文件
#ifndef sq_Queue_h
#define sq_Queue_h
#include <iostream>
using namespace std;
template<class T>
class sq_Queue
{
private:
int m_volume;
int m_front;
int m_rear;
int m_flagNonEmp;
T * m_sqQueue;
public:
sq_Queue(int);
~sq_Queue();
void prt_sq_Queue();
int flag_sq_Queue();
void ins_sq_Queue(T);
T del_sq_Queue();
};
template<class T>
sq_Queue<T>::sq_Queue(int m)
{
cout << "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" << endl;
if (m<1)
{
cout << "As m is less than 1, it is set to be 1. " << endl;
m = 1;
}
m_volume = m;
m_sqQueue = new T[m_volume];
m_front = m_volume;
m_rear = m_volume;
m_flagNonEmp = 0;
cout << "Construct a sq_Queue with volume " << m_volume << ". " << endl;
cout << "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" << endl;
cout << endl;
}
template<class T>
sq_Queue<T>::~sq_Queue()
{
delete [] m_sqQueue;
m_sqQueue = NULL;
cout << "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD" << endl;
cout << "Destruct the sq_Queue." << endl;
cout << "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD" << endl;
cout << endl;
}
template<class T>
void sq_Queue<T>::prt_sq_Queue()
{
cout << "=========================================================" << endl;
cout << "print the sq_Queue " << endl;
cout << "m_front = " << m_front << endl;
cout << "m_rear = " << m_rear << endl;
if (m_flagNonEmp == 0)
{
cout << "空队列!" << endl;
cout << "=========================================================" << endl;
cout << endl;
return;
}
int i = m_front;
do
{
i++;
if (i == m_volume + 1)
i = 1;
cout << m_sqQueue[i-1] << endl;
}while(i != m_rear);
cout << "=========================================================" << endl;
cout << endl;
return;
}
template<class T>
int sq_Queue<T>::flag_sq_Queue()
{
if ((m_flagNonEmp == 1) && (m_rear == m_front)) return(-1);
if (m_flagNonEmp == 0) return(0);
return(1);
}
template<class T>
void sq_Queue<T>::ins_sq_Queue(T x)
{
cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
if ((m_flagNonEmp == 1) && (m_rear == m_front))
{
cout << "Queue overflow! " << endl;
}
else
{
m_rear++;
if (m_rear == m_volume + 1) m_rear = 1;
m_sqQueue[m_rear - 1] = x;
m_flagNonEmp = 1;
cout << "Insert the element " << x << " at rear index " << m_rear << ". " << endl;
}
cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
cout << endl;
return;
}
template<class T>
T sq_Queue<T>::del_sq_Queue()
{
cout << "---------------------------------------------------------" << endl;
if (m_flagNonEmp == 0)
{
cout << "Queue underflow! " << endl;
cout << "---------------------------------------------------------" << endl;
cout << endl;
return(0);
}
m_front++;
if (m_front == m_volume + 1) m_front = 1;
T y = m_sqQueue[m_front - 1];
if (m_front == m_rear) m_flagNonEmp = 0;
cout << "Delete the element "<< y << " at front index " << m_front << ". " << endl;
cout << "---------------------------------------------------------" << endl;
cout << endl;
return (y);
}
#endif
mani.cpp测试主函数
#include "sq_Queue.h"
int main()
{
sq_Queue<int> q(10);
cout << "输出队头队尾指针以及队中元素: " << endl;
q.prt_sq_Queue();
q.ins_sq_Queue(50);
q.ins_sq_Queue(60);
q.ins_sq_Queue(70);
q.ins_sq_Queue(80);
q.ins_sq_Queue(90);
q.ins_sq_Queue(100);
cout << "输出队头队尾指针以及队中元素: " << endl;
q.prt_sq_Queue();
cout << "输出退队元素: " << endl;
q.del_sq_Queue();
q.del_sq_Queue();
q.del_sq_Queue();
cout << "输出队头队尾指针以及队中元素: " << endl;
q.prt_sq_Queue();
return 0;
}
运行结果