当前位置: 首页 > news >正文

世界疫情最新数据排名表2022年网站怎么优化关键词

世界疫情最新数据排名表2022年,网站怎么优化关键词,第三方电子商务平台有哪些优势,网站建设样式目录 队列一. 队列的概念及结构二. 队列的实现1. 要实现的功能2 具体的实现2.1 结构体2.2 初始化2.3 入队列2.4 出队列2.5 返回队首元素2.6 返回队尾元素2.7 队列元素个数2.8 队列判空2.9 队列销毁 三. 队列相关OJ题设计循环队列用队列实现栈用栈实现队列 四. 概念选择题五. 参…

目录

  • 队列
    • 一. 队列的概念及结构
    • 二. 队列的实现
      • 1. 要实现的功能
      • 2 具体的实现
        • 2.1 结构体
        • 2.2 初始化
        • 2.3 入队列
        • 2.4 出队列
        • 2.5 返回队首元素
        • 2.6 返回队尾元素
        • 2.7 队列元素个数
        • 2.8 队列判空
        • 2.9 队列销毁
    • 三. 队列相关OJ题
      • 设计循环队列
      • 用队列实现栈
      • 用栈实现队列
    • 四. 概念选择题
    • 五. 参考代码
      • Queue.h
      • Queue.c
      • test.c

队列

一. 队列的概念及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out)
入队列:进行插入操作的一端称为队尾
出队列:进行删除操作的一端称为队头
队列的先进先出

二. 队列的实现

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。
入队列与出队列

1. 要实现的功能

// 链式结构:表示队列
typedef struct QListNode
{
struct QListNode* _pNext;
QDataType _data;
}QNode;
// 队列的结构
typedef struct Queue
{
QNode* _front;
QNode* _rear;
}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
int QueueEmpty(Queue* q);
// 销毁队列
void QueueDestroy(Queue* q);

另外扩展了解一下,实际中我们有时还会使用一种队列叫循环队列。如操作系统课程讲解生产者消费者模型时可以就会使用循环队列。环形队列可以使用数组实现,也可以使用循环链表实现。
循环队列

2 具体的实现

本次队列的实现我们基于双向链表

2.1 结构体

为了适配多种数据类型,这里使用typedef对数据类型进行重命名,便于进行统一修改。
由于链表QueueNode的节点只含有next指针和值,所以我们额外定义一个结构体Queue记录下链表的头和尾,便于快速进行相关操作。

typedef int QDataType;typedef struct QueueNode
{struct QueueNode* next;QDataType val;
}QNode;typedef struct Queue
{QNode* phead; QNode* ptail;int size;
}Queue;
2.2 初始化

初始化将记录的头和尾置空,元素个数置空

void QueueInit(Queue* pq)
{assert(pq);pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}
2.3 入队列

入队列创建一个新节点,并放在记录好的尾节点的位置之后,成为新的尾节点,最后元素个数增加。

void QueuePush(Queue* pq, QDataType x)
{QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc failed");exit(1);}newnode->next = NULL;newnode->val = x;if (pq->ptail == NULL){pq->phead = newnode;pq->ptail = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}
2.4 出队列

出队列释放掉第一个节点(头节点),然后让第二个节点成为新的头节点,最后元素个数减少。

void QueuePop(Queue* pq)
{assert(pq);assert(pq->size);QNode* next = pq->phead->next;free(pq->phead);pq->phead = next;if (pq->phead == NULL)//just one node{pq->ptail = NULL;}pq->size--;
}
2.5 返回队首元素

返回我们记录的头节点的值即可。

QDataType QueueFront(Queue* pq)
{assert(pq);assert(pq->phead);return pq->phead->val;
}
2.6 返回队尾元素

返回我们记录的尾节点的值即可。

QDataType QueueBack(Queue* pq)
{assert(pq);assert(pq->ptail);return pq->ptail->val;
}
2.7 队列元素个数

返回我们记录的元素个数即可。

int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}
2.8 队列判空

返回我们记录的元素个数是否为零。

_Bool QueueEmpty(Queue* pq)
{assert(pq);return pq->size == 0;
}
2.9 队列销毁

类似于单链表的销毁,依次销毁每一个节点后将头尾置空,将元素个数置空。

void QueueDestroy(Queue* pq)
{assert(pq);QNode* pcur = pq->phead;while (pcur){QNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}

三. 队列相关OJ题

设计循环队列

题目链接:设计循环队列
循环队列

//顺序表
typedef struct 
{int* a;int head;int tail;int k;    
} MyCircularQueue;bool myCircularQueueIsEmpty(MyCircularQueue* obj);
bool myCircularQueueIsFull(MyCircularQueue* obj);MyCircularQueue* myCircularQueueCreate(int k) 
{MyCircularQueue* pq = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));//多开辟一个空间解决假溢出问题pq->a = malloc(sizeof(int) * (k + 1));pq->head = 0;pq->tail = 0;pq->k = k;return pq;
}bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) 
{if(myCircularQueueIsFull(obj)){return false;}obj->a[obj->tail] = value;obj->tail++;obj->tail %= (obj->k + 1);return true;
}bool myCircularQueueDeQueue(MyCircularQueue* obj) 
{if(myCircularQueueIsEmpty(obj)){return false;}   obj->head++;obj->head %= obj->k + 1;return true;
}int myCircularQueueFront(MyCircularQueue* obj)
{if(myCircularQueueIsEmpty(obj)){return EOF;}else{return obj->a[obj->head];}   
}int myCircularQueueRear(MyCircularQueue* obj) 
{if(myCircularQueueIsEmpty(obj)){return EOF;}else{//return obj->tail == 0 ? obj->a[obj->k] : obj->a[obj->tail - 1];//return obj->a[(obj->tail - 1 + obj->k + 1) % (obj->k + 1)];return obj->a[(obj->tail + obj->k) % (obj->k + 1)];}
}bool myCircularQueueIsEmpty(MyCircularQueue* obj) 
{return obj->head == obj->tail;    
}bool myCircularQueueIsFull(MyCircularQueue* obj) 
{return (obj->tail + 1) % (obj->k + 1) == obj->head;    
}void myCircularQueueFree(MyCircularQueue* obj) 
{free(obj->a);free(obj);
}/*** Your MyCircularQueue struct will be instantiated and called as such:* MyCircularQueue* obj = myCircularQueueCreate(k);* bool param_1 = myCircularQueueEnQueue(obj, value);* bool param_2 = myCircularQueueDeQueue(obj);* int param_3 = myCircularQueueFront(obj);* int param_4 = myCircularQueueRear(obj);* bool param_5 = myCircularQueueIsEmpty(obj);* bool param_6 = myCircularQueueIsFull(obj);* myCircularQueueFree(obj);
*/

用队列实现栈

题目链接:用队列实现栈

typedef int QDataType;typedef struct QueueNode
{struct QueueNode* next;QDataType val;
}QNode;typedef struct Queue
{QNode* phead; QNode* ptail;int size;
}Queue;队尾插入
//void QueuePush(QNode** pphead, QNode** pptail, QDataType x);
//
队头删除
//void QueuePop(QNode** pphead, QNode** pptail);//初始化
void QueueInit(Queue* pq);//队尾插入
void QueuePush(Queue* pq, QDataType x);//队头删除
void QueuePop(Queue* pq);//取队头数据
QDataType QueueFront(Queue* pq);//取队尾数据
QDataType QueueBack(Queue* pq);//队列元素个数
int QueueSize(Queue* pq);//判空
_Bool QueueEmpty(Queue* pq);//队列的销毁
void QueueDestroy(Queue* pq);void QueueInit(Queue* pq)
{assert(pq);pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}void QueuePush(Queue* pq, QDataType x)
{QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc failed");exit(1);}newnode->next = NULL;newnode->val = x;if (pq->ptail == NULL){pq->phead = newnode;pq->ptail = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}void QueuePop(Queue* pq)
{assert(pq);assert(pq->size);QNode* next = pq->phead->next;free(pq->phead);pq->phead = next;if (pq->phead == NULL){pq->ptail = NULL;}pq->size--;
}QDataType QueueFront(Queue* pq)
{assert(pq);assert(pq->phead);return pq->phead->val;
}QDataType QueueBack(Queue* pq)
{assert(pq);assert(pq->ptail);return pq->ptail->val;
}int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}_Bool QueueEmpty(Queue* pq)
{assert(pq);return pq->size == 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QNode* pcur = pq->phead;while (pcur){QNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}typedef struct {Queue q1;Queue q2;
} MyStack;MyStack* myStackCreate() 
{MyStack* pst = (MyStack*)malloc(sizeof(MyStack));if(pst == NULL){perror("malloc failed");exit(1);}QueueInit(&pst->q1);QueueInit(&pst->q2);return pst;
}void myStackPush(MyStack* obj, int x) 
{if(!QueueEmpty(&(obj->q1))){QueuePush(&(obj->q1), x);}else{QueuePush(&(obj->q2), x);}
}int myStackPop(MyStack* obj) 
{//如果队列不为空,就把前size - 1个数据放在另一个队列中,释放最后一个节点的数据Queue* emp = &(obj->q1);//假设q1为空Queue* noemp = &(obj->q2);if(QueueEmpty(&(obj->q2)))//如果q2为空{emp = &(obj->q2);noemp = &(obj->q1);}int sz = QueueSize(noemp);while(sz > 1){QueuePush(emp, QueueFront(noemp));QueuePop(noemp);sz--;}int top = QueueFront(noemp);QueuePop(noemp);return top;
}int myStackTop(MyStack* obj) 
{if(!QueueEmpty(&(obj->q1))){return QueueBack(&(obj->q1));}else{return QueueBack(&(obj->q2));}
}bool myStackEmpty(MyStack* obj) 
{return QueueEmpty(&(obj->q1)) && QueueEmpty(&(obj->q2));
}void myStackFree(MyStack* obj) 
{QueueDestroy(&obj->q1);QueueDestroy(&obj->q2);free(obj);
}/*** Your MyStack struct will be instantiated and called as such:* MyStack* obj = myStackCreate();* myStackPush(obj, x);* int param_2 = myStackPop(obj);* int param_3 = myStackTop(obj);* bool param_4 = myStackEmpty(obj);* myStackFree(obj);
*/

用栈实现队列

题目链接:用栈实现队列
用栈实现队列

typedef int STDataType;typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;//初始化
void STInit(ST* pst);//销毁
void STDestroy(ST* pst);//插入数据(入栈)
void STPush(ST* pst, STDataType x);//删除数据(出栈)
void STPop(ST* pst);//获取栈顶数据
STDataType STTop(ST* pst);//判空
_Bool STEmpty(ST* pst);//获取数据个数
int STSize(ST* pst);void STInit(ST* pst)
{assert(pst);pst->a = NULL;pst->capacity = 0;// top指向栈顶数据的下一个位置pst->top = 0; top指向栈顶数据//pst->top = -1;
}void STDestroy(ST* pst)
{assert(pst);free(pst->a);pst->a = NULL;pst->capacity = 0;pst->top = 0;
}void STPush(ST* pst, STDataType x)
{assert(pst);//扩容if (pst->top == pst->capacity){int newcapacity = (pst->capacity == 0) ? 4 : (2 * pst->capacity);STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));if (tmp == NULL){perror("realloc failed");exit(1);}pst->a = tmp;pst->capacity = newcapacity;}//存放数据pst->a[pst->top] = x;pst->top++;
}void STPop(ST* pst)
{assert(pst);assert(pst->top > 0);pst->top--;
}STDataType STTop(ST* pst)
{assert(pst);assert(pst->top > 0);return pst->a[pst->top - 1];
}_Bool STEmpty(ST* pst)
{assert(pst);return pst->top == 0;
}int STSize(ST* pst)
{assert(pst);return pst->top;
}typedef struct 
{ST s1;ST s2;
} MyQueue;MyQueue* myQueueCreate() 
{MyQueue* pqe = (MyQueue*)malloc(sizeof(MyQueue));if(pqe == NULL){perror("malloc failed");exit(1);}STInit(&(pqe->s1));STInit(&(pqe->s2));return pqe;
}void myQueuePush(MyQueue* obj, int x) 
{if(!STEmpty(&(obj->s1))){STPush(&(obj->s1), x);}else{STPush(&(obj->s2), x);}
}int myQueuePop(MyQueue* obj)
{ST* emp = &(obj->s1);//假设s1为空ST* noemp = &(obj->s2);if (STEmpty(&(obj->s2)))//如果s2为空{emp = &(obj->s2);noemp = &(obj->s1);}int sz = STSize(noemp);if (sz == 1){int top = STTop(noemp);STPop(noemp);return top;}else{while (sz > 1){STPush(emp, STTop(noemp));STPop(noemp);sz--;}int top = STTop(noemp);STPop(noemp);ST* tmp = noemp;noemp = emp;emp = tmp;sz = STSize(noemp);while (sz > 0){STPush(emp, STTop(noemp));STPop(noemp);sz--;}return top;}
}int myQueuePeek(MyQueue* obj)
{ST* emp = &(obj->s1);//假设s1为空ST* noemp = &(obj->s2);if (STEmpty(&(obj->s2)))//如果s2为空{emp = &(obj->s2);noemp = &(obj->s1);}int sz = STSize(noemp);if (sz == 1){int top = STTop(noemp);return top;}else{while (sz > 1){STPush(emp, STTop(noemp));STPop(noemp);sz--;}int top = STTop(noemp);STPush(emp, STTop(noemp));STPop(noemp);ST* tmp = noemp;noemp = emp;emp = tmp;sz = STSize(noemp);while (sz > 0){STPush(emp, STTop(noemp));STPop(noemp);sz--;}return top;}
}bool myQueueEmpty(MyQueue* obj) 
{return STEmpty(&obj->s1) && STEmpty(&obj->s2);
}void myQueueFree(MyQueue* obj) 
{STDestroy(&obj->s1);STDestroy(&obj->s2);free(obj);  
}/*** Your MyQueue struct will be instantiated and called as such:* MyQueue* obj = myQueueCreate();* myQueuePush(obj, x);* int param_2 = myQueuePop(obj);* int param_3 = myQueuePeek(obj);* bool param_4 = myQueueEmpty(obj);* myQueueFree(obj);
*/

四. 概念选择题

  1. 循环队列的存储空间为 Q(1:100) ,初始状态为front=rear=100 。经过一系列正常的入队与退队操作后, front=rear=99 ,则循环队列中的元素个数为( )
    A 1
    B 2
    C 99
    D 0或者100
  2. 以下( )不是队列的基本运算?
    A 从队尾插入一个新元素
    B 从队列中删除第i个元素
    C 判断一个队列是否为空
    D 读取队头元素的值
  3. 现有一循环队列,其队头指针为front,队尾指针为rear;循环队列长度为N。其队内有效长度为?(假设队头不存放数据)
    A (rear - front + N) % N + 1
    B (rear - front + N) % N
    C ear - front) % (N + 1)
    D (rear - front + N) % (N - 1)
    答案
  4. D
  5. B
  6. B

五. 参考代码

Queue.h

#pragma once#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>typedef int QDataType;typedef struct QueueNode
{struct QueueNode* next;QDataType val;
}QNode;typedef struct Queue
{QNode* phead; QNode* ptail;int size;
}Queue;队尾插入
//void QueuePush(QNode** pphead, QNode** pptail, QDataType x);
//
队头删除
//void QueuePop(QNode** pphead, QNode** pptail);//初始化
void QueueInit(Queue* pq);//队尾插入
void QueuePush(Queue* pq, QDataType x);//队头删除
void QueuePop(Queue* pq);//取队头数据
QDataType QueueFront(Queue* pq);//取队尾数据
QDataType QueueBack(Queue* pq);//队列元素个数
int QueueSize(Queue* pq);//判空
_Bool QueueEmpty(Queue* pq);//队列的销毁
void QueueDestroy(Queue* pq);

Queue.c

#include "Queue.h"void QueueInit(Queue* pq)
{assert(pq);pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}void QueuePush(Queue* pq, QDataType x)
{QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc failed");exit(1);}newnode->next = NULL;newnode->val = x;if (pq->ptail == NULL){pq->phead = newnode;pq->ptail = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}void QueuePop(Queue* pq)
{assert(pq);assert(pq->size);QNode* next = pq->phead->next;free(pq->phead);pq->phead = next;if (pq->phead == NULL)//just one node{pq->ptail = NULL;}pq->size--;
}QDataType QueueFront(Queue* pq)
{assert(pq);assert(pq->phead);return pq->phead->val;
}QDataType QueueBack(Queue* pq)
{assert(pq);assert(pq->ptail);return pq->ptail->val;
}int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}_Bool QueueEmpty(Queue* pq)
{assert(pq);return pq->size == 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QNode* pcur = pq->phead;while (pcur){QNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}

test.c

#include "Queue.h"int main()
{Queue q;QueueInit(&q);QueuePush(&q, 1);QueuePush(&q, 2);QueuePush(&q, 3);QueuePush(&q, 4);QueuePush(&q, 5);while (!QueueEmpty(&q)){printf("%d ", QueueFront(&q));QueuePop(&q);}return 0;
}

文章转载自:
http://tartness.c7498.cn
http://francolin.c7498.cn
http://leaper.c7498.cn
http://cumshaw.c7498.cn
http://chemic.c7498.cn
http://reinform.c7498.cn
http://rammer.c7498.cn
http://monkshood.c7498.cn
http://peculator.c7498.cn
http://trination.c7498.cn
http://snobling.c7498.cn
http://nuyorican.c7498.cn
http://philosophise.c7498.cn
http://seconde.c7498.cn
http://brought.c7498.cn
http://courtly.c7498.cn
http://halt.c7498.cn
http://mosslike.c7498.cn
http://lactoovovegetarian.c7498.cn
http://incorrigibly.c7498.cn
http://connotational.c7498.cn
http://tappet.c7498.cn
http://basket.c7498.cn
http://ephemeral.c7498.cn
http://trichromic.c7498.cn
http://herbaceous.c7498.cn
http://schrank.c7498.cn
http://pulsatile.c7498.cn
http://troubled.c7498.cn
http://degeneracy.c7498.cn
http://semiworks.c7498.cn
http://perspicacious.c7498.cn
http://lambent.c7498.cn
http://crossbearer.c7498.cn
http://familism.c7498.cn
http://polyautography.c7498.cn
http://parageusia.c7498.cn
http://malefic.c7498.cn
http://intact.c7498.cn
http://bleomycin.c7498.cn
http://puncheon.c7498.cn
http://panencephalitis.c7498.cn
http://autorotate.c7498.cn
http://extendable.c7498.cn
http://panatrophy.c7498.cn
http://sacrificially.c7498.cn
http://biosonar.c7498.cn
http://vermicide.c7498.cn
http://thanatism.c7498.cn
http://landor.c7498.cn
http://auditorial.c7498.cn
http://replenishment.c7498.cn
http://thiller.c7498.cn
http://sematic.c7498.cn
http://euxenite.c7498.cn
http://bestir.c7498.cn
http://atonalism.c7498.cn
http://semiofficially.c7498.cn
http://alarmist.c7498.cn
http://obscurity.c7498.cn
http://flocculi.c7498.cn
http://release.c7498.cn
http://bscp.c7498.cn
http://demise.c7498.cn
http://crane.c7498.cn
http://halvah.c7498.cn
http://buildable.c7498.cn
http://feuilletonist.c7498.cn
http://wlan.c7498.cn
http://penumbra.c7498.cn
http://whenas.c7498.cn
http://discourtesy.c7498.cn
http://unilingual.c7498.cn
http://dulcet.c7498.cn
http://chartreuse.c7498.cn
http://latin.c7498.cn
http://naillike.c7498.cn
http://bliny.c7498.cn
http://colza.c7498.cn
http://trichinella.c7498.cn
http://strenuosity.c7498.cn
http://humiliatory.c7498.cn
http://antimissile.c7498.cn
http://confines.c7498.cn
http://lighthearted.c7498.cn
http://basil.c7498.cn
http://introvertive.c7498.cn
http://retropack.c7498.cn
http://herniation.c7498.cn
http://fungal.c7498.cn
http://beachnik.c7498.cn
http://messman.c7498.cn
http://leproid.c7498.cn
http://pleasantry.c7498.cn
http://mitis.c7498.cn
http://ophthalmoscope.c7498.cn
http://propel.c7498.cn
http://cerotic.c7498.cn
http://earpiece.c7498.cn
http://cinquedea.c7498.cn
http://www.zhongyajixie.com/news/71262.html

相关文章:

  • django 做网站赚钱免费网站制作教程
  • 网站佣金怎么做凭证许昌网络推广外包
  • 国外电商网站如何做icp备案seo快速排名外包
  • 浙江建设信息港网站查询分类信息网
  • 连云港商城网站开发设计免费h5制作网站
  • 企业网站开发技术题库网络关键词优化软件
  • 西安 房产网站建设电商seo搜索引擎优化
  • 免费建网站中文域名东莞网站快速排名提升
  • 手机站和微网站的区别qq群引流推广网站
  • 有口碑的大良网站建设搜狗收录提交入口
  • 谁有手机网站发几个吧网络营销渠道的特点
  • xml文件里做网站超链接网站广告投放收费标准
  • 专业制作网站cba目前排行
  • 广州网站排名百度人工服务在线咨询
  • 装置艺术那个网站做的好sem培训班
  • yw27777最新跳转接口seo黑帽技术
  • java web开发要学什么外贸网站推广seo
  • 温州建设网站制作百度app推广
  • 做动画视频的网站网站流量统计工具
  • ps做购物小网站微博推广费用一般多少
  • 广州企业网站建设多少钱网站优化联系
  • 平台卖货关键词优化排名软件案例
  • 公司的网站费怎样做会计分录网站建站公司
  • 做网站要不要用控件极速一区二区三区精品
  • 企业网站建设 租用服务器淘宝指数查询官网手机版
  • 有专门为个人网站做推广的吗电话销售如何快速吸引客户
  • 地图网站设计怎么建立企业网站
  • 站长工具视频域名交易域名出售
  • 湖北智能网站建设制作百度收录怎么查询
  • wp做网站需要多久优秀软文范例100字