🖊作者 : D. Star.
📘专栏 : 数据结构
😆今日分享 :"混账"的由来 :
----------从前,蒙古族过着群居的游牧生活,白天,男人们出去放牧,只留下老人或妇女看守帐篷。由于种种因素,留在家的年轻小伙子们为了找姑娘谈情说爱,便混进姑娘们的帐篷里去。有时候,老人会愤怒地骂上句“你又混帐来了”“混帐东西又来了”。小伙子讨个没趣,也就急忙退出来。久而久之,“混账”一词演化为一句骂人的话,与当初的“混进帐篷”已无关系了。
队列 :
✔头文件-声明:
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<assert.h>
typedef int QDataType;
typedef struct QueueNode
{
QDataType data;
struct QueueNode* next;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
}Queue;
//初始化
Queue* InitQueue();
//销毁
void DestroyQueue(Queue* pq);
//入队
void PushQueue(Queue* pq,QDataType x);
//出队
void PopQueue(Queue* pq);
//取头
QDataType QueueFront(Queue* pq);
//取尾
QDataType QueueBack(Queue* pq);
//判断是否为空
bool EmptyQueue(Queue* pq);
//队列的长度
int SizeQueue(Queue* pq);
✔队列功能实现:
🔎初始化:
//初始化
Queue* InitQueue()
{
Queue* pq = (Queue*)malloc(sizeof(Queue));
if (pq == NULL)
{
perror("malloc fail");
exit(-1);
}
pq->head = NULL;
pq->tail = NULL;
pq->size = 0;
return pq;
}
🔎销毁:
//销毁
void DestroyQueue(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* del = cur;
cur = cur->next;
free(del);
}
pq->head =pq->tail = NULL;
pq->size = 0;
}
🔎入队:
//入队
void PushQueue(Queue* pq, QDataType x)
{
assert(pq);
QNode* newNode = (QNode*)malloc(sizeof(QNode));
if (newNode == NULL)
{
perror("malloc fail");;
exit(-1);
}
newNode->data = x;
newNode->next = NULL;
if (pq->tail == NULL)
{
pq->head = pq->tail = newNode;
}
else
{
pq->tail->next = newNode;
pq->tail = newNode;
}
pq->size++;
}
🔎出队:
//出队
void PopQueue(Queue* pq)
{
assert(pq);
assert(!EmptyQueue(pq));
if (pq->head != pq->tail)
{
QNode* del = pq->head;
pq->head = pq->head->next;
free(del);
}
else
{
free(pq->head);
pq->head = pq->tail = NULL;
}
pq->size--;
}
🔎取头:
//取头
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!EmptyQueue(pq));
return pq->head->data;
}
🔎取尾:
//取尾
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!EmptyQueue(pq));
return pq->tail->data;
}
🔎判断是否为空:
//判断是否为空
bool EmptyQueue(Queue* pq)
{
assert(pq);
return pq->head == NULL && pq->tail == NULL;
}
🔎队列的长度:
//队列的长度
int SizeQueue(Queue* pq)
{
assert(pq);
时间复杂度为N;
//int size = 0;
//QNode* cur = pq->head;
//while (cur)
//{
// cur = cur->next;
// size++;
//}
//时间时间复杂度为1;
return pq->size;
}
✔测试文件
#define _CRT_SECURE_NO_WARNINGS
#include"QueueNode.h"
//队列
test01()
{
Queue* pq = InitQueue();
PushQueue(pq, 1);
PushQueue(pq, 2);
PushQueue(pq, 3);
PushQueue(pq, 4);
printf("%d\n", QueueFront(pq));//1
//PopQueue(pq);
printf("%d\n", QueueBack(pq));//4
PopQueue(pq);//2 3 4
PopQueue(pq);//3 4
PopQueue(pq);//4
printf("%d\n", QueueFront(pq));//4
循环打印队列中的值
//while (!EmptyQueue(pq))
//{
// printf("%d ",QueueFront(pq));
// PopQueue(pq);
//}
DestroyQueue(pq);
}
main()
{
test01();
system("pause");
return 0;
}
感谢家人的阅读,若有不准确的地方 欢迎在评论区指正!