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

想学会网站建设要会什么越秀网站建设优化

想学会网站建设要会什么,越秀网站建设优化,做网站友情链接的步骤,中小企业网站建设问题文章目录前言一、栈1.1 栈的概念结构1.2栈的实现二、队列2.1队列的概念及结构2.2队列的实现三、栈和队列面试题总结前言 一、栈 1.1 栈的概念结构 栈也是一种线性表,数据在逻辑上挨着存储。只允许在固定的一端进行插入和删除元素。进行插入和删除操作的一端叫栈顶…

文章目录

  • 前言
  • 一、栈
    • 1.1 栈的概念结构
    • 1.2栈的实现
  • 二、队列
    • 2.1队列的概念及结构
    • 2.2队列的实现
  • 三、栈和队列面试题
  • 总结


前言

一、栈

1.1 栈的概念结构

栈也是一种线性表,数据在逻辑上挨着存储。只允许在固定的一端进行插入和删除元素。进行插入和删除操作的一端叫栈顶,另一端叫栈底。符合LIFO 先进后出。
在这里插入图片描述

压栈:插入操作。
出栈:删除操作。

1.2栈的实现

栈的实现用数组实现更好,因为完美符合数组的尾插尾删。
数组的缓存利用率高一点。
在这里插入图片描述
小练习:
在这里插入图片描述

支持动态增长的栈:

typedef int STDataType;
typedef struct Stack
{STDataType* _a;int _top; // 栈顶int _capacity; // 容量
}Stack;

初始化栈:

void StackInit(ST* ps)
{assert(ps);ps->a = NULL;ps->top = ps->capacity = 0;
}

入栈:进栈(栈的插入操作),若栈未满,则将x加入使之成为新栈顶。

void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->top == ps->capacity){int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));if (tmp == NULL){perror("realloc fail");exit(-1);}ps->a = tmp;ps->capacity = newCapacity;}ps->a[ps->top] = x;ps->top++;
}

出栈:出栈(栈的删除操作),若栈非空,则弹出栈顶元素,并用x返回。

void StackPop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));--ps->top;
}

获取栈顶元素:

STDataType StackTop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));return ps->a[ps->top - 1];
}

获取栈中有效元素个数:

int StackSize(ST* ps)
{assert(ps);return ps->top;
}

销毁栈:栈销毁,并释放占用的存储空间

void StackDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->capacity = ps->top = 0;
}

检测栈是否为空,如果为空返回非零结果,如果不为空返回0:

bool StackEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}
Stack.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STDataType;
typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;void StackInit(ST* ps);
void StackDestroy(ST* ps);
void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);
STDataType StackTop(ST* ps);
bool StackEmpty(ST* ps);
int StackSize(ST* ps);
Stack.c
#include "Stack.h"void StackInit(ST* ps)
{assert(ps);ps->a = NULL;ps->top = ps->capacity = 0;
}void StackDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->capacity = ps->top = 0;
}void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->top == ps->capacity){int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));if (tmp == NULL){perror("realloc fail");exit(-1);}ps->a = tmp;ps->capacity = newCapacity;}ps->a[ps->top] = x;ps->top++;
}void StackPop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));--ps->top;
}STDataType StackTop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));return ps->a[ps->top - 1];
}bool StackEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}int StackSize(ST* ps)
{assert(ps);return ps->top;
}

二、队列

2.1队列的概念及结构

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

2.2队列的实现

队列用链表的结构实现更优

在这里插入图片描述

链式结构:表示队列

typedef struct QListNode
{ struct QListNode* _pNext; QDataType _data; 
}QNode;

队列的结构:

typedef struct Queue
{QNode* head;QNode* tail;int size;
}Queue;

初始化队列:

void QueueInit(Queue* pq)
{assert(pq);pq->head = pq->tail = NULL;pq->size = 0;
}

队尾入队列:

void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");exit(-1);}else{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 QueuePop(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));if (pq->head->next == NULL){free(pq->head);pq->head = pq->tail = NULL;}else{QNode* del = pq->head;pq->head = pq->head->next;free(del);del = NULL;}pq->size--;
}

获取队列头部元素:

QDataType QueueFront(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->head->data;
}

获取队列队尾元素:

QDataType QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->tail->data;
}

获取队列中有效元素个数:

int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}

检测队列是否为空,如果为空返回非零结果,如果非空返回0:

bool QueueEmpty(Queue* pq)
{assert(pq);return pq->head == NULL && pq->tail == NULL;
}

销毁队列:

void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->head;while (cur){QNode* del = cur;cur = cur->next;free(del);}pq->head = pq->tail = NULL;
}
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 data;
}QNode;typedef struct Queue
{QNode* head;QNode* tail;int size;
}Queue;void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
bool QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);
Queue.c
#include"Queue.h"void QueueInit(Queue* pq)
{assert(pq);pq->head = pq->tail = NULL;pq->size = 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->head;while (cur){QNode* del = cur;cur = cur->next;free(del);}pq->head = pq->tail = NULL;
}void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");exit(-1);}else{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 QueuePop(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));if (pq->head->next == NULL){free(pq->head);pq->head = pq->tail = NULL;}else{QNode* del = pq->head;pq->head = pq->head->next;free(del);del = NULL;}pq->size--;
}QDataType QueueFront(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->head->data;
}QDataType QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->tail->data;
}bool QueueEmpty(Queue* pq)
{assert(pq);return pq->head == NULL && pq->tail == NULL;
}int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}

三、栈和队列面试题

题目链接:
1.https://leetcode.cn/problems/valid-parentheses/
在这里插入图片描述

在这里插入图片描述

题目链接:
2.https://leetcode.cn/problems/implement-stack-using-queues/

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

总结

http://www.15wanjia.com/news/157957.html

相关文章:

  • 深圳我的网站wordpress主导航菜单
  • 顺德高端网站建设网站建设的目标用户是
  • 怎么做网站跳转澄迈住宅与建设局网站
  • 视频网站设计模板如何规划企业网站
  • 珠海市企业网站制作平台对网站建设的评价语
  • 如何做好公司网站建设兰州企业网站优化
  • wordpress网站搬家vps网络营销网站推广方法
  • 云南餐饮网站建设如何设置网站服务器访问权限
  • 珠海做企业网站广告公司网站首页
  • 网站设计制作售价多少钱西安网站制作开发公司
  • 手机网站被禁止访问怎么设置打开如何在互联网上做推广
  • 郑州网站建设网站制作微信小程序电商平台开发
  • 如何建网站老鱼网0基础学剪辑视频
  • 网站开发前端与后端建设网站源码
  • 网站建设什么意思网站搭建工作室加盟
  • pc端网站建设哪里有大连装修网站推广
  • 做网站用什么电脑好汽车最好网站建设
  • 文化厅加强网站建设合肥公司建设网站
  • 番禺网站建设公司排名高端品牌鞋子有哪些
  • iis默认网站怎么设置产品设计专业介绍
  • 做网站找哪家好 07月网站建设定制公司
  • 专门做情侣装的网站福田蒙派克质量怎么样
  • 做自己的网站收费吗谷歌优化招聘
  • 怎么样自己建设网站三艺网站建设
  • 闸北做网站公司怎样在手机上面建设网站
  • 加热器网站怎么做的wordpress后台进
  • 互联网是什么佛山网站优化质量好
  • 淘宝可做的团购网站旅游网站建设方案
  • 郑州高端网站公司国际最新时事新闻热点
  • 商城网站系广州番禺桥南做网站