数据结构作业一

数据结构作业一

一队人站成一排,从左到右按1,2,1,2的序号报数,报1的出列,报2的站到最右端,直到所有人出队,用环形队列和链队实现排队序列的输出。
后续持续更新,比较简单只有代码,不解释,有问题可以交流
链队实现 源代码

#include<iostream>
using namespace std;
typedef struct node
{
	node* next;
	int data;
}mynode;
typedef struct queue
{
	node* front;
	node* rear;
}myqueue;
void init(myqueue*& q)
{
	q = new myqueue;
	q->front = q->rear = NULL;
}
bool isempty(myqueue* q)
{
	return q->front == NULL;
}
bool enque(myqueue*& q, int e)
{
	node* p = new node;
	p->data = e;
	p->next = NULL;//初始化
	if (q->front == NULL)//空队列
	{
		q->rear = q->front = p;
	}
	else
	{
		q->rear->next = p;
		q->rear = p;//更新尾节点
	}
	return true;
}
bool deque(myqueue*& q, int& e)
{
	if (isempty(q))//当前链表尾空
	{
		return false;
	}
	else if (q->front->next == NULL)//只有一个节点
	{
		e = q->front->data;
		q->front = q->rear = NULL;
		return true;
	}
	node* p = q->front;//保存首节点
	e = p->data;
	q->front = p->next;//跟新头节点
	delete p;
	return true;
}
void destroy(myqueue*& q)
{
	if (!isempty(q))
	{
		node* pre = q->front;
		node* p = pre->next;
		while (p != NULL)
		{
			delete pre;
			pre = p;
			p = p->next;
		}
		delete pre;
	}
}
void test(myqueue*& q, int arr[])
{
	int e;
	int flag = 0;//定义标志,记录出队次序
	for (int i = 0; i < 8; i++)
	{
		enque(q, arr[i]);
	}
	cout << "链队出队序列为" << endl;
	while(!isempty(q))
	{
		deque(q, e);
		flag++;//出队次序+1
		if (flag % 2 == 1)
		{
			cout << e << "\t";
		}
		else
		{
			enque(q, e);
		}
	}
}
int  main()
{
	queue* q;
	int e;
	int arr[] = { 1,2,3,4,5,6,7,8 };
	init(q);
	test(q, arr);
	destroy(q);
	return 0;
}

环形队列实现 源代码

#include <iostream>
#define MAX 10
using namespace std;
typedef int mytype;
typedef struct queue
{
	mytype data[MAX];
	int front;
	int rear;
}mystruct;
void init(mystruct*& q)
{
	q = new mystruct;
	q->front = 0;
	q->rear = 0;
}
bool enque(mystruct*& q, mytype e)
{
	if ((q->front + MAX - q->rear) == 1)//尾指针追上头指针此时队满
	{
		return false;
	}
	q->data[q->rear] = e;//原先的队头出没有存放元素
	q->rear = (q->rear + 1) % MAX;//再取余
}
bool deque(mystruct*& q, mytype& e)
{
	if (q->front == q->rear)
	{
		return false;
	}
	e = q->data[q->front];
	q->front = (q->front + 1) % MAX;
}
void destroy(mystruct*& q)
{
	delete q;
}
bool isempty(mystruct* q)
{
	return q->front == q->rear;
}
void test(mystruct*& q, mytype arr[])
{
	int flag=0;//定义标志,区分出队的次序是奇数还是偶数
	mytype e;
	for (int i = 0; i < 8; i++)
	{
		enque(q, arr[i]);
	}
	cout << "环形队列出队序列为:" << endl;
	while(!isempty(q))
	{
		deque(q, e);
		flag++;//出对次序计数
		if (flag % 2 == 1)//只与出队次序有关
			cout << e << "\t";
		else
			enque(q, e);
	}
}
int main()
{
	mystruct* q;
	mytype arr[] = { 1,2,3,4,5,6,7,8 };
	init(q);
	test(q, arr);
	destroy(q);
	return 0;
}```


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端小Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值