【数据结构 —— 线性表方法总结】

1、链表

1.1、线性表

  • 创建线性表:

    • 线性表示意图(图片来源于网络):

      在这里插入图片描述

    • 创建线性表

      sqlink L;
      
      • sqlink 是结构体的指针
    • 申请堆空间

      L=(sqlist)malloc(sizeof(sqlist))
      
    • 判断创建是否成功

      if(L==0){
          printf("malloc erro");
          return -1;
      }
      
    • 初始化线性表

      memset(L,0,sizeof(sqlist));
      
    • 给last赋值

      last->-1;
      
    • 返回值

      return L;
      
  • 向线性表中插入数据

    • 示意图(图片来源于网络)

      在这里插入图片描述

    • 判断线性表是否为满(full)

       if (L->last == N-1)
         {
          printf("the sqlist is full,inster erro\n");
          return -1;
         }
      

      定义数组中N为128#define N 128

      • 验证链表中的last的位置是不是链表的最后一个位置(是则满不是则空)

      • L->last为最后一个

      • N-1为链表的最后一个角标

    • 检查需要插入的位置是否合理(是否超出范围)

       if (pos<0||pos>L->last+1)
         {
          printf("this pos erro");
          return-1;
         }
      
      • 即现在输入的位置pos是否小于0会长是否大于链表的最大长度

      • L->last+1链表的角标从0开始算,因此此处的+1表示位置从1开始算

      • pos表示的是插入的位置

    • 移动元素、更新last位置

      for ( i = L->last; i >=pos; i--)
         {
           L->data[i+1]=L->data[i];
         } 
         L->data[pos]=vale;
         L->last++;
          return 0;
      }
      
      • for循环的意思为:定位获取当前链表长度i的值,将pos之后的数字从后往前依次往后移动1个位置(每次)
      • for循环截至条件为:pos定位的位置
      • 通过移动次数更行last的位置
  • 清空线性表

    • 判断是否为空

      if(L==NULL){
          retun -1;
      }
      

      若表L为空则表示没有清空的意义

    • 清空操作

      memset(L,0,sizeof(sqlist));
      
    • 重新设置last的位置

      L ->last ==NULL;
      
    • 返回

      return 0;
      
  • 判断线性表是否为空

    • 通过last判断,因为last始终处于最后一个,因此只需要验证last的位置即可

      if (L->last==-1)
          {
             printf("this sqlist is NULL")
             return -1;
          }
      

      last的值还未发生改变,即last==-1时该表任然为空,反之不为空。

       else{
              printf("this sqlist not is NULL")
              return 0;
          }
      }
      
  • 求线性表的长度

    • 判断链表是否为空

      if (L==NULL)
          {
          	printf("L is NULL")
              return -1;
      	}
      
    • 不为空则根据last进行计算

    •  return (L->last+1);
      
  • 查询指定元素位置

    • 判断该线性表是否为空表

      if (L == NULL) {
          	printf("the sqlist is NILL")
              return -1;
          }
      
    • 通过遍历查找对应的值

       for (int i = 0; i <= L->last; i++) {
              if (L->data[i] == value) {
                  return i;
              }
          }
      
          return -1;
      }
      
      • 定位到最后一个值L->last并进行遍历和判断
  • 查看遍历线性表

    • 判断链表是否创建成功

      if (L==NULL)
          {
              return -1;
          }
      
    • 判断是否为空

      	if (L->last==-1)
          {
              printf("the sqlink is null");
          }
      
    • 遍历打印

       for (size_t i = 0; i < L->last; i++)
          {
              printf("%d",L->data[i]);
          }
          return 0;
      }
      
      • size_t.h文件中创建结构体时int的重命名
  • 释放空间

    • 判断是否为空

       if(L==NULL){
              printf("this is NULL");
              return -1;
          }
      
    • 释放空间

      	else{
              free(L);
              return 0;
          }
      

      ​ 直接使用free()即可

  • 删除指定位置的数据

    • 先判断是否创建成功

      if (L == NULL) {
              printf("this is error");
              return -1;
          }
      
    • 判断线性表是否为空

       if (L->last == -1) {
              printf("this is NULL");
              return -1;
          }
      
    • pos的位置是否合理

      if (pos > L->last || pos < 0) {
              printf("pos is error");
              return -1;
          }
      
    • 使用for循环移动

       for (int i = pos; i < L->last; i++) {
              L->data[i - 1] = L->data[i];
          }
      
    • 移动last的位置

      L->last--;
      
  • 数组合并

    • 遍历对比(可以使用上面的查询函数,也可以直接遍历)

      for (int i = 0; i < L->last; ++i) {
              for (int j = 0; j < S->last; ++j) 
      
    • 新建一个变量来接受验证是否相同

      ret = (L->data[i] == S->data[j]);
      
    • 进行插入

      if (ret != 1) {
          L->data[lcoal + 1] = S->data[j];
          L->last++;
         }
      

      这里验证ret,如果相同即返回0ret

    • 返回

       return L->last;
      
    • 完整代码

      int list_merge(sqlink L, sqlink S) {
          int lcoal = L->last;
          int ret;
          for (int i = 0; i < L->last; ++i) {
              for (int j = 0; j < S->last; ++j) {
                  ret = (L->data[i] == S->data[j]);
                  if (ret != 1) {
                      L->data[lcoal + 1] = S->data[j];
                      L->last++;
                  }
              }
          }
          return L->last;
      }
      
  • 删除相同元素

    • 判断线性表是否为空

      if(L->last==0){
              return 0;
          }
      
    • 遍历比较

      for (int i = 0; i < L->last; ++i) {
              for (int j = L->last; j >= 0; --j) {
                  ret=L->data[i]==L->data[j];
      
    • 删除重复元素

      if(ret ==1){
      	for (int k = j; k < L->last; ++k) {
      		L->data[k]=L->data[k+1];
      }
      
    • 移动last

      L->last--;
      

    完整代码
    sqlist.h

#ifndef UNTITLED_SQLIST_H
#define UNTITLED_SQLIST_H

typedef int data_t;
#define N 128

typedef struct sqlist_t {
    data_t data[N];
    int last;
} sqlist, *sqlink;

sqlink sqlist_create();

int list_clear(sqlink L);

int list_empty(sqlink L);

int list_length(sqlink L);

int list_locate(sqlink L, data_t value);

int list_insert(sqlink L, data_t value, int pos);

int list_show(sqlink L);

int list_free(sqlink L);

int list_delete(sqlink L, int pos);

int list_merge(sqlink L,sqlink S);

int list_Delete_Duplicates(sqlink L);

#endif //UNTITLED_SQLIST_H

sqlist.c

#include <stdio.h>
#include <stdlib.h>
#include "sqlist.h"
#include <string.h>

sqlink sqlist_create() {
    sqlink L;
    L = (sqlink) malloc(sizeof(sqlist));

    if (L == NULL) {
        printf("malloc ERROR\n");
        return L;
    }

    memset(L, 0, sizeof(sqlist));
    L->last = -1;

    return L;
}

int list_clear(sqlink L) {
    if (L == NULL) {
        return -1;
    }

    memset(L, 0, sizeof(sqlist));
    L->last = -1;

    return 0;
}

int list_empty(sqlink L) {
    if (L == NULL) {
        return -1;
    } else {
        return (L->last == -1);
    }
}

int list_length(sqlink L) {
    if (L == NULL) {
        return -1;
    } else {
        return (L->last + 1);
    }
}

int list_locate(sqlink L, data_t value) {
    if (L == NULL) {
        return -1;
    }

    for (int i = 0; i <= L->last; i++) {
        if (L->data[i] == value) {
            return i;
        }
    }

    return -1;
}

int list_insert(sqlink L, data_t value, int pos) {
    if (L == NULL) {
        return -1;
    }

    if (L->last == N - 1) {
        printf("The sqlist is full, insert error.\n");
        return -1;
    }

    if (pos < 0 || pos > L->last + 1) {
        printf("Invalid position.\n");
        return -1;
    }

    for (int i = L->last; i >= pos; i--) {
        L->data[i + 1] = L->data[i];
    }

    L->data[pos] = value;
    L->last++;

    return 0;
}

int list_show(sqlink L) {
    if (L == NULL) {
        return -1;
    }

    if (L->last == -1) {
        printf("The sqlink is empty.\n");
        return 0;
    }

    for (int i = 0; i <= L->last; i++) {
        printf("%d ", L->data[i]);
    }
    printf("\n");

    return 0;
}

int list_free(sqlink L) {
    if (L == NULL) {
        printf("this is NULL");
        return -1;
    } else {
        free(L);
        return 0;
    }
}

int list_delete(sqlink L, int pos) {
    if (L == NULL) {
        printf("this erro");
        return -1;
    }

    if (L == -1) {
        printf("this is NILL");
        return -1;
    }

    if (pos > L->last || pos < 0) {
        printf("pos is erro");
        return -1;
    }
    for (int i = pos; i < L->last; ++i) {
        L->data[i - 1] = L->data[i];
        L->last--;
    }
}

int list_merge(sqlink L, sqlink S) {
    int lcoal = L->last;
    int ret;
    for (int i = 0; i < L->last; ++i) {
        for (int j = 0; j < S->last; ++j) {
            ret = (L->data[i] == S->data[j]);
            if (ret != 1) {
                L->data[lcoal + 1] = S->data[j];
                L->last++;
            }
        }

    }
    return L->last;

}
int list_Delete_Duplicates(sqlink L){
    if(L->last==0){
        return 0;
    }
    int ret;
    for (int i = 0; i < L->last; ++i) {
        for (int j = L->last; j >= 0; --j) {
            ret=L->data[i]==L->data[j];
            if(ret ==1){
                for (int k = j; k < L->last; ++k) {
                    L->data[k]=L->data[k+1];
                }
                L->last--;

            }
        }

    }
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "sqlist.h"
#include "sqlist.c"
#include <string.h>

int main(int argc, const char *argv[]) {
    sqlink L;
    L = sqlist_create();
    if (L == NULL) {
        return -1;
    }
    list_insert(L, 100, 0);
    list_insert(L, 100, 1);
    list_insert(L, 110, 1);
    list_show(L);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值