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

白酒企业网站源码seo文章是什么意思

白酒企业网站源码,seo文章是什么意思,网站首页页面设计多少钱,国内做日化官方网站目录 一.【Leetcode225】队列实现栈 1.链接 2.题目再现 3.解法 二.【Leetcode232】栈实现队列 1.链接 2.题目再现 3.解法 一.【Leetcode225】队列实现栈 1.链接 队列实现栈 2.题目再现 3.解法 这道题给了我们两个队列,要求去实现栈; 首先&…

 

  

目录

一.【Leetcode225】队列实现栈

1.链接

2.题目再现

 3.解法

二.【Leetcode232】栈实现队列

1.链接

2.题目再现

3.解法


一.【Leetcode225】队列实现栈

1.链接

队列实现栈

2.题目再现

 3.解法

这道题给了我们两个队列,要求去实现栈;

首先,我们要知道栈和队列的特征:

栈:后进先出,只能从栈顶入数据和出数据;

队列:先进先出,从队尾入数据,队头出数据;

根据这些特点,我们可以采用两边倒的方法来实现;

具体来说:

1.入栈时就是在不为空的队列插入数据,若两个队列都为空,就随便插入到一个队列中;

2.出栈时将不为空的队列的数据倒入为空的队列中,当不为空的队列就剩一个数据时,就停止向空队列倒数据,然后再删点那最后一个数据;

3.判空时,需要两个队列都为空,才算栈为空;

4.取栈顶元素即取不为空的队列的队尾元素,在取栈顶元素前要判断栈是否为空;

5.销毁栈时,要先销毁其中的两个队列,然后再销毁栈。

因为是用C语言实现的,所以得自己手搓个队列。

typedef int Qdatatype;typedef struct QueueNode
{struct QueeuNode* next;Qdatatype data;
}QueueNode;typedef struct Queue
{QueueNode* head;QueueNode* tail;
}Queue;
void Queueinit(Queue* pq)
{assert(pq);pq->head = NULL;pq->tail = NULL;
}void Queuedestroy(Queue* pq)
{assert(pq);QueueNode* cur = pq->head;while (cur != pq->tail){QueueNode* next = cur->next;free(cur);cur = next;}pq->head = pq->tail = NULL;
}void Queuepush(Queue* pq, Qdatatype x)
{assert(pq);QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));if (newnode == NULL){perror("malloc fail");exit(-1);}newnode->data = x;newnode->next = NULL;if (pq->head == NULL){pq->head = pq->tail = newnode;}else{pq->tail->next = newnode;pq->tail = newnode;}
}void Queuepop(Queue* pq)
{assert(pq);assert(pq->head);QueueNode* next = pq->head->next;if (pq->head->next == NULL){free(pq->head);pq->head = pq->tail = NULL;}else{free(pq->head);pq->head = next;}
}Qdatatype Queuefront(Queue* pq)
{assert(pq);assert(pq->head);return pq->head->data;
}Qdatatype Queueback(Queue* pq)
{assert(pq);assert(Queuesize(pq) > 0);return pq->tail->data;
}int Queuesize(Queue* pq)
{assert(pq);int size = 0;QueueNode* cur = pq->head;while (cur != pq->tail->next){size++;cur = cur->next;}return size;
}bool Queueempty(Queue* pq)
{assert(pq);return pq->head == NULL;
}typedef struct 
{Queue q1;Queue q2;
} MyStack;MyStack* myStackCreate() {MyStack*obj=(MyStack*)malloc(sizeof(MyStack));if(obj==NULL)exit(-1);Queueinit(&obj->q1);Queueinit(&obj->q2);return obj;
}void myStackPush(MyStack* obj, int x) 
{if(!Queueempty(&obj->q1)){Queuepush(&obj->q1,x);}else{Queuepush(&obj->q2,x);}
}int myStackPop(MyStack* obj) {Queue*empty=&obj->q1;Queue*noempty=&obj->q2;if(!Queueempty(&obj->q1)){empty=&obj->q2;noempty=&obj->q1;}while(Queuesize(noempty)>1){Queuepush(empty,Queuefront(noempty));Queuepop(noempty);}int front=Queuefront(noempty);Queuepop(noempty);return front;
}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);
}

二.【Leetcode232】栈实现队列

1.链接

栈实现队列

2.题目再现

3.解法

这个的解法和上面的类似,只不过这个不用总是来回倒;

根据栈和队列的特征,我们会发现将一个栈中的数据倒入另一个栈时,数据的顺序刚好符合队列的要求,不需要再重复地倒数据,所以我们可以让一个栈专门用来入数据(Pushst)一个栈专门用来出数据(Popst)当我们要出数据而这个栈为空时,我们才将用来入数据的栈中的数据倒入用来出数据的栈 。

如图:

1.判空时,需要两个栈都为空,队列才为空;

2.返回队头数据时,和出数据的操作类似,只是不需要删除队头的数据,还有在之前要判断队列是否为空;

3.销毁队列前,要先销毁两个栈。

同样,因为是C语言,得先手搓个栈。

#define MR_CAP 5
typedef int STdatatype;typedef struct Stack
{STdatatype* arr;int top;int capacity;
}ST;void Stackinit(ST* ps)
{assert(ps);ps->arr = (STdatatype*)malloc(MR_CAP * sizeof(STdatatype));if (ps->arr == NULL){perror("Stackinit malloc");exit(-1);}ps->top = 0;ps->capacity = MR_CAP;
}void Stackdestroy(ST* ps)
{assert(ps);free(ps->arr);ps->arr = NULL;ps->top = 0;ps->capacity = 0;
}void Stackpush(ST* ps, STdatatype x)
{assert(ps);if (ps->top == ps->capacity){STdatatype* tmp = (STdatatype*)realloc(ps->arr, ps->capacity * 2 * sizeof(STdatatype));if (tmp == NULL){perror("Stackpush realloc");exit(-1);}else{ps->arr = tmp;ps->capacity *= 2;}}ps->arr[ps->top] = x;ps->top++;
}void Stackpop(ST* ps)
{assert(ps);assert(ps->top > 0);ps->top--;
}STdatatype Stacktop(ST* ps)
{assert(ps);return ps->arr[ps->top - 1];
}int Stacksize(ST* ps)
{assert(ps);return ps->top;}bool Stackempty(ST* ps)
{assert(ps);if (ps->top == 0){return true;}elsereturn false;}typedef struct {ST Pushst;ST Popst;
} MyQueue;MyQueue* myQueueCreate() {MyQueue*obj=(MyQueue*)malloc(sizeof(MyQueue));if(obj==NULL)exit(-1);Stackinit(&obj->Pushst);Stackinit(&obj->Popst);return obj;
}void myQueuePush(MyQueue* obj, int x) {Stackpush(&obj->Pushst,x);
}int myQueuePeek(MyQueue* obj) {if(Stackempty(&obj->Popst)){while(!Stackempty(&obj->Pushst)){Stackpush(&obj->Popst,Stacktop(&obj->Pushst));Stackpop(&obj->Pushst);}}return Stacktop(&obj->Popst);}int myQueuePop(MyQueue* obj) {int front=myQueuePeek(obj);Stackpop(&obj->Popst);return front;
}bool myQueueEmpty(MyQueue* obj) {return Stackempty(&obj->Pushst)&&Stackempty(&obj->Popst);
}void myQueueFree(MyQueue* obj) {Stackdestroy(&obj->Pushst);Stackdestroy(&obj->Popst);free(obj);
}

🐲👻这两道题的讲解就到这里了,若有错误或是建议欢迎小伙伴们指出。🐯🤖

🥰🤩希望小伙伴们可以多多支持博主哦。😍😃

😁😄谢谢你的阅读。😼😸

 

http://www.hkea.cn/news/436070/

相关文章:

  • 网络推广seo公司seo排名的方法
  • 南山做网站多少钱百度资讯
  • 西安哪里有做网站的小学生收集的新闻10条
  • 做游戏网站有几个要素seo网站关键词优化报价
  • 蓬业东莞网站建设技术支持东莞做网站公司首选
  • 网站版式设计获客渠道有哪些
  • 今日军事新闻简短扬州seo优化
  • 国外好看的教育类网站模板下载东莞做网站最好的是哪家
  • 微擎与wordpress快速优化seo软件推广方法
  • 英文网站设计哪家好免费网站搭建
  • 网站建设公司 销量深圳谷歌seo公司
  • 新蔡哪有做网站建设的全球疫情今天最新消息
  • 怎么做平台网站百度seo报价方法
  • 帮人做网站 怎么收费怎么用网络推广
  • 网站排名优化建设百度广告投放技巧
  • 文件服务器网站搭建教程好的竞价托管公司
  • 黑龙江省城乡和住房建设厅网站首页百度链接地址
  • 网站模板修改工具专业seo关键词优化
  • 口碑好的句容网站建设yahoo搜索
  • 深圳网站建设外贸公司价格网络营销的背景和意义
  • 长春网站建设硕成传媒seo快速排名优化公司
  • web网站开发能使用c 吗免费建立个人网站申请
  • 织梦网站修改教程视频网站优化培训学校
  • 南沙区交通和建设局网站中国十大网络销售公司
  • 免费建设网站的方法百度网址大全 官网
  • 手机网站设计制作公司微信推广费用一般多少
  • 建设网站需要什么注册域名费用一般多少钱
  • 女性门户网站源码百度指数功能有哪些
  • 怎么帮公司做网站建设谷歌搜索引擎免费入口 香港
  • 请写出网站建设前期需要做的准备外贸定制网站建设电话