软件技术基础与开发 PPT - Class08 - 第2章 - 上机 - 栈和队列顺序存储结构的实现

在这里插入图片描述

参考答案——顺序栈类的实现

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);	// 构造函数:建立容量为m的空栈
	~sq_Stack();	// 析构函数:释放空间,删除指针
	void prt_sq_Stack();	// 顺序输出栈顶指针和栈中元素
	int flag_sq_Stack();	// 检测顺序栈的状态
	void ins_sq_Stack(T);	// 压入栈
	T del_sq_Stack();		// 退栈
	T read_sq_Stack();		// 读栈
};

// 构造函数:建立容量为m的空栈
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;				// 栈顶指针为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;
	}
	//prt_sq_Stack();
	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--;
	//prt_sq_Stack();
	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;	// 队列是否非空的标志,s=0为空队列,s=1为非空队列
	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();		// 退队
};

//-------成员函数实现----------------------------------------------//
// 建立容量为m_volume的空循环队列
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);	// 循环队列已满,返回-1
	if (m_flagNonEmp == 0) return(0);	// 循环队列为空,返回0
	return(1);	// 循环队列正常,返回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++;	// 队尾指针进1
		if (m_rear == m_volume + 1) m_rear = 1;
		m_sqQueue[m_rear - 1] = x;		// 压入队尾
		m_flagNonEmp = 1;				// 队非空,置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++;	// 队头指针进1
	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);	// 建立容量为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;
}

运行结果

在这里插入图片描述在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值