复习数据结构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

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


相关文章
|
存储 算法 Java
【数据结构】蓝桥杯常见习题(二)
【数据结构】蓝桥杯常见习题(二)
10957 0
|
存储 人工智能 算法
数据结构第2章课后习题答案(下)
数据结构第2章课后习题答案(下)
42 0
|
存储 人工智能 算法
数据结构第1章课后习题答案
数据结构第1章课后习题答案
76 0
|
算法 5G
数据结构第6章课后习题答案(上)
数据结构第6章课后习题答案
109 0
|
存储 机器学习/深度学习 人工智能
数据结构第4章课后习题答案
数据结构第4章课后习题答案
110 0
|
存储 算法
数据结构第5章课后习题答案(下)
数据结构第5章课后习题答案(下)
62 0
|
存储 算法 数据建模
数据结构第6章课后习题答案(下)
数据结构第6章课后习题答案(下)
96 0
|
存储 人工智能 算法
数据结构第3章课后习题答案
数据结构第3章课后习题答案
50 0
|
存储 人工智能 算法
数据结构第3章课后习题答案(下)
数据结构第3章课后习题答案(下)
67 0
|
存储 机器学习/深度学习 NoSQL
数据结构第5章课后习题答案(上)
数据结构第5章课后习题答案
232 0