[数据结构][队列]链表模拟队列操作 原创

#include<iostream>
#include<cstdio>
using namespace std;

#define MAXSIZE 100

typedef int Status;
typedef int Elem;

typedef struct QNode
{
	/* data */
	Elem data;
	struct QNode *next;

}QNode, *QueuePtr;

typedef struct
{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;//这种写法让我想起了 定义结构体 然后定义类 在类里放结构体 

Status InitQueue(LinkQueue &Q)
{
	Q.front = Q.rear = new QNode;
	Q.front->next = NULL; //头指针 空

	return 1;
}

Status EnQueue_H(LinkQueue &Q, Elem e)
{
	QNode *p = new QNode;
	p->data = e;
	p->next = NULL;

	Q.rear->next = p;//队尾的结点指向 新节点p
	Q.rear = p;//队尾为p

	return 1;
}

Status DeQueue(LinkQueue &Q, Elem &e)
{
	if (Q.rear == Q.front) return 0;

	QNode *p = Q.front->next;
	e = p->data;

	Q.front->next = p->next; //其实就是往下走 当删除了

	if (Q.rear == p) Q.rear = Q.front; //如果整个没了 那就尾巴指向头

	delete p;

	return 1;
}

Elem GetHead(LinkQueue &Q)
{
	if (Q.front != Q.rear)
		return Q.front->next->data;

	return 0;
}

int main()
{

	LinkQueue Q;
	
	//1
	InitQueue(Q);

	//1
	EnQueue_H(Q, 1);
	cout << GetHead(Q);

	//1
	Elem e = 0;
	DeQueue(Q, e);
	cout << GetHead(Q);

	return 1;
}
posted @   俺叫西西弗斯  阅读(0)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
历史上的今天:
2023-06-10 [算法]归并排序 原创
点击右上角即可分享
微信分享提示