您现在的位置是:主页 > news > wordpress客户使用的后端/东莞seo网络培训
wordpress客户使用的后端/东莞seo网络培训
admin2025/4/25 5:02:19【news】
简介wordpress客户使用的后端,东莞seo网络培训,软件开发工具03173课后题,网站开发项目商业计划书队列的定义 队列是一种特殊的线性表。 队列仅在线性表的两端进行操作:队头(Front):取出数据元素的一端队尾(Rear):插入数据元素的一端 队列不允许在中间部位进行操作! 队列的性质:先进先出 队列的一些常用操作&…
队列的定义
队列是一种特殊的线性表。
队列仅在线性表的两端进行操作:
队头(Front):取出数据元素的一端
队尾(Rear):插入数据元素的一端
队列不允许在中间部位进行操作!
队列的性质:先进先出
队列的一些常用操作:
创建队列,销毁队列,清空队列,进队列,出队列,获取队头元素,获取队列的长度
1. 队列的顺序存储实现
队列的顺序存储实现是基于线形表的代码实现的,因此需要调用线形表的头文件,线性表的代码在前面的文章中已经详细说明,此处不再赘述。
//顺序队列 头文件 SeqQueue.h
#ifndef _SEQQUEUE_H_
#define _SEQQUEUE_H_typedef void SeqQueue;SeqQueue *SeqQueue_Create(int capacity);
void SeqQueue_Destroy(SeqQueue *queue);
void SeqQueue_Clear(SeqQueue *queue);
int SeqQueue_Append(SeqQueue *queue, void *item); //加入队列
void *SeqQueue_Retrieve(SeqQueue *queue); //出队列
void *SeqQueue_Header(SeqQueue *queue); //获取头部
int SeqQueue_Length(SeqQueue *queue);
int SeqQueue_Capacity(SeqQueue *queue);#endif
//顺序队列 源文件 SeqQueue.c
#include "SeqList.h"
#include "SeqQueue.h"SeqQueue *SeqQueue_Create(int capacity) // O(1)
{return SeqList_Create(capacity);
}void SeqQueue_Destroy(SeqQueue *queue) // O(1)
{SeqList_Destroy(queue);
}void SeqQueue_Clear(SeqQueue *queue) // O(1)
{SeqList_Clear(queue);
}int SeqQueue_Append(SeqQueue *queue, void *item) // O(1)加入队列
{return SeqList_Insert(queue, item, SeqList_Length(queue));
}void *SeqQueue_Retrieve(SeqQueue *queue) // O(n) 出队列
{return SeqList_Delete(queue, 0);
}void *SeqQueue_Header(SeqQueue *queue) // O(1) 获取头部元素
{return SeqList_Get(queue, 0);
}int SeqQueue_Length(SeqQueue *queue) // O(1)
{return SeqList_Length(queue);
}int SeqQueue_Capacity(SeqQueue *queue) // O(1)
{return SeqList_Capacity(queue);
}
2. 队列的链式存储实现
同样,链式存储也要借助于链表的代码。
//链式队列 头文件 LinkList.h
#ifndef _LINKQUEUE_H_
#define _LINKQUEUE_H_typedef void LinkQueue;LinkQueue *LinkQueue_Create();
void LinkQueue_Destroy(LinkQueue *queue);
void LinkQueue_Clear(LinkQueue *queue);
int LinkQueue_Append(LinkQueue *queue, void *item);
void *LinkQueue_Retrieve(LinkQueue *queue);
void *LinkQueue_Header(LinkQueue *queue);
int LinkQueue_Length(LinkQueue *queue);#endif
//链式队列 源文件 LinkList.c
#include <stdio.h>
#include <malloc.h>
#include "LinkList.h"
#include "LinkQueue.h"typedef struct _tag_LinkQueueNode
{LinkListNode header;void *item;
} TLinkQueueNode;LinkQueue *LinkQueue_Create() // O(1)
{return LinkList_Create();
}void LinkQueue_Destroy(LinkQueue *queue) // O(n)
{LinkQueue_Clear(queue);LinkList_Destroy(queue);
}void LinkQueue_Clear(LinkQueue *queue) // O(n)
{while (LinkQueue_Length(queue) > 0){LinkQueue_Retrieve(queue);}
}int LinkQueue_Append(LinkQueue *queue, void *item) // O(n) 数据加入队列
{TLinkQueueNode *node = (TLinkQueueNode *)malloc(sizeof(TLinkQueueNode));int ret = (item != NULL) && (node != NULL);if (ret){node->item = item;ret = LinkList_Insert(queue, (LinkListNode *)node, LinkList_Length(queue));}if (!ret){free(node);}return ret;
}void *LinkQueue_Retrieve(LinkQueue *queue) // O(1) 出队列
{TLinkQueueNode *node = (TLinkQueueNode *)LinkList_Delete(queue, 0);void *ret = NULL;if (node != NULL){ret = node->item;free(node);}return ret;
}void *LinkQueue_Header(LinkQueue *queue) // O(1) 获取头部元素
{TLinkQueueNode *node = (TLinkQueueNode *)LinkList_Get(queue, 0);void *ret = NULL;if (node != NULL){ret = node->item;}return ret;
}int LinkQueue_Length(LinkQueue *queue) // O(1)
{return LinkList_Length(queue);
}
小结
队列是一种特殊的线性表。
队列只允许在线性表的头部和尾部进行操作。
队列通常有两种实现方式:
- 顺序结构实现
- 链式结构实现
顺序队列的优化方案
顺序队列:
线性表的第一个元素作为队头
线性表的最后一个元素作为队尾
入队的新元素是在线性表的最后,时间复杂度为O(1)。
出队时需要将后续的所有元素向前移动,时间复杂度O(n)。
希望将出队时的时间复杂度降为O(1)。
优化方案:
定义front使其始终代表队头的下标
出队时将队头元素返回,且front++
定义rear使其始终代表队尾下一个元素的下标
入队时将新元素插入,且rear++
没有必要只将下标为0的位置定义为队头。
如图所示:
链式队列的优化方案
链式队列:
线性表的第一个元素作为队头
线性表的最后一个元素作为队尾
入队的新元素是在线性表的最后,时间复杂度为O(n)。
出队的元素即链表的第一个元素,时间复杂度O(1)。
需要让入队的时间复杂度简化为O(1)。
优化方案:
定义rear指针始终指向链表中的最有一个元素。
入队时将新元素通过rear插入队尾,且将rear指向新元素。
小结
优化的顺序队列循环利用顺序空间提高出队
操作的效率。
优化的链式队列定义rear指针指向队尾元素
提高出队操作的效率。