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

栈和队列的初始化,插入,删除,销毁。

目录

题外话

顺序表和链表优缺点以及特点

一.栈的特点

二. 栈的操作

2.1初始化 

2.2 栈的销毁

2.3 栈的插入

2.3 输出top

2.4 栈的删除

2.5 输出栈


题外话

顺序表和链表优缺点以及特点

特点:顺序表,逻辑地址=物理地址。可以任意访问,访问时间复杂度O(1).。实现分配空                         间,当空间不足时,要动态扩容。顺序表在销毁时可以直接free,但链表要一                         个个删 除。

           链表:不连续的空间靠指针指向下一个地址。不用实现分配空间。

优缺点:

            顺序表:适和访问,不适合插入删除,时间负责度为O(N)。链表适和插入删除操                           作。

一.栈的特点

        (1)先进后出

        (2)栈不能任意打印,栈只能访问栈顶

        (3)栈只能尾插头删

二. 栈的操作

2.1初始化 

         

void STInit(ST* pst) 
{assert(pst);pst->a = NULL;pst->top = -1;pst->capacity = 0;
}

2.2 栈的销毁

2.3 栈的插入

注意:🤖

如果你初始化为0,那么就是先插入在++;

如果你初始化为-1,那就是先++,在插入。

}
//插入
void STPush(ST* pst, STDataType x) 
{assert(pst);if (pst->top == pst->capacity-1){int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);if (tmp == NULL) {perror("realloc fail");return;}pst->a = tmp;pst->capacity = newcapacity;}pst->top++;pst->a[pst->top] = x;
}

2.3 输出top

注意:

由于栈的特性,只能先进先出,尾插头删,不能任意输出,所以我们只能输出头。

void STTop(ST* pst) 
{assert(pst);assert(pst->top >= -1);return pst->a[pst->top];
}

2.4 栈的删除

//删除
void STPop(ST* pst) 
{assert(pst);assert(pst->top>=-1);pst->top--;

2.5 输出栈

while (STEmpty(&st) != true) {printf("%d ", STTop(&st));STPop(&st);
}

 

 栈的完整代码

#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>typedef int STDataType;
typedef struct STack
{STDataType* a;	//数值的指针是下标int top;int capacity;
}ST;void STInit(ST* pst);
void STDestory(ST* pst);
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);
bool STEmpty(ST* pst);
int STSize(ST* pst);
STDataType STTop(ST* pst);#include"Stack.h"
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>void STInit(ST* pst) 
{assert(pst);pst->a = NULL;pst->top = -1;pst->capacity = 0;
}
void STDestory(ST* pst) 
{assert(pst);free(pst->a);pst->a = NULL;pst->top = -1;pst->capacity = 0;}
//插入
void STPush(ST* pst, STDataType x) 
{assert(pst);if (pst->top == pst->capacity-1){int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);if (tmp == NULL) {perror("realloc fail");return;}pst->a = tmp;pst->capacity = newcapacity;}pst->top++;pst->a[pst->top] = x;
}
//输出头结点
STDataType STTop(ST* pst) 
{assert(pst);assert(pst->top >= -1);return pst->a[pst->top];
}
//删除
void STPop(ST* pst) 
{assert(pst);assert(pst->top>=-1);pst->top--;
}
bool STEmpty(ST* pst) 
{assert(pst);if (pst->top == -1) {return true;}else {return false;}
}
int STSize(ST* pst) 
{assert(pst);return pst->top;
}#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"Stack.h"void Test1() {ST st;STInit(&st);STPush(&st, 1);STPush(&st, 2);STPush(&st, 3);STPush(&st, 4);printf("%d\n", STTop(&st));STPop(&st);printf("%d\n", STTop(&st));while (STEmpty(&st) != true) {printf("%d ", STTop(&st));STPop(&st);}}int main() 
{Test1();return 0;
}

 

http://www.lryc.cn/news/234457.html

相关文章:

  • 重温《Unix设计哲学》
  • AIGC创作系统ChatGPT源码,AI绘画源码,支持最新GPT-4-Turbo模型,支持DALL-E3文生图
  • Spring条件注解@Conditoinal+ Profile环境切换应用@Profile
  • Scrum框架中的Sprint
  • openfeign、nacos获取接口提供方真实IP
  • Linux系统编程学习 NO.9——git、gdb
  • 【联邦学习+区块链】TORR: A Lightweight Blockchain for Decentralized Federated Learning
  • 《网络协议》08. 概念补充
  • 利用NVIDIA DALI读取视频帧
  • TSINGSEE青犀AI智能分析+视频监控工业园区周界安全防范方案
  • 【算法每日一练]-图论(保姆级教程 篇5(LCA,最短路,分层图)) #LCA #最短路计数 #社交网络 #飞行路线 # 第二短路
  • 德迅云安全为您介绍关于抗D盾的一些事
  • leetcode算法之位运算
  • java常用的几个图片处理工具对Tiff文件的支持
  • SQL必知会(二)-SQL查询篇(11)-联结表
  • 多模态大一统:开启全模态LLM和通用AI时代的大门
  • Alibaba Nacos注册中心实战
  • 京东数据采集与挖掘(京东大数据):2023年10月京东冰箱品牌销售排行榜
  • 某事业单位转型二类后绩效项目成功案例纪实
  • MySQL 和 SQL Server之间的数据迁移方法
  • 单元测试实战(五)普通类的测试
  • js 迭代器iterator 和 生成器Generator 10
  • 100套Axure RP大数据可视化大屏模板及通用组件库
  • 【OpenGauss源码学习 —— 执行算子(Append算子)】
  • Java(一)(引用类型的参数在传递,方法重载,面向对象编程基础)
  • Vue第1天:特性概览
  • C++语法基础知识面经汇总
  • AM@幂级数性质@幂级数和函数求解
  • PHP低版本安全问题
  • 结构体——C语言初阶