怎么用ajax做电商网站,如何建设一个个人网站,wordpress默认邮件在哪里设置密码,活动发布类网站开发目录
1.队列
1.1队列的概念及结构
1.2 队列的实际应用联想
1.3队列的实现
2. 队列应用——队列实现栈
主要思路 1.队列
1.1队列的概念及结构 队列#xff1a;只允许在一端进行插入数据操作#xff0c;在另一端进行删除数据操作的特殊线性表#xff0c;队列具有先进…目录
1.队列
1.1队列的概念及结构
1.2 队列的实际应用联想
1.3队列的实现
2. 队列应用——队列实现栈
主要思路 1.队列
1.1队列的概念及结构 队列只允许在一端进行插入数据操作在另一端进行删除数据操作的特殊线性表队列具有先进先出FIFO(First In First Out) 入队列进行插入操作的一端称为 队尾 出队列进行删除操作的一端称为 队头 1.2 队列的实际应用联想
1. 基于队列的特点--先进先出可以联想到大厅服务中的叫号机先到的先拿号先拿号的先办业务队列中的人是在等的人办完业务为出队列取号为如队列。
2. 也可以是查找和自己间接相关的人像是查找qq好友的好友开始自己在队列中后来将自己出队列将自己好友入队列再将队列中的人出来他们的好友再入队列当然入队列的时候肯定有限制不能是和之前重复的以此类推即可知道和自己外围相关的人
注仅为有代码新人的有端联想不一定真正适合这些情况
1.3队列的实现 队列也可以数组和链表的结构实现使用链表的结构实现更优一些因为如果使用数组的结构出队列在数组头上出数据效率会比较低。 下面我们来实现队列 分两个文件Queue.h与Queue.c Queue.h进行头文件和函数的声明以及队列结构的创建 #pragma once
#include stdio.h
#include assert.h
#include stdlib.h
#include stdbool.h//方便后面对不同数据的操作
typedef int QDataType;//链式队列-队列结点结构体
typedef struct QListNode
{QDataType _data;struct QListNode* _pnext;
}QNode;//队列结构体
typedef struct Queue
{QNode* _phead;QNode* _ptail;int _size;
}Queue;// 初始化队列
void QueueInit(Queue* q);
// 队尾入队列
void QueuePush(Queue* q, QDataType data);
// 队头出队列
void QueuePop(Queue* q);
// 获取队列头部元素
QDataType QueueFront(Queue* q);
// 获取队列队尾元素
QDataType QueueBack(Queue* q);
// 获取队列中有效元素个数
int QueueSize(Queue* q);
// 检测队列是否为空如果为空返回非零结果如果非空返回0
bool QueueEmpty(Queue* q);
// 销毁队列
void QueueDestroy(Queue* q);Queue.c进行队列相关函数的实现 #include Queue.h//队列初始化
void QueueInit(Queue* pst)
{assert(pst);pst-_phead pst-_ptail NULL;pst-_size 0;
}// 队尾入队列
void QueuePush(Queue* pst,QDataType x)
{assert(pst);QNode* newnode (QNode*)malloc(sizeof(QNode));newnode-_data x;newnode-_pnext NULL;if (pst-_phead NULL){pst-_phead pst-_ptail newnode;}else{pst-_ptail-_pnext newnode;pst-_ptail newnode;}pst-_size;
}// 队头出队列
void QueuePop(Queue* pst)
{assert(pst);assert(pst-_size);QNode* next pst-_phead-_pnext;free(pst-_phead);pst-_phead next;pst-_size--;
}// 获取队列队尾元素
QDataType QueueBack(Queue* pst)
{assert(pst);assert(pst-_size);return pst-_ptail-_data;
}// 获取队列头部元素
QDataType QueueFront(Queue* pst)
{assert(pst);assert(pst-_size);return pst-_phead-_data;
}// 获取队列中有效元素个数
int QueueSize(Queue* pst)
{assert(pst);return pst-_size;
}// 检测队列是否为空如果为空返回True如果非空返回False
bool QueueEmpty(Queue* pst)
{assert(pst);return pst-_size 0;
}// 销毁队列
void QueueDestroy(Queue* pst)
{assert(pst);QNode* cur pst-_phead;while (cur){QNode* next cur-_pnext;free(cur);cur next;}pst-_size 0;pst-_phead pst-_ptail NULL;
} 2. 队列应用——队列实现栈 225. 用队列实现栈 - 力扣LeetCode 题目 我们用直接使用前队列的代码实现栈。 主要思路 创建两个队列相互导数据实现后进先出的效果压数据时直接在有数据的那个队列中插入数据出栈时将有数据的队列中的size-1个数据导入另一个队列里则第一个队列中剩下的那个数据是我们要出栈的数据这就是主要思路。 下面是代码演示代码有点长但是前面的大部分都是前面实现队列的代码直接复制粘贴的 typedef int QDataType;
// 链式队列-队列结点结构体
typedef struct QListNode {QDataType _data;struct QListNode* _pnext;
} QNode;// 队列结构体
typedef struct Queue {QNode* _phead;QNode* _ptail;int _size;
} Queue;// 队列初始化
void QueueInit(Queue* pst) {assert(pst);pst-_phead pst-_ptail NULL;pst-_size 0;
}// 队尾入队列
void QueuePush(Queue* pst, QDataType x) {assert(pst);QNode* newnode (QNode*)malloc(sizeof(QNode));newnode-_data x;newnode-_pnext NULL;if (pst-_phead NULL) {pst-_phead pst-_ptail newnode;} else {pst-_ptail-_pnext newnode;pst-_ptail newnode;}pst-_size;
}// 队头出队列
void QueuePop(Queue* pst) {assert(pst);assert(pst-_size);QNode* next pst-_phead-_pnext;free(pst-_phead);pst-_phead next;pst-_size--;
}// 获取队列队尾元素
QDataType QueueBack(Queue* pst) {assert(pst);assert(pst-_size);return pst-_ptail-_data;
}// 获取队列头部元素
QDataType QueueFront(Queue* pst) {assert(pst);assert(pst-_size);return pst-_phead-_data;
}// 获取队列中有效元素个数
int QueueSize(Queue* pst) {assert(pst);return pst-_size;
}// 检测队列是否为空如果为空返回True如果非空返回False
bool QueueEmpty(Queue* pst) {assert(pst);return pst-_size 0;
}// 销毁队列
void QueueDestroy(Queue* pst) {assert(pst);QNode* cur pst-_phead;while (cur) {QNode* next cur-_pnext;free(cur);cur next;}pst-_size 0;pst-_phead pst-_ptail NULL;
}typedef struct {Queue p1;Queue p2;
} MyStack;MyStack* myStackCreate() {MyStack* st (MyStack*)malloc(sizeof(MyStack));QueueInit((st-p1));QueueInit((st-p2));return st;
}void myStackPush(MyStack* obj, int x) {if (!QueueEmpty((obj-p1))) {QueuePush((obj-p1), x);} else {QueuePush((obj-p2), x);}
}int myStackPop(MyStack* obj) {Queue* empty (obj-p1);Queue* nonempty (obj-p2);if (!QueueEmpty(empty)) {empty (obj-p2);nonempty (obj-p1);}while (QueueSize(nonempty) 1) {QueuePush(empty, QueueFront(nonempty));QueuePop(nonempty);}int ret QueueFront(nonempty);QueuePop(nonempty);return ret;
}int myStackTop(MyStack* obj) {if (!QueueEmpty(obj-p1)) {return QueueBack((obj-p1));} else {return QueueBack((obj-p2));}
}
bool myStackEmpty(MyStack* obj) {return QueueEmpty((obj-p1)) QueueEmpty((obj-p2));
}void myStackFree(MyStack* obj) {QueueDestroy((obj-p1));QueueDestroy((obj-p2));free(obj);
}各位看官姥爷点个赞再走吧欢迎在评论区讨论。