队列 (更新中)

以下为个人知识点的整合,以便自己复习,如有错误请前辈们指正,谢谢!


用结构体定义顺序循环队列:


.h文件

#define listside 5

typedef struct
{
	int data[listside];
	int frist;
	int last;
}Queue;

.ccp文件


判断是否为空:

bool queuelist::isempty()
{
	if (length == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

判断是否存满了:

bool queuelist::isfull()
{
	if ((!isempty()) && (queue->frist == queue->last))
	{
		return true;
	}
	else
	{
		return false;
	}
}

向队列添加数据:

void queuelist::addqueue(int values)
{
	if (queue == NULL)
	{
		queue = (Queue*)malloc(sizeof(Queue));
		queue->last = 0;
		queue->frist = 0;
	}

	if (queue->last == listside)
	{
		queue->last = 0;
	}

	if (isfull())
	{
		printf("存满了:无法存入数据(%d),请读取已有的数据\n", values);
		return;
	}

	queue->data[queue->last++] = values;
	length++;
}

读取队列数据:

int queuelist::getqueue()
{
	if (isempty())
	{
		printf("队列为空,请存入数据\n");
		return NULL;
	}
	else if (queue->frist == listside)
	{
		queue->frist = 0;
		length--;
		return queue->data[queue->frist++];
	}
	else
	{
		length--;
		return queue->data[queue->frist++];
	}
}

获取队列存储的长度:

int queuelist::getlength()
{
	return length;
}



代码整合: https://share.weiyun.com/126da10ed15c36a37cc24f20cfec3271


用结构体定义链队列:


.h文件

typedef struct node
{
	int date;
	struct node *next;
}linkedlist;

typedef struct que
{
	linkedlist *frist;
	linkedlist *last;
}Queue;


.cpp文件


向队列添加数据:

void queuelist::addqueue(int values)
{
	if (queue == NULL)
	{
		queue = (Queue*)malloc(sizeof(Queue));
		queue->frist = (linkedlist*)malloc(sizeof(linkedlist));
		queue->frist->next = NULL;
		queue->last = queue->frist;
	}
	queue->last->next = (linkedlist*)malloc(sizeof(linkedlist));
	queue->last = queue->last->next;
	queue->last->date = values;
}


判断队列是否为空:

bool queuelist::isEmply()
{
	if (queue == NULL)
	{
		return true;
	}
	else
	{
		return false;
	}
}

判断队列是否读取完毕:

bool queuelist::isReadEnd()
{
	if (queue->frist == queue->last&&queue->last != NULL)
	{
		return true;
	}
	else
	{
		return false;
	}
}


读取队列:

int queuelist::getqueue()
{
	if (isEmply())
	{
		printf("该队列为空\n");
		return 0;
	}
	else if (isReadEnd())
	{
		printf("读取结束\n");
		return 0;
	}
	else
	{
		linkedlist *temp = queue->frist;
		queue->frist = queue->frist->next;
		free(temp);
		return queue->frist->date;
	}

}

代码整合:https://share.weiyun.com/006530bde3067f07aa795df40a15d5e7









  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值