重生之“我打数据结构,真的假的?”--3.栈和队列

news2024/9/17 3:56:12

1.栈和队列的基本概念

1.1 栈

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。

1.2 初始化及功能实现

#include"stack.h"

typedef int datatype;
typedef struct stack
{
	datatype* a;
	int top;
	int capacity;
}ST;

void InitST(ST* sl)
{
	assert(sl);                  //sl不为NULL
	sl->a = NULL;
	sl->top = 0;
	sl->capacity = 0;
}//初始化

void STpush(ST* sl, int data)
{
	assert(sl);
	if (sl->top == sl->capacity)
	{
		int newcapacity = sl->capacity == 0 ? 2 : sl->capacity * 2;
		datatype* tmp = (datatype*)realloc(sl->a, newcapacity * sizeof(datatype));
		sl->a = tmp;
		sl->capacity = newcapacity;
	}
	sl->a[sl->top] = data;
	sl->top++;
}//插入
void STpop(ST* sl)
{
	assert(sl);                //!!!!!!判断指针是否为空,《《《《判断的是栈是否存在》》》》》》          
	assert(!STempty(sl));      //!!!!!!!!!与上文不同,《《《《判断的是栈是否为空》》》》》》
		sl->top--;
}//删除

datatype STtop(ST* sl)
{
	assert(sl);
	assert(!STempty(sl));
	return sl->a[sl->top-1];
}//取栈顶

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

bool STempty(ST* sl)
{
	assert(sl);
	return sl->top == 0;
}

1.3 括号匹配问题

. - 力扣(LeetCode)

思路:

1.设立一个栈,依次从s中取出元素,如果为左括号类型 则入栈

2.否则取栈顶元素(如果栈为空则直接返回false)如果匹配

则删除栈顶元素,如果不匹配,返回false

3.s访问完后,如果栈为空,则返回true,否则返回false

bool isValid(char* s) {

typedef struct stack
{
	char * a;
	int top;
	int capacity;
}ST;

bool STempty(ST* sl)
{
	assert(sl);
	return sl->top == 0;
}
void InitST(ST* sl)
{
	assert(sl);                  //sl不为NULL
	sl->a = NULL;
	sl->top = 0;
	sl->capacity = 0;
}

void STpush(ST* sl, char data)
{
	assert(sl);
	if (sl->top == sl->capacity)
	{
		int newcapacity = sl->capacity == 0 ? 2 : sl->capacity * 2;
		char* tmp = (char*)realloc(sl->a, newcapacity * sizeof(char));
		sl->a = tmp;
		sl->capacity = newcapacity;
	}
	sl->a[sl->top] = data;
	sl->top++;
}
void STpop(ST* sl)
{
	assert(sl);                //!!!!!!判断指针是否为空,《《《《判断的是栈是否存在》》》》》》          
	assert(!STempty(sl));      //!!!!!!!!!与上文不同,《《《《判断的是栈是否为空》》》》》》
		sl->top--;
}

char STtop(ST* sl)
{
	assert(sl);
	assert(!STempty(sl));
	return sl->a[sl->top-1];
}

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

    int i=0;
    char j;
    char k;
    ST L;
    ST R;
    InitST(&L);
    InitST(&R);
    while(s[i]!='\0')
    {
     if(s[i]=='('||s[i]=='['||s[i]=='{')
    {
        STpush(&L, s[i]);
        i++;
    }
     else
     {
       if(STempty(&L))
       return false;
      if((STtop(&L)=='('&&s[i]==')')||(STtop(&L)=='{'&&s[i]=='}')||(STtop(&L)=='['&&s[i]==']'))
       {
        STpop(&L);
        i++;
       }
      else
      return false;
     }
    }
   if(STempty(&L))
   return true;
   else
   return false;

}

2.队列 

2.1 概念

队列的顺序实现是指分配一块连续的存储单元存放队列中的元素,并附设两个指针:队头指针 front 指向队头元素,队尾指针 rear 指向队尾元素

2.2 初始化及功能实现

typedef int datatype;
typedef struct Queuenode
{
	datatype data;
	struct Queuenode * next;
}Qnode;

typedef struct QT
{
	Qnode* head;
	Qnode* tail;
	int size;
}QT;


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

bool Queueempty(QT* sl)
{
	assert(sl);
	return sl->head ==NULL;
}

void Queuepush(QT* sl, int x)
{
	assert(sl);
	Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
	newnode->next = NULL;
	newnode->data = x;
	if (Queueempty(sl))
	{
		sl->head = newnode;
		sl->tail = newnode;
	}
	else
	{
		sl->tail->next = newnode;
		sl->tail = newnode;
	}
	sl->size++;
}

void Queuepop(QT* sl)
{
	assert(sl);
	assert(!Queueempty(sl));
	Qnode* next = sl->head->next;
	free(sl->head);
	sl->head = next;
	sl->size--;
}

int Queuetop(QT* sl)
{
	assert(sl);
	assert(!Queueempty(sl));
	return sl->head->data;
}


void Queuedestroy(QT* sl)
{
	assert(sl);
	while (!Queueempty(sl))
		Queuepop(sl);
	sl->head = sl->tail = NULL;
	sl->size = 0;
}

2.3 循环队列 

. - 力扣(LeetCode)

typedef struct Queuenode
{
	int data;
	struct Queuenode * next;
}Qnode;

typedef struct {
Qnode* head;
Qnode* tail;
int size;
int flag;
} MyCircularQueue;

MyCircularQueue* myCircularQueueCreate(int k) {
     MyCircularQueue* sl=NULL;
     sl=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
     sl->head = NULL;
     sl->tail = NULL;
     sl->size = 0;
     sl->flag = k;
     return sl;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->head ==NULL;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if(obj->size>=obj->flag)
    return false;
    Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
    newnode->next = NULL;
    newnode->data = value;
    if(myCircularQueueIsEmpty(obj))
    {
	obj->head = newnode;
	obj->tail = newnode;
    obj->tail->next=obj->head;
    obj->head->next=obj->tail;
    }
   else
   { 
	obj->tail->next = newnode;
	obj->tail = newnode;
    if(obj->head->next==obj->head)
    obj->head->next=obj->tail;
    obj->tail->next=obj->head;
    }
obj->size++;
return true;

}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    
  if(myCircularQueueIsEmpty(obj))
  return false;
  else{
   if(obj->head!=obj->tail)
   {
     Qnode* next = obj->head->next;
     obj->tail->next=next;
    free(obj->head);
    obj->head = next;
    obj->size--;
   }
   else{
    free(obj->head);
     obj->head = obj->tail = NULL;
     obj->size = 0;
   }
  }
  return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    int i=0;
    if(myCircularQueueIsEmpty(obj))
    return -1;
    else
    return obj->head->data;
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
    return -1;
    else
    return obj->tail->data;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return obj->size==obj->flag;
}

void myCircularQueueFree(MyCircularQueue* obj) {
   
    while (!myCircularQueueIsEmpty(obj))
	{
     Qnode* next = obj->head->next;
     obj->tail->next=next;
     if(obj->head!=obj->tail)
     { free(obj->head);
      obj->head = next;
      obj->size--;
     }
     else{
        obj->head = obj->tail = NULL;
        obj->size = 0;
        break;
     }
    }
    
}

3.用栈实现队列 

 . - 力扣(LeetCode)

 思路:

1.可以设立两个栈 p,q

(1)入队:将p中元素依次入栈q,再将函数传递的数据入栈q

(2)出队:将q中元素依次入栈p,再取q栈顶元素

typedef struct stack
{
	int * a;
	int top;
	int capacity;
}ST;

typedef struct {
ST* p;
ST* q;
} MyQueue;


MyQueue* myQueueCreate() 
{
    MyQueue*sl=NULL;
    sl=(MyQueue*)malloc(sizeof(MyQueue));
    sl->q=(ST*)malloc(sizeof(ST));
    sl->p=(ST*)malloc(sizeof(ST));
    sl->q->a=NULL;
    sl->q->top=0;
    sl->q->capacity=0;
    sl->p->a=NULL;
    sl->p->top=0;
    sl->p->capacity=0;
    return sl;
}

bool STempty(ST* sl)
{
	assert(sl);
	return sl->top == 0;
}

int STtop(ST* sl)
{
	assert(sl);
	assert(!STempty(sl));
    int i;
	i=sl->a[sl->top-1];
    return i;
}


void myQueuePush(MyQueue* obj, int x) {
    while(!STempty(obj->q))
    {
if (obj->p->top == obj->q->capacity)
{
	int newcapacity = obj->q->capacity == 0 ? 2 : obj->p->capacity * 2;
	int * tmp = (int*)realloc(obj->p->a, newcapacity * sizeof(int));
	obj->p->a = tmp;
	obj->p->capacity = newcapacity;
}
obj->p->a[obj->p->top] =STtop(obj->q) ;
obj->p->top++;
obj->q->top--;


    }
    if (obj->p->top == obj->p->capacity)
{
	int newcapacity = obj->p->capacity == 0 ? 2 : obj->p->capacity * 2;
	int * tmp = (int*)realloc(obj->p->a, newcapacity * sizeof(int));
	obj->p->a = tmp;
	obj->p->capacity = newcapacity;
}
obj->p->a[obj->p->top] = x;
obj->p->top++;
}

int myQueuePop(MyQueue* obj) {
    while(!STempty(obj->p))
    {
        if (obj->q->top == obj->q->capacity)
{
	int newcapacity = obj->q->capacity == 0 ? 2 : obj->q->capacity * 2;
	int * tmp = (int*)realloc(obj->q->a, newcapacity * sizeof(int));
	obj->q->a = tmp;
	obj->q->capacity = newcapacity;
}
obj->q->a[obj->q->top] =STtop(obj->p) ;
obj->q->top++;
obj->p->top--;
    }
    int i=STtop(obj->q);
    obj->q->top--;
    return i;
}

int myQueuePeek(MyQueue* obj) {
    while(!STempty(obj->p))
    {

   if (obj->q->top == obj->q->capacity)
{
	int newcapacity = obj->q->capacity == 0 ? 2 : obj->q->capacity * 2;
	int * tmp = (int*)realloc(obj->q->a, newcapacity * sizeof(int));
	obj->q->a = tmp;
	obj->q->capacity = newcapacity;
}
obj->q->a[obj->q->top] =STtop(obj->p) ;
obj->q->top++;
obj->p->top--;
    }
     int i=STtop(obj->q);
    return i;
}

bool myQueueEmpty(MyQueue* obj) {
    if(STempty(obj->p)&&STempty(obj->q))
    return true;
    else
    return false;
}

void myQueueFree(MyQueue* obj) {
    free(obj->p);
    free(obj->q);
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1952531.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

鸿蒙开发——axios封装请求、拦截器

描述:接口用的是PHP,框架TP5 源码地址 链接:https://pan.quark.cn/s/a610610ca406 提取码:rbYX 请求登录 HttpUtil HttpApi 使用方法

开源模型应用落地-LangChain实用小技巧-ChatPromptTemplate的partial方法(一)

一、前言 在当今的自然语言处理领域,LangChain 框架因其强大的功能和灵活性而备受关注。掌握一些实用的小技巧,能够让您在使用 LangChain 框架时更加得心应手,从而更高效地开发出优质的自然语言处理应用。 二、术语 2.1.LangChain 是一个全方…

TCP/IP协议(全的一b)应用层,数据链层,传输层,网络层,以及面试题

目录 TCP/IP协议介绍 协议是什么,有什么作用? 网络协议为什么要分层 TCP/IP五层网络协议每层的作用 应⽤层 DNS的作用及原理 DNS工作流程 数据链路层 以太⽹帧格式 MAC地址的作用 ARP协议的作⽤ ARP协议的工作流程 MTU以及MTU对 IP / UD / TCP 协议的影响 传输层…

MySQL(持续更新中)

第01章_数据库概述 1. 数据库与数据库管理系统 1.1 数据库相关概念 DB:数据库(Database)即存储数据的“仓库”,其本质是一个文件系统。它保存了一系列有组织的数据DBMS:数据库管理系统(Database Manageme…

2024年【广东省安全员B证第四批(项目负责人)】考试报名及广东省安全员B证第四批(项目负责人)模拟考试

题库来源:安全生产模拟考试一点通公众号小程序 广东省安全员B证第四批(项目负责人)考试报名根据新广东省安全员B证第四批(项目负责人)考试大纲要求,安全生产模拟考试一点通将广东省安全员B证第四批&#x…

AFast and Accurate Dependency Parser using Neural Networks论文笔记

基本信息 作者D Chendoi发表时间2014期刊EMNLP网址https://emnlp2014.org/papers/pdf/EMNLP2014082.pdf 研究背景 1. What’s known 既往研究已证实 传统的dp方法依存句法分析特征向量稀疏,特征向量泛化能力差,特征计算消耗大,并且是人工构…

UE5 with plugins AirSim in Windows ROS in WSL2-Ubuntu 20.04配置过程记录

一、概述 因为需要使用到Windows系统下的UE5和插件AirSIm进行研究,所以在Windows环境下进行配置。但又因为需要使用到ros进行操作,所以,在通过对诸多资源进行考察过后,因为UE5plugins AirSim已经配置成功。只需要考虑跟ROS的通信以…

构建查询洞察 UI

本文字数:2631;估计阅读时间:7 分钟 作者:Bucky Schwarz 本文在公众号【ClickHouseInc】首发 我们最近发布了 Query Insights 的初步实现,为 ClickHouse Cloud 用户提供了一种便捷的方法来查看和解释查询日志。该功能对…

CSS 两种盒模型 box-sizing content-box 和 border-box

文章目录 Intro谨记box-sizing 两个不同赋值的效果区别?宽高的数值计算标准盒模型 box-sizing: content-box; box-sizing 属性的全局设置 Intro 先问一句:box-sizing 和它的两个属性值是做什么用的?以前我并不知道它的存在,也做…

GeneCompass:跨物种大模型用于破解基因调控机理

GeneCompass是第一个基于知识的跨物种基础模型,该模型预先训练了来自人类和小鼠的超过1.2亿个单细胞转录组。在预训练过程中,GeneCompass有效整合了四种生物先验知识,以自监督的方式增强了对基因调控机制的理解。对多个下游任务进行微调&…

SSM酒店信息管理系统-计算机毕业设计源码41731

摘要 酒店信息管理系统是一种基于计算机技术的管理工具,旨在提高酒店业务效率和服务质量。该系统通过集成多个功能模块,实现酒店各项业务的自动化管理,包括客房信息管理、预订信息管理、入住信息管理、退房信息管理、续费信息管理等。该系统可…

免费使用正版的Typora教程

1.来到Typora官网下载安装。 Typora官网: https://typoraio.cn/ 2.激活主程序 编辑修改Typora安装目录下文件 下面展示文件目录路径 : D:\SoftWare\Typora1.9.5\resources\page-dist\static\js\LicenseIndex.180dd4c7.4da8909c.chunk.js查找:e.hasAc…

打通“链上数据脉络” 欧科云链数字生态建设成果凸显

7月25日,据Coindesk报道,全球领先的区块链技术和服务提供商欧科云链宣布旗下OKLink浏览器与Polygon Labs正式达成合作,成为AggLayer首个区块链搜索引擎及Web3数据分析平台,将为开发者提供精简易用的链上数据访问和开发工具&#x…

Python 中的正反斜杠用法详解

在Python编程中,字符串是一个常用的数据类型,字符串中的斜杠(反斜杠\和正斜杠/)具有特殊的用法和意义,本文将介绍这两种斜杠的用法。 一、反斜杠的转义作用 在Python中,反斜杠(\)…

2024年必读高质量计算机编程书籍

点击上方关注 “终端研发部” 设为“星标”,和你一起掌握更多数据库知识 1、推荐书籍:《Python Cookbook》 理由:这本书是Python学习者公认的经典教程,由资深Python专家David Beazley编写。它不仅仅是一本Python语言的参考手册&am…

【React 】开发环境搭建详细指南

文章目录 一、准备工作1. 安装 Node.js 和 npm2. 选择代码编辑器 二、创建 React 项目1. 使用 Create React App2. 手动配置 React 项目 三、集成开发工具1. ESLint 和 Prettier2. 使用 Git 进行版本控制 在现代前端开发中,React 是一个非常流行的框架,用…

科学又省力 宠物浮毛怎么去掉便捷高效?除毛秘籍养宠空气净化器

上次和朋友逛完街去她家,她家的猫哈基米一开门就飞奔过来,朋友直接抱起它狂亲。结果,猫毛和汗水粘得到处都是,手臂上、脸上都是,看得我这鼻炎星人直起鸡皮疙瘩。很多养宠物的朋友都说,天天给猫狗梳毛&#…

C++-----多态

一.对多态的解释 场景:买车票时,学生是半价,军人要优先......,对于不同的人群,在同一个售票窗口会受到不同折扣,这就是多态的体现。 上图就是多态的效果。 为什么Ticket的参数是person类型,但却能接受不同…

嵌入式初学-C语言-五

C语言语句概述 C语句的分类 ⑴ 控制语句 用于完成一定的控制功能 ① if ( ) …… else …… ② for ( ) …… ③ while ( ) …… ④ do …… while ( ) ⑤ continue ⑥ break ⑦ switch ( ) ⑧ return ⑨ goto 标号 (无条件跳转语句) 说明:“( ) ”…

Flutter Dio网络请求报错FormatException: Unexpected character

最近开发Flutter项目,网络请求采用的是Dio框架,在发起网络请求的时候报错: 网络请求返回的数据为: var returnCitySN {\"cip\": \"127.0.0.1\", \"cid\": \"00\", \"cname\"…