数据结构---链式队列(动态队列)

链式队列:队列的链式存储结构,其实就是线性表的单链表,只是将其限制了只能头删除在尾插入,我们把它简称为链队列。为了操作上的方便,我们将队头指针指向链队列的头结点,而队尾指针指向终端节点

链式队列三个重要组成部分:
1.链式队列的长度
2.指向队首的指针
3.指向队尾的指针

1.结构类型:
定义节点类型,用于存储数据,再定义一个链式队列类型,用于控制整个队列

#define MAX 50
typedef struct _node {
	int date;
	struct _node* next;
}LinkNode;
typedef LinkNode* QueuePtr;
typedef struct LinkQ {
	int length;
	QueuePtr fornt;
	QueuePtr rear;
}LinkQueue;

2.初始化:经队首和队尾指针指向空,防止系统崩溃,再将长度设为0

int  init(LinkQueue* LQ) {
	if (!LQ) return 0;
	LQ->length = 0;
	LQ->fornt = LQ->rear = NULL;
	return 1;
}

3.是否为空或满:
空:如果队首指针指向空,说明整个队列为空
满:如果长度大于最大长度,则整个队列已满

int Isempty(LinkQueue* LQ) {
	if (!LQ) return 0;
	if (LQ->fornt == NULL) {
		return 1;
	}
	return 0;
}
int Isfull(LinkQueue* LQ) {
	if (!LQ) return 0;
	if (LQ->length == MAX) {
		return 1;
	}
	return 0;
}

4.添加数据(入队):现将要添加的数据存入一个节点中,判断是否为空,经队首和队尾指针都指向该节点。如果不为空将这个节点添加到队尾,队尾指针在指向它,并且长度+1。

int Enter(LinkQueue* LQ,int e) {
	if (!LQ) return 0;
	LinkNode *tem = new LinkNode;//函数结束不会被释放,知道使用delete关键字
	tem->date = e;
	tem->next = NULL;
	if (Isempty(LQ)) {
		LQ->fornt = LQ->rear = tem;
	}
	else {
		LQ->rear->next = tem;
		LQ->rear = tem;
	}
	LQ->length++;
	return 1;
}

5.删除一个数据(出队):先判断队列是否为空,如果不为空,并且队首指针和队尾指针指向同一个位置,那么说明队列只剩下一个节点,那么将他都出来后,整个队列为空,所经将队首队尾指针设置为空。 如果有多个节点头指针向后指一位,长度-1;

int Delete(LinkQueue* LQ) {
	if (!LQ) return 0;
	LinkNode* tem = new LinkNode;
	if (Isfull(LQ)) {
		cout << "队列为空" << endl;
		return 0;
	}
	if (LQ->fornt == LQ->rear) {
		tem = LQ->fornt;
		LQ->rear = LQ->rear = NULL;
	}
	else {
		tem = LQ->fornt;
		LQ->fornt = LQ->fornt->next;
	}
	delete tem;
	LQ->length--;
	return 1;
}

6.打印:遍历链表

int print(LinkQueue *LQ) {
	if (!LQ) return 0;
	if (Isempty(LQ)) {
		cout << "列表为空" << endl;
		return  0;
	}
	LinkNode* tem = new LinkNode;
	tem = LQ->fornt;
	while (tem) {
		cout << tem->date << endl;
		tem = tem->next;
	}
	return 1;
}

全部代码:

#include
#include
using namespace std;
#define MAX 50
typedef struct _node {
	int date;
	struct _node* next;
}LinkNode;
typedef LinkNode* QueuePtr;
typedef struct LinkQ {
	int length;
	QueuePtr fornt;
	QueuePtr rear;
}LinkQueue;
int  init(LinkQueue* LQ) {
	if (!LQ) return 0;
	LQ->length = 0;
	LQ->fornt = LQ->rear = NULL;
	return 1;
}
int Isempty(LinkQueue* LQ) {
	if (!LQ) return 0;
	if (LQ->fornt == NULL) {
		return 1;
	}
	return 0;
}
int Isfull(LinkQueue* LQ) {
	if (!LQ) return 0;
	if (LQ->length == MAX) {
		return 1;
	}
	return 0;
}
int Enter(LinkQueue* LQ,int e) {
	if (!LQ) return 0;
	LinkNode *tem = new LinkNode;//函数结束不会被释放,知道使用delete关键字
	tem->date = e;
	tem->next = NULL;
	if (Isempty(LQ)) {
		LQ->fornt = LQ->rear = tem;
	}
	else {
		LQ->rear->next = tem;
		LQ->rear = tem;
	}
	LQ->length++;
	return 1;
}
int Delete(LinkQueue* LQ) {
	if (!LQ) return 0;
	LinkNode* tem = new LinkNode;
	if (Isfull(LQ)) {
		cout << "队列为空" << endl;
		return 0;
	}
	if (LQ->fornt == LQ->rear) {
		tem = LQ->fornt;
		LQ->rear = LQ->rear = NULL;
	}
	else {
		tem = LQ->fornt;
		LQ->fornt = LQ->fornt->next;
	}
	delete tem;
	LQ->length--;
	return 1;
}
int print(LinkQueue *LQ) {
	if (!LQ) return 0;
	if (Isempty(LQ)) {
		cout << "列表为空" << endl;
		return  0;
	}
	LinkNode* tem = new LinkNode;
	tem = LQ->fornt;
	while (tem) {
		cout << tem->date << endl;
		tem = tem->next;
	}
	return 1;
}
int Getfornt(LinkQueue* LQ,int &e) {
	if (!LQ||Isempty(LQ)) return 0;
	e = LQ->fornt->date;
	return 0;
}
int des(LinkQueue* LQ) {
	if (!LQ || Isempty(LQ)) {
		cout << "列表为空" << endl;
		return 0;
	}
	while (LQ->fornt) {
		LinkNode* tep = LQ->fornt;
		LQ->fornt = LQ->fornt->next;
		delete tep;
		tep = LQ->fornt;
	}
	LQ->rear = NULL;
	LQ->length = 0;
	return 0;
}
int main() {
	LinkQueue* LQ  = new LinkQueue;
	
	init(LQ);
	for (int i = 0;i < 5;i++) {
		Enter(LQ, i);
	}
	print(LQ);
	cout << "出队后:" << endl;
	Delete(LQ);  
	print(LQ);
	cout << "销毁后:" << endl;
	des(LQ);
	print(LQ);
	system("pause");
	return 0;
}

你可能感兴趣的:(数据结构和算法,队列,数据结构,链表,c++)