• 假如面试官让你手撕顺序表,该如何快速准确的交出一份让面试官满意的答卷?


    目录

    1.概念

    2结构

    3.顺序表的接口

    4.与顺序表相关的面试题

    5.顺序表的优缺

    1.概念

    顺序表是一段物理上连续存储数据元素的线性结构,一般使用数组存储,在数组上完成数据的增删查改。

    2结构

    采用动态增长的方式实现顺序表

    1. typedef int SeqListDatatype;
    2. typedef struct SeqList
    3. {
    4. SeqListDatatype* a;
    5. int capacity;
    6. int size;
    7. }SeqList;

    capacity:标识容量

    size:标识数据个数

    3.顺序表的接口

    void SeqListInit(SeqList* psl);

    1. void SeqListInit(SeqList* psl)
    2. {
    3. assert(psl);
    4. psl->a = NULL;
    5. psl->capacity = psl->size = 0;
    6. }

    // 检查空间,如果满了,进行增容
    void CheckCapacity(SeqList* psl);

    1. void CheckCapacity(SeqList* psl)
    2. {
    3. assert(psl);
    4. int newCapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;
    5. SeqListDatatype* tmp = (SeqListDatatype*)realloc(psl->a, sizeof(SeqList) * newCapacity);
    6. if (tmp == NULL)
    7. {
    8. perror("realloc fail:");
    9. exit(-1);
    10. }
    11. psl->a = tmp;
    12. psl->capacity = newCapacity;
    13. }

    // 顺序表尾插
    void SeqListPushBack(SeqList* psl, SeqListDatatype x);

    1. void SeqListPushBack(SeqList* psl, SeqListDatatype x)
    2. {
    3. assert(psl);
    4. if (psl->capacity == psl->size)
    5. {
    6. CheckCapacity(psl);
    7. }
    8. psl->a[psl->size] = x;
    9. psl->size++;
    10. }

    // 顺序表尾删
    void SeqListPopBack(SeqList* psl);

    1. void SeqListPopBack(SeqList* psl)
    2. {
    3. assert(psl);
    4. assert(psl->size > 0);
    5. psl->size--;
    6. }

    // 顺序表头插
    void SeqListPushFront(SeqList* psl, SeqListDatatype x);

    1. void SeqListPushFront(SeqList* psl, SeqListDatatype x)
    2. {
    3. assert(psl);
    4. if (psl->capacity == psl->size)
    5. {
    6. CheckCapacity(psl);
    7. }
    8. int end = psl->size - 1;
    9. while (end >= 0)
    10. {
    11. psl->a[end + 1] = psl->a[end];
    12. end--;
    13. }
    14. psl->a[0] = x;
    15. psl->size++;
    16. }

    // 顺序表头删
    void SeqListPopFront(SeqList* psl);

    1. void SeqListPopFront(SeqList* psl)
    2. {
    3. assert(psl);
    4. int begin = 0;
    5. while (begin < psl->size - 1)
    6. {
    7. psl->a[begin] = psl->a[begin + 1];
    8. begin++;
    9. }
    10. psl->size--;
    11. }

    // 顺序表查找
    int SeqListFind(SeqList* psl, SeqListDatatype x);

    1. int SeqListFind(SeqList* psl, SeqListDatatype x)
    2. {
    3. assert(psl);
    4. for (int i = 0; i < psl->size; i++)
    5. {
    6. if (psl->a[i] == x)
    7. {
    8. return i;
    9. }
    10. }
    11. return -1;
    12. }

    // 顺序表在pos位置插入x
    void SeqListInsert(SeqList* psl, size_t pos, SeqListDatatype x);

    1. void SeqListInsert(SeqList* psl, size_t pos, SeqListDatatype x)
    2. {
    3. assert(psl);
    4. assert((int)pos <= psl->size);
    5. if (psl->capacity == psl->size)
    6. {
    7. CheckCapacity(psl);
    8. }
    9. int end = psl->size - 1;
    10. while (end >= (int)pos)
    11. {
    12. psl->a[end + 1] = psl->a[end];
    13. --end;
    14. }
    15. psl->a[pos] = x;
    16. psl->size++;
    17. }

    // 顺序表删除pos位置的值
    void SeqListErase(SeqList* psl, size_t pos);

    1. void SeqListErase(SeqList* psl, size_t pos)
    2. {
    3. assert(psl);
    4. int begin = pos;
    5. while (begin < psl->size - 1)
    6. {
    7. psl->a[begin] = psl->a[begin + 1];
    8. ++begin;
    9. }
    10. psl->size--;
    11. }

    // 顺序表销毁
    void SeqListDestory(SeqList* psl);

    1. void SeqListDestory(SeqList* psl)
    2. {
    3. assert(psl);
    4. free(psl->a);
    5. psl->a = NULL;
    6. psl->capacity = psl->size = 0;
    7. }

    // 顺序表打印
    void SeqListPrint(SeqList* psl);

    1. void SeqListPrint(SeqList* psl)
    2. {
    3. for (int i = 0; i < psl->size; i++)
    4. {
    5. printf("%d ", psl->a[i]);
    6. }
    7. printf("\n");
    8. }

     4.与顺序表相关的面试题

    1. 原地移除数组中所有的元素val,要求时间复杂度为O(N),空间复杂度为O(1)。oj链接

    1. int removeElement(int* nums, int numsSize, int val){
    2. int left1 = 0;
    3. int left2 = 0;
    4. while(left1 < numsSize)
    5. {
    6. if(nums[left1] == val)
    7. {
    8. left1++;
    9. }
    10. else
    11. {
    12. nums[left2++] = nums[left1++];
    13. }
    14. }
    15. return left2;
    16. }

    2. 删除排序数组中的重复项。oj链接

    1. int removeDuplicates(int* nums, int numsSize){
    2. int left1 = 0;
    3. int left2 = 0;
    4. while(left1 < numsSize)
    5. {
    6. if(nums[left1] == nums[left2])
    7. {
    8. left1++;
    9. }
    10. else
    11. {
    12. nums[++left2] = nums[left1++];
    13. }
    14. }
    15. return left2 + 1;
    16. }

    3. 合并两个有序数组。OJ链接

    1. void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
    2. int end1 = m - 1;
    3. int end2 = n - 1;
    4. int end = nums1Size - 1;
    5. while(end1 >= 0 && end2 >= 0)
    6. {
    7. if(nums1[end1] >= nums2[end2])
    8. nums1[end--] = nums1[end1--];
    9. else
    10. nums1[end--] = nums2[end2--];
    11. }
    12. while(end2 >= 0)
    13. {
    14. nums1[end--] = nums2[end2--];
    15. }
    16. }

    5.顺序表的优缺点

    顺序表的优点:

    1.尾插和尾删效率高

    2.可以通过下标进行随机访问

    .顺序表的缺点:

    1.头部和中部插入删除效率低

    2.扩容可能会造成空间大量浪费

  • 相关阅读:
    Django创建模型
    .net技术第一章
    机器人种类知多少
    代码随想录算法训练营第23期day59|503.下一个更大元素II、42. 接雨水
    【稳定性】稳定性建设之弹性设计
    全志A40i开发板硬件说明书——100%国产+工业级方案(上)
    萨特——治愈了迷茫的你吗
    基层管理者的思考方式
    【Excel】合并复杂单元格
    JAVA学习第九课:用Swing开发GUI程序
  • 原文地址:https://blog.csdn.net/qq_65307907/article/details/127833528