数据结构—队列的顺序和链式存储
程序员文章站
2022-06-05 14:46:02
...
队列的顺序存储
队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表
队列的特点:先进先出
queue.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 1024
typedef struct QUEUE
{
struct QUEUE* Data[MAX_SIZE];//创建队列容器1024
int size;//统计队列数据
}Queue;
typedef void* Sequence_Queue;
//初始化
Sequence_Queue Init_Queue();
//入队
void Push_Queue(Sequence_Queue SeqQueue, void* data);
//出队
void Pop_Queue(Sequence_Queue SeqQueue);
//获得队头元素
void* Front_Queue(Sequence_Queue SeqQueue);
//获得队尾元素
void* Back_Queue(Sequence_Queue SeqQueue);
//获得队列长度
int Size_Queue(Sequence_Queue SeqQueue);
//销毁队列
void Destroy_Queue(Sequence_Queue SeqQueue);
queue.c
#include"queue.h"
//初始化
Sequence_Queue Init_Queue()
{
Queue* SeqQueue = (Queue*)malloc(sizeof(Queue));
if (NULL == SeqQueue)
{
printf("Init_Queue SeqQueue malloc error\n");
return NULL;
}
int i = 0;
for (; i < MAX_SIZE; ++i)
{
SeqQueue->Data[i] = NULL;
}
SeqQueue->size = 0;
return SeqQueue;
}
//入队
void Push_Queue(Sequence_Queue SeqQueue, void* data)
{
if (NULL == SeqQueue)
{
printf("Push_Queue SeqQueue is NULL\n");
return;
}
if (NULL == data)
{
printf("Push_Queue data is NULL\n");
return;
}
Queue* seqqueue = (Queue*)SeqQueue;
//选择最下标最大的为头端队列
if (seqqueue->size == MAX_SIZE)
{
return;
}
int i = seqqueue->size-1;
for (; i >=0; --i)
{
seqqueue->Data[i + 1] = seqqueue->Data[i];
}
seqqueue->Data[0] = data;
++seqqueue->size;
}
//出队
void Pop_Queue(Sequence_Queue SeqQueue)
{
if (NULL == SeqQueue)
{
printf("Pop_Queue SeqQueue is NULL\n");
return;
}
Queue* seqqueue = (Queue*)SeqQueue;
if (seqqueue->size == 0)
{
return;
}
--seqqueue->size;
}
//获得队头元素
void* Front_Queue(Sequence_Queue SeqQueue)
{
if (NULL == SeqQueue)
{
printf("Front_Queue SeqQueue is NULL\n");
return NULL;
}
Queue* seqqueue = (Queue*)SeqQueue;
if (seqqueue->size == 0)
{
return NULL;
}
return seqqueue->Data[seqqueue->size - 1];
}
//获得队尾元素
void* Back_Queue(Sequence_Queue SeqQueue)
{
if (NULL == SeqQueue)
{
printf("Back_Queue SeqQueue is NULL\n");
return NULL;
}
Queue* seqqueue = (Queue*)SeqQueue;
if (seqqueue->size == 0)
{
return NULL;
}
return seqqueue->Data[0];
}
//获得队列长度
int Size_Queue(Sequence_Queue SeqQueue)
{
if (NULL == SeqQueue)
{
printf("Size_Queue SeqQueue is NULL\n");
return -1;
}
Queue* seqqueue = (Queue*)SeqQueue;
return seqqueue->size;
}
//销毁队列
void Destroy_Queue(Sequence_Queue SeqQueue)
{
if (NULL == SeqQueue)
{
printf("Destroy_Queue SeqQueue is NULL\n");
}
free(SeqQueue);
}
main.c
#include"queue.h"
typedef struct STUDENT
{
char name[64];
int age;
}student;
int main()
{
student s1 = { "student1", 10 };
student s2 = { "student2", 20 };
student s3 = { "student3", 30 };
student s4 = { "student4", 40 };
student s5 = { "student5", 50 };
Queue* SeqQueue=Init_Queue();
Push_Queue(SeqQueue, &s1);
Push_Queue(SeqQueue, &s2);
Push_Queue(SeqQueue, &s3);
Push_Queue(SeqQueue, &s4);
Push_Queue(SeqQueue, &s5);
//获取队列长度
int size = Size_Queue(SeqQueue);
printf("Size:%d\n", size);
//获取队列尾部
student* SBack = (student*)Back_Queue(SeqQueue);
printf("Name:%s,Age:%d\n", SBack->name, SBack->age);
printf("------------------------\n");
//获取队列的每个元素
while (SeqQueue->size>0)
{
student* s = (student*)Front_Queue(SeqQueue);
printf("Name:%s,Age:%d\n",s->name,s->age);
Pop_Queue(SeqQueue);
}
Destroy_Queue(SeqQueue);
getchar();
return 0;
}
打印结果
队列的链式存储
queue_link.h
#pragma once
#include<stdlib.h>
#include<stdio.h>
//链式队列节点
typedef struct QUEUENODE{
struct QUEUENODE* next;
}QueueNode;
//队列
typedef struct LINKQUEUE{
QueueNode header;
int size;
}LinkQueue;
typedef void* LQueue;
//初始化
LQueue Init_LinkQueue();
//入队
void Push_LinkQueue(LQueue queue, QueueNode* data);
//出队
void Pop_LinkQueue(LQueue queue);
//获得队头元素
void* Front_LinkQueue(LQueue queue);
//获得队尾元素
void* Back_LinkQueue(LQueue queue);
//队列大小
int Size_LinkQueue(LQueue queue);
//销毁队列
void Destroy_LinkQueue(LQueue queue);
#include"queue_link.h"
//初始化
LQueue Init_LinkQueue()
{
LinkQueue* lqueue = (LinkQueue*)malloc(sizeof(LinkQueue));
if (NULL == lqueue)
{
printf("Init_LinkQueue lqueue malloc error\n");
}
lqueue->header.next = NULL;
lqueue->size = 0;
return lqueue;
}
//入队
void Push_LinkQueue(LQueue queue, QueueNode* data)
{
if (NULL == queue)
{
printf("Push_LinkQueue queue is NULL\n");
return;
}
if (NULL == data)
{
printf("Push_LinkQueue data is NULL\n");
return;
}
LinkQueue* lqueue = (LinkQueue*)queue;
//辅助指针,找到最后一个元素
QueueNode* Pcurrent = &(lqueue->header);
int i=0;
for (; i < lqueue->size; ++i)
{
Pcurrent = Pcurrent->next;
}
//新元素入队列
data->next = Pcurrent->next;
Pcurrent->next = data;
++lqueue->size;
}
//出队
void Pop_LinkQueue(LQueue queue)
{
if (NULL == queue)
{
printf("Pop_LinkQueue queue is NULL\n");
return;
}
LinkQueue* lqueue = (LinkQueue*)queue;
if (lqueue->size == 0)
{
return;
}
//辅助指针
QueueNode* Pcurrent = lqueue->header.next;
lqueue->header.next = Pcurrent->next;
--lqueue->size;
}
//获得队头元素
void* Front_LinkQueue(LQueue queue)
{
if (NULL == queue)
{
printf("Front_LinkQueue queue is NULL\n");
return NULL;
}
LinkQueue* lqueue = (LinkQueue*)queue;
if (lqueue->size == 0)
{
return NULL;
}
return lqueue->header.next;
}
//获得队尾元素
void* Back_LinkQueue(LQueue queue)
{
if (NULL == queue)
{
printf("Back_LinkQueue queue is NULL\n");
return NULL;
}
LinkQueue* lqueue = (LinkQueue*)queue;
if (lqueue->size == 0)
{
return NULL;
}
QueueNode* Pcurrent = &(lqueue->header);
int i = 0;
for (; i < lqueue->size; ++i)
{
Pcurrent = Pcurrent->next;
}
return Pcurrent;
}
//队列大小
int Size_LinkQueue(LQueue queue)
{
if (NULL == queue)
{
printf("Size_LinkQueue queue is NULL\n");
return -1;
}
LinkQueue* lqueue = (LinkQueue*)queue;
return lqueue->size;
}
//销毁队列
void Destroy_LinkQueue(LQueue queue)
{
if (NULL == queue)
{
printf("Destroy_LinkQueue queue is NULL\n");
return;
}
free(queue);
}
main.c
#include"queue_link.h"
typedef struct STUDENT2
{
QueueNode header;//前四个字节存放下一个节点的地址(指向下一个节点)
char name[64];
int age;
}student;
int main()
{
student s1 = { NULL, "aaaa", 10 };
student s2 = { NULL, "aaaa", 20 };
student s3 = { NULL, "aaaa", 30 };
student s4 = { NULL, "aaaa", 40 };
student s5 = { NULL, "aaaa", 50 };
LQueue lqueue = Init_LinkQueue();
Push_LinkQueue(lqueue, &s1);
Push_LinkQueue(lqueue, &s2);
Push_LinkQueue(lqueue, &s3);
Push_LinkQueue(lqueue, &s4);
Push_LinkQueue(lqueue, &s5);
//获得队列大小
LinkQueue* queue = (LinkQueue*)lqueue;
int size = queue->size;
printf("Size=%d\n",size);
//获得队尾元素
student* Back = (student*)Back_LinkQueue(lqueue);
printf("Name:%s,Age:%d\n", Back->name, Back->age);
printf("-------------------------------------\n");
//取出所有队列元素
while (queue->size>0)
{
student* s = (student*)Front_LinkQueue(lqueue);
printf("Name:%s,Age:%d\n",s->name,s->age);
Pop_LinkQueue(lqueue);
}
Destroy_LinkQueue(lqueue);
getchar();
return 0;
}
打印结果
上一篇: C语言重构【11】盛最多水的容器