数据结构=C++语言=队列==顺序存储实现

关键在于::


数组形式

循环队列


 //顺序存储结构
#include 
  
  
   
   
#include 
   
   
    
    
#include 
    
    
     
     
const int MAXSIZE=10;
typedef int ElemType;
struct SeQueuestr
{
	ElemType elem[MAXSIZE];
	int front, rear;
};
using namespace std;
class SeQueue
{
public:
	SeQueue();
	~SeQueue();
	int Empty();
	void Display();
	void AddQ(ElemType x);
	ElemType DelQ();
private:
	ElemType elem[MAXSIZE];
	int front, rear; 
};

SeQueue::SeQueue()
{
	front = 0;
	rear = 0;
}

SeQueue::~SeQueue()
{
}

int SeQueue::Empty() {
	if (rear=front)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void SeQueue::Display() {
	cout << "开始输出" << endl;
	int f;
	f = front;
	while (f!=rear)
	{
		cout << elem[f+1]<< endl;
		f = (f + 1) % MAXSIZE;
	}
	cout << "输出完毕" << endl;
}
void SeQueue::AddQ(ElemType x) {
	if ((rear+1)%MAXSIZE==front)//循环队列的满队列判断
	{
		cout << "Queue is Full!!" << endl;
	}
	else
	{
		rear = (rear + 1) % MAXSIZE;//出队列或者进队列都要头尾指针做加一后取余运算;
		elem[rear] = x;
	}
}
ElemType SeQueue::DelQ() {
	if (rear==front)//循环队列的空判断
	{
		cout << "Queue is empty!" << endl;
		return -1;
	}
	else
	{
		front = (front + 1) % MAXSIZE;
		return elem[front];
	}
}
int main() {
	ElemType e;
	int a,j;
	SeQueue s;
	cout << "队列顺序存储结构" << endl;
	cout << "输入1进行入队,输入2进行出队,输入-999结束" << endl;
	cin >> a;
	while (a != -999)
	{
		if (a == 1) {
			cout << "输入入队数据:" << endl;
			cin >> e;
			s.AddQ(e);
			s.Display();
			cout << "输入指令:" << endl;
			cin >> a;
		}
		if (a == 2) {
			e = s.DelQ();
			cout << "出队数据为:" << e << endl;
			s.Display();
			cout << "输入指令:" << endl;
			cin >> a;
		}
		else {
			cout << "错误指令!" << endl;
			cout << "输入指令:" << endl;
			cin >> a;
		}
	}

	_getch();
	return 0;

 }
    
    
   
   
  
  

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值