// linkQueue.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
using namespace std;
typedef int Element;
typedef struct LinkNode {
Element date;
struct LinkNode* next;
}LinkNode;
typedef struct {
LinkNode* front, * rear;
}LinkQueue;
//================================================带头结点=========================================
//初始化
bool init(LinkQueue &Q) {
//申请空间
Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
if (Q.front == NULL || Q.rear == NULL) {
cout << "初始化失败" << endl;
}
Q.front->next = NULL;
cout << "初始化成功" << endl;
return true;
}
//判空
bool isEmpty(LinkQueue& Q) {
if (Q.front == Q.rear) {
cout << "队列为空" << endl;
return true;
}
cout << "队列不空" << endl;
return false;
}
//入队
bool insert(LinkQueue& Q, Element e) {
LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
if (p == NULL) {
cout << "分配空间失败" << endl;
return false;
}
p->date = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
cout << "元素" << e << "入队成功" << endl;
return true;
}
//出队
bool del(LinkQueue& Q, Element e) {
if (isEmpty(Q)) {
return false;
}
LinkNode* p = Q.front->next;
e = p->date;
Q.front->next = p->next;
//如果front的下一个元素是rear,在删除尾节点后,front 和 rear 的next都指向了空,变为空队列。
if (Q.rear == p) {
Q.front = Q.rear;
}
free(p);
cout << "元素" << e << "出队成功" << endl;
return true;
}
bool show(LinkQueue& Q) {
if (isEmpty(Q)) {
return false;
}
Element e = 0;
LinkNode* p = Q.front;
while (p->next != NULL) {
cout << p->next->date << endl;
p = p->next;
}
return true;
}
//测试带头结点
void testLinkQueue() {
LinkQueue Q;
init(Q);
isEmpty(Q);
Element e = 1;
while (e <= 3) {
insert(Q, e);
e++;
}
show(Q);
isEmpty(Q);
del(Q, e);
del(Q, e);
del(Q, e);
show(Q);
}
//================================================不带头结点=========================================
//初始化
bool init2(LinkQueue& Q) {
Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode*));
if (Q.front == NULL || Q.rear == NULL) {
cout << "初始化失败" << endl;
return false;
}
Q.front = Q.rear = NULL;
cout << "初始化成功";
return true;
}
//判空
bool isEmpty2(LinkQueue& Q) {
if (Q.front == NULL) {
cout << "队列为空" << endl;
return true;
}
cout << "队列不为空" << endl;
return false;
}
//入队
bool insert2(LinkQueue& Q, Element e) {
LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
if (p == NULL) {
cout << "分配空间失败" << endl;
return false;
}
p->date = e;
p->next = NULL;
if (isEmpty2(Q)) {
Q.front = p;
Q.rear = p;
}
else {
Q.rear->next = p;
Q.rear = p;
}
cout << "元素" << e << "入队成功" << endl;
return true;
}
//出队
bool del2(LinkQueue& Q, Element& e) {
if (isEmpty2(Q)) {
return false;
}
LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
if (p == NULL) {
cout << "分配空间失败" << endl;
return false;
}
p = Q.front;
e = p->date;
Q.front = p->next;
if (p == Q.rear) {
Q.front = NULL;
Q.rear = NULL;
}
free(p);
cout << "元素" << e << "出队成功" << endl;
return true;
}
//展示
bool show2(LinkQueue& Q) {
if (isEmpty2(Q)) {
return false;
}
LinkNode* p = Q.front;
while (p != NULL) {
cout << p->date << endl;
p = p->next;
}
return true;
}
void testLinkQueue2() {
LinkQueue Q;
init2(Q);
isEmpty2(Q);
Element e = 1;
while (e <= 3) {
insert2(Q, e);
e++;
}
show2(Q);
del2(Q, e);
del2(Q, e);
del2(Q, e);
del2(Q, e);
}
int main()
{
cout << "===================带头节点==================" <<endl;
testLinkQueue();
cout << "===================不带头节点==================" <<endl;
testLinkQueue2();
}