复习数据结构No.1

简介: 复习数据结构No.1

引言:

伴随着考试的来临,此时需要我们复习一下以前的知识啦!

101.jpeg


数据结构中的顺序表复习

完整顺序表代码如下:

#include<assert.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
typedef int SLDateType;//这个位置把typedef和#define给混淆了,此时百度,然后得到,下次要搞清楚
//百度总结得出结论:1.typedef只适用于给数据类型进行取别名,而#define功能更加强大,可以给许多的数据取别名 2.一个不带分号,一个带分号 3.一个是C语言中的关键字,一个不是 4.一个在预处理是会直接展开,一个不会,只会进行编译是替换
typedef struct SequenceList
//此时漏了把数据类型给typedef(下次记住就行)
{
  SLDateType* _arr;
  int _size;
  int _capacity;
}SL;
void SequenceListInit(SL* ps)
{
  ps->_arr = NULL;
  ps->_capacity = 0, ps->_size = 0;
}
void SequenceListDestory(SL* ps)
{
  free(ps->_arr);
  ps->_arr = NULL;
}
void SequenceListCheckCapacity(SL* ps)
{
  if (ps->_size == ps->_capacity)
  {
    int newcapacity = ps->_capacity == 0 ? 4 : ps->_capacity * 2;//此时容量的问题是搞定了,但是实际的空间问题还没有搞定
    SLDateType* tmp = (SLDateType*)realloc(ps->_arr, sizeof(SLDateType) * newcapacity);
    if (realloc == nullptr)
    {
      cout << "realloc fail" << endl;
    }
    else
    {
      ps->_arr = tmp;
      ps->_capacity = newcapacity;//这个位置不敢写成 ps->_size = ps->_capacity //因为_size从头到尾都是不需要你自己动的,是要自己搞定++的,只要把_size和_capacity的关系处理好就行了(_capacity的本质还是为了把_size的大小问题搞定而已)
    }
  }
}
void SequenceListPushBack(SL* sl, SLDateType x)
{
  SequenceListCheckCapacity(sl);
  sl->_arr[sl->_size] = x;
  sl->_size++;//非常神奇的就把size++给忘了,说明没吃透(下次不允许忘)
}
void SequenceListPushFront(SL* sl, SLDateType x)
{
  SequenceListCheckCapacity(sl);
  int end = sl->_size;
  for (int i = 0; i < sl->_size; ++i)
  {
    sl->_arr[end - i] = sl->_arr[end - 1 - i];
  }
  sl->_arr[0] = x;
  sl->_size++;
}
void SequenceListPoptBack(SL* sl)
{
  assert(sl->_size > 0);
  sl->_size--;
}
void SequenceListPoptFront(SL* sl)//这个位置少了一个判断(就是没有数据的情况)
{
  if (sl->_size > 0)
  {
    int begin = 0;
    for (int i = 0; i < sl->_size - 1; ++i)
    {
      sl->_arr[begin + i] = sl->_arr[begin + 1 + i];
    }
    sl->_size--;
  }
}
SLDateType SequenceListFind(SL* sl, SLDateType x)
{
  for (int i = 0; i < sl->_size; ++i)
  {
    if (sl->_arr[i] = x)
    {
      return i;
    }
  }
}
void SequenceListInsert(SL* sl, int pos, SLDateType x)
{
  SequenceListCheckCapacity(sl);
  assert(pos < sl->_size ||  pos >= 0);
  int end = sl->_size;
  for (int i = 0; i < sl->_size - pos; ++i)
  {
    sl->_arr[end - i] = sl->_arr[end - 1 - i];
  }
  sl->_arr[pos] = x;//这个位置你非常神奇的没有把size++漏掉,但是把插入数据给漏掉了,人才
  sl->_size++;
}
void SequenceListErase(SL* sl, int pos)
{
  assert(pos < sl->_size ||  pos >= 0);//其中之一不满足就要报错,所以不可以使用&&要使用||(不敢当人才)
  for (int i = 0; i < sl->_size - pos; ++i)
  {
    sl->_arr[pos] = sl->_arr[pos + 1];
  }
  sl->_size--;
}
void SequenceListPrint(SL* ps)
{
  for (int i = 0; i < ps->_size; ++i)
  {
    cout << ps->_arr[i] << " ";
  }
  cout << endl;
}
int main()
{
  SL sl;
  SequenceListInit(&sl);
  //SequenceListPushBack(&sl, 1);
  //SequenceListPushBack(&sl, 2);
  //SequenceListPushBack(&sl, 3);
  //SequenceListPushBack(&sl, 4);
  //SequenceListPushBack(&sl, 5);
  SequenceListPushFront(&sl, 1);
  SequenceListPushFront(&sl, 2);
  SequenceListPushFront(&sl, 3);
  SequenceListPushFront(&sl, 4);
  SequenceListPushFront(&sl, 5);
  //SequenceListPoptBack(&sl);
  //SequenceListPoptFront(&sl);
  //SequenceListInsert(&sl, 1, 1);
  //SequenceListInsert(&sl, 2, 2);
  //SequenceListInsert(&sl, 3, 3);
  //SequenceListInsert(&sl, 4, 4);
  //SequenceListInsert(&sl, 5, 5);
  SequenceListErase(&sl, 1);
  SequenceListErase(&sl, 2);
  SequenceListErase(&sl, 3);
  SequenceListErase(&sl, 4);
  SequenceListErase(&sl, 5);
  cout << "wo shi gao shou" << endl;
  SequenceListPrint(&sl);
  SequenceListDestory(&sl);
  return 0;
}

以上有注释的位置,都是我在写的过程中,出现了一定的问题的地方,估计也是顺序表中比较容易出问题的地方,感兴趣的小伙伴可以看一看,当然你肯定是不会像我一样,犯这么傻的错误!

102.jpeg

总结:吃旧饭都吃不明白,但是吃的过程中,仿佛可以看见肉丝哦!


相关文章
|
11月前
|
Ubuntu Linux Android开发
Android Studio支持多种操作系统
Android Studio支持多种操作系统
503 1
|
机器学习/深度学习 缓存 PyTorch
PyTorch 2.0 推理速度测试:与 TensorRT 、ONNX Runtime 进行对比
PyTorch 2.0 于 2022 年 12 月上旬在 NeurIPS 2022 上发布,它新增的 torch.compile 组件引起了广泛关注,因为该组件声称比 PyTorch 的先前版本带来更大的计算速度提升。
1133 0
|
6月前
|
存储 负载均衡 测试技术
ACK Gateway with Inference Extension:优化多机分布式大模型推理服务实践
本文介绍了如何利用阿里云容器服务ACK推出的ACK Gateway with Inference Extension组件,在Kubernetes环境中为多机分布式部署的LLM推理服务提供智能路由和负载均衡能力。文章以部署和优化QwQ-32B模型为例,详细展示了从环境准备到性能测试的完整实践过程。
|
存储 监控 芯片
单片机的扩展结构
单片机的扩展结构
502 2
|
存储 Java 索引
Java的基本语法格式详解
Java的基本语法格式详解
318 0
|
人工智能 数据安全/隐私保护
Anthropic
Anthropic
195 1
ArcGIS:要素标注时报错:未找到要素,无法验证表达式
ArcGIS:要素标注时报错:未找到要素,无法验证表达式
604 0
|
SQL 存储 关系型数据库
MySQL-binlog+dump备份还原
MySQL-binlog+dump备份还原
347 0
|
数据可视化
拿到基因表达矩阵之后的那点事(三)
之前的流程我们已经通过三种常用的方法对样品之间做了差异分析,接下来我们就以最流行的DEseq2包分析的结果接着进行分析,可视化~
333 0
|
固态存储 内存技术
SSD主控与NAND闪存之多通道交互
目前主流的SSD会有4或者8个Channel,SSD主控有一个专门的模块管理不同Channel之间的读写操作。