首先要清楚队列的特点:先进先出
一、顺序队列
队尾插,对头出,同时和顺序栈相同,要动态申请内存。
#pragma once
#include
typedef char ListQueueType;
typedef struct ListQueue
{
ListQueueType* data;
int size;//已有元素
int capacity;//数据容量
int head;//头
int tail;//尾
}ListQueue;
void Init(ListQueue* queue);//初始化
void Destory(ListQueue* queue);//销毁队列
void Expansion(ListQueue* queue);//扩容
void Push(ListQueue* queue,ListQueueType value);//写入队列
void Pop(ListQueue* queue);出队列
int front(ListQueue* queue,ListQueueType* value);//队首元素
#include
#include"queue.h"
#include
void Init(ListQueue* queue)
{
if(queue == NULL)
{
//非法输入
return;
}
queue->size = 0;
queue->capacity = 1000;
queue->head = 0;
queue->tail = 0;
queue->data = (ListQueueType*)malloc(sizeof(ListQueueType)*(queue->capacity));
return;
}
//销毁
void Destory(ListQueue* queue)
{
if(queue == NULL)
{
return;
}
free(queue->data);
queue->size = 0;
queue->capacity = 0;
queue->head = 0;
queue->tail = 0;
return;
}
//扩容
void Expansion(ListQueue* queue)
{
if(queue == NULL)
{
return;
}
queue->capacity = queue->capacity*2+1;//重新开辟一块新的内存
ListQueueType* New_data = (ListQueueType*)malloc
(sizeof(ListQueueType)*(queue->capacity));
//将原来的数据进行拷贝
//但是要考虑(head和tail的前后问题)
if(queue->head < queue->tail)//head 在前
{
int i = 0;
for (;isize;++i)
{
New_data[i] = queue->data[i];
}
}
else//tail在前
{
int i = queue->head;
for(;isize;++i)
{
New_data[i] = queue->data[i];
}
i = 0;
for(;itail;++i)
{
New_data[queue->size+i] = queue->data[i];
}
queue->tail = queue->head + queue->size;//更新tail的下标
}
free(queue->data);
queue->data = New_data;
return;
}
//压栈
void Push(ListQueue* queue,ListQueueType value)
{
if(queue == NULL)
{
return;
}
if(queue->size >= queue->capacity)//内存不够时,开辟新的内存
{
Expansion(queue);
}
if(queue->tail == queue->capacity)
{
queue->tail = 0;
queue->data[queue->tail++] = value;
queue->size++;
return;
}
queue->data[queue->tail++] = value;
queue->size++;
return;
}
//出栈
void Pop(ListQueue* queue)
{
if(queue == NULL)
{
return;
}
if(queue->size == 0)
{
printf("NULL\n");
return;
}
if(queue->head == queue->capacity-1)
{
queue->head = 0;
queue->size--;
}
else
{
++queue->head;
--queue->size;
if(queue->size == 0)
{
printf("size = 0\n");
}
}
return;
}
int front(ListQueue* queue,ListQueueType* value)
{
if(queue == NULL || value == NULL)
{
return -1;
}
if(queue->size == 0)
{
value = NULL;
return -1;
}
*value = queue->data[queue->head];
return 0;
}
为了看到自己的写入的数据,这里打印了一下,可以自己测试一下。
void Print(ListQueue* queue)
{
if(queue == NULL)
{
printf("空顺序队列\n");
return;
}
int i = 0;
for(;isize;++i)
{
printf("[%c]\t",queue->data[i]);
}
printf("\n");
}
二、链式队列
尾节点入队列,头节点出队列。
stack.h
#pragma once
#include
typedef char LinkQueueType;
typedef struct LinkQueueNode
{
LinkQueueType data;
struct LinkQueueNode* next;
}LinkQueueNode;
typedef struct LinkQueue
{
LinkQueueNode* head;
LinkQueueNode* tail;
}LinkQueue;
void Init(LinkQueue* queue);//初始化
void Destory(LinkQueue* queue);//销毁
LinkQueueNode* CreatedNode(LinkQueueType value);//创建新的节点
void Push(LinkQueue* queue,LinkQueueType value);//尾插入队列
void Pop(LinkQueue* queue);//头删出队列
int Front(LinkQueue* queue,LinkQueueType* value);//队首元素
#include
#include"queue.h"
#include
void Init(LinkQueue* queue)
{
if(queue == NULL)
{
return;
}
queue->head = NULL;
queue->tail = NULL;
}
void DestoryNode(LinkQueueNode* Node)//销毁节点
{
free(Node);
return;
}
LinkQueueNode* CreatedNode(LinkQueueType value)//创建节点
{
LinkQueueNode* node = (LinkQueueNode*)malloc(sizeof(LinkQueueNode));
node->data = value;
node->next = NULL;
return node;
}
void Push(LinkQueue* queue,LinkQueueType value)
{
if(queue == NULL)
{
return;
}
LinkQueueNode* node = CreatedNode(value);
if(queue->head == NULL)
{
queue->head = queue->tail = node;
}
else
{
queue->tail->next = node;
queue->tail = queue->tail->next;
}
return;
}
void Pop(LinkQueue* queue)
{
if(queue == NULL)
{
return;
}
if(queue->head == NULL)
{
printf("空队列\n");
return;
}
LinkQueueNode* to_delete = queue->head;
queue->head = to_delete->next;
DestoryNode(to_delete);
return;
}
int Front(LinkQueue* queue,LinkQueueType* value)
{
if(queue == NULL || value == NULL)
{
return -1;
}
if(queue->head == NULL)
{
printf("空队列\n");
return -1;
}
*value = queue->head->data;
return 0;
}
void Destory(LinkQueue* queue)
{
if(queue == NULL)
{
return;
}
LinkQueueNode* to_delete = queue->head;
while(to_delete != NULL)
{
LinkQueueNode* next = to_delete->next;
DestoryNode(to_delete);
to_delete = next;
}
queue->head = NULL;
queue->tail = NULL;
return;
}