<数据结构> 实验三:栈和队列——栈

一、实验目的

     巩固栈和队列数据结构,学会运用栈和队列。

1.回顾栈和队列的逻辑结构和受限操作特点,栈和队列的物理存储结构和常见操作。

2.学习运用栈和队列的知识来解决实际问题。

3.进一步巩固程序调试方法。

4.进一步巩固模板程序设计。

二、实验内容

1.自己选择顺序或链式存储结构,定义一个空栈类,并定义入栈、出栈、取栈元素基本操作。然后在主程序中对给定的N个数据进行验证,输出各个操作结果。


顺序栈:

//Seq_Stack.h

#include <iostream>
using namespace std;

#ifndef STACK_H_
#define STACK_H_
#define Maxsize 10

template <typename T>
class Seq_Stack{
public:
	Seq_Stack(){
		m_Top = -1;
		m_length = 0;
	}
	~Seq_Stack(){}
	void In_stack();                             //压栈
	void Out_stack();                            //弹栈
	void Get_stack();                             //取栈
	bool Empty();                                //判断是否为空栈
	void Choice_menu();                          //选择菜单
private:
	T m_data[Maxsize];
	int m_Top;                                  //栈顶伪指针
	int m_length;
};
#endif

template <typename T>
void Seq_Stack<T>::In_stack(){
	if (m_length == Maxsize){
		cout << "\n该栈已满." << endl;
		system("pause");
		system("cls");
		return;
	}
	m_Top = (m_Top + 1) % Maxsize;
	cout << "\n请输入压栈元素:";
	cin >> m_data[m_Top];
	cout << m_data[m_Top] << " 压栈." << endl;
	m_length++;
	system("pause");
	system("cls");
}

template <typename T>
void Seq_Stack<T>::Out_stack(){
	if (this->Empty()){
		cout << endl;
		cout << m_data[m_Top--] << " 出栈." << endl;
		m_length--;
	}
	system("pause");
	system("cls");
}

template <typename T>
bool Seq_Stack<T>::Empty(){
	if (m_length == 0){
		cout << "\n该栈为空栈." << endl;
		return false;
	}
	return true;
}

template <typename T>
void Seq_Stack<T>::Get_stack(){
	if (m_length == 0){
		cout << "\n该栈为空栈." << endl;
		system("pause");
		system("cls");
		return;
	}
	cout << "\n栈顶元素为:" << m_data[m_Top]<< endl;
	system("pause");
	system("cls");
}

template <typename T>
void Seq_Stack<T>::Choice_menu(){
	int ch;
	cout << "┏----------------------------------------------------------------------------┓";
	cout << "┃                                                                            ┃";
	cout << "┠---------------------------------顺 序 栈-----------------------------------┨";
	cout << "┃                                                                            ┃";
	cout << "┃                                                                            ┃";
	cout << "┃                             1.  压     栈                                  ┃";
	cout << "┃                             2.  弹     栈                                  ┃";
	cout << "┃                             3.  取     栈                                  ┃";
	cout << "┃                             0.  退     出                                  ┃";
	cout << "┃                                                                            ┃";
	cout << "┃                                                                            ┃";
	cout << "┠----------------------------------------------------------------------------┨";
	cout << "                                                                                ";
	cout << "                                                                                ";
	cout << "请选择你要执行的选项:";
	cin >> ch;
	while (ch != 1 && ch != 2 && ch != 3 && ch != 0){
		cout << "\n没有该选项,请重新选择." << endl;
		cout << "\n请选择你要执行的选项:";
		cin >> ch;
	}
	switch (ch){
	case 1:
		system("cls");
		this->In_stack();
		break;
	case 2:
		system("cls");
		this->Out_stack();
		break;
	case 3:
		system("cls");
		this->Get_stack();
		break;
	case 0:
		cout << "\n                 感谢你的使用!!" << endl;
		exit(EXIT_FAILURE);
	}
}



//Seq_Stack.cpp

#include <iostream>
#include "Seq_Stack.h"


int main(){
	Seq_Stack<int> ss;
	while (1)
		ss.Choice_menu();
	return 0;
}


实验结果:

操作选择


压栈操作

连续压了三次


弹栈与取栈操作


全部弹出后继续弹栈与取栈结果都是




链栈

//Link_Queue.h

#include <iostream>
using namespace std;

#ifndef STACK_H_
#define STACK_H_

template <typename T>
struct Node{
	T data;
	Node<T> *next;
};

template <typename T>
class Link_Queue{
public:
	Link_Queue();
	~Link_Queue();
	void In_Queue();                             //入队
	void Out_Queue();                            //出队
	void Get_Queue();                             //取队
	bool Empty();                                //判断是否为空队列
	void Choice_menu();                          //选择菜单
private:
	Node<T> *p_Front;                                  //队列头指针
	Node<T> *p_Rear;                                   //队列尾指针
	int m_length;
};
#endif

template <typename T>
Link_Queue<T>::Link_Queue(){
	p_Front = new Node<T>;
	p_Front->next = NULL;
	p_Rear = p_Front;
	m_length = 0;
}

template <typename T>
Link_Queue<T>::~Link_Queue(){
	Node<T> *p, *q;
	p = p_Front;
	q = p;
	while (p != NULL){
		p = p->next;
		delete q;
		q = p;
	}
}

template <typename T>
void Link_Queue<T>::In_Queue(){
	T x;
	Node<T> *p;
	p = new Node<T>;
	cout << "\n请输入入队元素:";
	cin >> x;
	p->data = x;
	p->next = p_Rear->next;
	p_Rear->next = p;
	p_Rear = p;
	cout << x << " 入队." << endl;
	system("pause");
	system("cls");
}

template <typename T>
void Link_Queue<T>::Out_Queue(){
	Node<T> *p;
	p = p_Front->next;
	if (this->Empty()){
		cout << "\n" << p->data << " 出队." << endl;
		p_Front->next = p->next;
		delete p;
		system("pause");
		system("cls");
	}
}

template <typename T>
void Link_Queue<T>::Get_Queue(){
	Node<T> *p;
	p = p_Front->next;
	if (this->Empty()){
		cout << "\n队头元素为:" << p->data << endl;
		system("pause");
		system("cls");
	}
}

template <typename T>
bool Link_Queue<T>::Empty(){
	Node<T> *p;
	p = p_Front;
	if (p->next == NULL){
		cout << "\n该栈为空栈." << endl;
		system("pause");
		system("cls");
		return false;
	}
	return true;
}

template <typename T>
void Link_Queue<T>::Choice_menu(){
	int ch;
	cout << "┏----------------------------------------------------------------------------┓";
	cout << "┃                                                                            ┃";
	cout << "┠---------------------------------链  队  列---------------------------------┨";
	cout << "┃                                                                            ┃";
	cout << "┃                                                                            ┃";
	cout << "┃                             1.  入     队                                  ┃";
	cout << "┃                             2.  出     队                                  ┃";
	cout << "┃                             3.  取     队                                  ┃";
	cout << "┃                             0.  退     出                                  ┃";
	cout << "┃                                                                            ┃";
	cout << "┃                                                                            ┃";
	cout << "┠----------------------------------------------------------------------------┨";
	cout << "                                                                                ";
	cout << "                                                                                ";
	cout << "请选择你要执行的选项:";
	cin >> ch;
	while (ch != 1 && ch != 2 && ch != 3 && ch != 0){
		cout << "\n没有该选项,请重新选择." << endl;
		cout << "\n请选择你要执行的选项:";
		cin >> ch;
	}
	switch (ch){
	case 1:
		system("cls");
		this->In_Queue();
		break;
	case 2:
		system("cls");
		this->Out_Queue();
		break;
	case 3:
		system("cls");
		this->Get_Queue();
		break;
	case 0:
		cout << "\n                 感谢你的使用!!" << endl;
		exit(EXIT_FAILURE);
	}
}


//Link_Stack.cpp

#include <iostream>
#include "Link_Stack.h"
using namespace std;

int main(){
	Link_Stack<int> ls;
	while (1)
		ls.Choice_menu();
	return 0;
}

实验结果与上述顺序表大同小异,就不发图了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值