数据结构第三章队列链式实现

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <initializer_list>
#include <deque>
#include <list>
#include <array>
#include <forward_list>
#include <sstream>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <memory>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <type_traits>
#include <utility>
#include <tuple>
#include <bitset>
#include <regex>
#include <random>
#include <iomanip>
#include <ctime>
#include <cmath>
#define ERROR -1
using namespace::std;
using ElementType = int;
#define ERROR -1
#define MAXSIZE 10
struct Node;
typedef Node* PtrToNode;
struct Node
{
	ElementType Data;
	PtrToNode Next;
};
typedef PtrToNode Position;



struct QNode;
typedef QNode* PtrToQNode;
struct QNode
{
	Position front;
	Position rear;
	int MaxSize;
};
typedef PtrToQNode Queue;

Queue CreateQueue(int MaxSize)
{
	Queue Q = (Queue)malloc(sizeof(QNode));
	Q->front=NULL;
	Q->rear=NULL;
	Q->MaxSize = MaxSize;
	return Q;
}

bool IsFull(Queue Q)
{
	int cnt = 1;
	Position now;
	now = Q->front;
	if (Q->rear == nullptr)
	{
		return false;
	}
	else
	{
		while (now != Q->rear->Next)
		{
			++cnt;
			now = now->Next;
		}
		if (cnt >= Q->MaxSize)
			return true;
		else
			return false;
	}
}

bool AddQueue(Queue Q, ElementType X)
{
	if (IsFull(Q))
	{
		cout << "The Queue is full.Invalid Addition." << endl;
		return false;
	}
	else
	{

		PtrToNode addition = (PtrToNode)malloc(sizeof(Node));
		addition->Data = X;
		addition->Next = nullptr;
		if (Q->rear == NULL)
		{
			Q->rear = addition;
		}
		else
		{
			Q->rear->Next = addition;
			Q->rear = addition;
		}
		if (Q->front == NULL)
		{
			Q->front = addition;
		}
		return true;
	}
}

bool IsEmpty(Queue Q)
{
	return (Q->front == NULL);
}

ElementType DeleteQueue(Queue Q)
{
	Position FrontCell;
	ElementType FrontElem;
	if (IsEmpty(Q))
	{
		cout << "The Queue is empty.Invalid delete." << endl;
		return ERROR;
	}
	else
	{
		FrontCell = Q->front;
		if (Q->front == Q->rear)
			Q->front = Q->rear = NULL;
		else
		{
			Q->front = Q->front->Next;
		}
		FrontElem = FrontCell->Data;
		free(FrontCell);
		return FrontElem;
	}
}

void show(Queue Q)
{
	if (IsEmpty(Q))
	{
		cout << "The Queue is empty.Invalid show." << endl;
		return;
	}
	else
	{
		PtrToNode Pos;
		Pos = Q->front;
		while (Pos)
		{
			cout << Pos->Data << endl;
			Pos = Pos->Next;
		}
		return;
	}
}

int main()
{
	Queue Q = CreateQueue(MAXSIZE);
	AddQueue(Q, 3);
	AddQueue(Q, 4);
	AddQueue(Q, 5);
	show(Q);
	DeleteQueue(Q);
	show(Q);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值