0x00数据结构——C语言实现(队列)

0x00数据结构——C语言实现(队列)

实现

/*
使用队列时,插入在一端进行而删除在另一端进行
队列的基本操作是入队(enqueue),它是在表的末端(队尾(rear))插入一个元素,
还有出队(dequeue),它是删除(或返回)在表的开头(队头(front))的元素。
dequeue(Q) enqueue(Q,X)
=================
| |
<---------------| queue Q |<-----------------
| |
=================
利用
functions:
创建一个空的队列;
判断队列是不是空的;
判断队列是不是满的;
释放队列所占用的空间;
将队列置空;
出队;
获得队头元素;
入队;
输出;
*/
#ifndef QUEUE_H
#define QUEUE_H
typedef enum {
false = 0,
true
} BOOL;
struct node;
typedef struct node node;
typedef node *pos;
typedef node *queue;
// 创建一个空的队列;
queue create_queue(void);
// 判断队列是不是空的;
BOOL is_empty(queue Q);
// 判断队列是不是满的;
BOOL is_full(queue Q);
// 将队列置空;
BOOL set_empty(queue Q);
// 释放队列所占用的空间;
BOOL delete_queue(queue *Q);
// 出队;
int dequeue(queue Q);
// 获得队头元素;
int front_queue(queue Q);
// 入队;
void enqueue(queue Q, int x);
// 输出;
void print_queue(queue Q);
#endif
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
#define MAXQUEUE 100
struct node {
int val;
struct node *next;
struct node *pre;
};
//struct node;
//typedef struct node node;
//typedef node *pos;
//typedef node *queue;
// 创建一个空的队列;
queue create_queue(void)
{
queue tmp = (queue)malloc(sizeof(node));
tmp->val = 0;
tmp->next = NULL;//附加头节点的next域指向队头
tmp->pre = NULL;//附加头节点的pre域指向队尾
return tmp;
}
// 判断队列是不是空的;
BOOL is_empty(queue Q)
{
return (Q->val == 0);
}
// 判断队列是不是满的;
BOOL is_full(queue Q)
{
return (Q->val == MAXQUEUE);
}
// 将队列置空;
BOOL set_empty(queue Q)
{
node *tmp = Q->next;
while(tmp!=NULL) {
(tmp->pre)->next = tmp->next;
(tmp->next)->pre = tmp->pre;
free(tmp);
(Q->val)--;
tmp = Q->next;
}
return true;
}
// 释放队列所占用的空间;
BOOL delete_queue(queue *Q)
{
set_empty(*Q);
free(*Q);
*Q = NULL;
return true;
}
// 出队;
int dequeue(queue Q)
{
if(is_empty(Q)) {
printf("Q is empty\n");
return -1;
}
node *tmp = Q->next;
int r = tmp->val;
Q->next = tmp->next;
if(tmp->next != NULL) {
(tmp->next)->pre = NULL;
}
(Q->val)--;
return r;
}
// 获得队头元素;
int front_queue(queue Q)
{
return (Q->next)->val;
}
// 入队;
void enqueue(queue Q, int x)
{
if(is_full(Q)) {
printf("Q is full\n");
}
node *tmp = (queue)malloc(sizeof(node));
tmp->val = x;
if(Q->pre != NULL) {
(Q->pre)->next = tmp;
tmp->pre = (Q->pre);
tmp->next = NULL;
Q->pre = tmp;
} else {
tmp->pre = NULL;
tmp->next =NULL;
Q->pre = tmp;
Q->next = tmp;
}
(Q->val)++;
}
// 输出;
void print_queue(queue Q)
{
node *tmp = Q->next;
if(tmp == NULL) {
printf("->");
} else {
while(tmp != NULL) {
printf("->%d", tmp->val);
tmp = tmp->next;
}
}
printf("\n");
}

实验

#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
int main()
{
queue Q;
int i;
Q = create_queue();
for(i = 0; i<10; i++) {
print_queue(Q);
enqueue(Q, i);
}
while(!is_empty(Q)) {
dequeue(Q);
print_queue(Q);
}
return 0;
}

实验结果

->
->0
->0->1
->0->1->2
->0->1->2->3
->0->1->2->3->4
->0->1->2->3->4->5
->0->1->2->3->4->5->6
->0->1->2->3->4->5->6->7
->0->1->2->3->4->5->6->7->8
->1->2->3->4->5->6->7->8->9
->2->3->4->5->6->7->8->9
->3->4->5->6->7->8->9
->4->5->6->7->8->9
->5->6->7->8->9
->6->7->8->9
->7->8->9
->8->9
->9
->
Time elapsed: 000:00:031
Press any key to continue
posted @   main_c  阅读(219)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示