C数据结构-线性表之顺序表

打印 上一主题 下一主题

主题 896|帖子 896|积分 2698

什么是线性表




线性表的插入元素



线性表的删除元素



线性表顺序存储的缺点


线性表的特点


1.线性表的实例

首先我们创建3个文件,分别如下:
liner_data
--sqlist.c
--sqlist.h
--test.c
  1. sqlist.h
  2. // .h文件中定位数据的结构以及函数的方法
  3. typedef int data_t;
  4. #define N 128  //定义一个宏
  5. typedef struct {
  6.     data_t data[N];
  7.     int last;
  8. } sqlist, *sqlink;
  9. sqlink list_create();
  10. int list_clear(sqlink L);
  11. int list_delete(sqlink L);
  12. int list_empty(sqlink L);
  13. int list_length(sqlink L);
  14. int list_locate(sqlink L, data_t value);
  15. int list_insert(sqlink L, data_t value, int pos);
  16. int list_show(sqlink L);
复制代码
下面编写sqlist.c文件:函数实现的功能
  1. //
  2. // Created by Lenovo on 2023/9/9.
  3. //
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "sqlist.h"
  8. sqlink list_create(){
  9.     // malloc
  10.     sqlink L;
  11.     L = (sqlink)malloc(sizeof(sqlist));
  12.     if(L == NULL){
  13.         printf("list malloc failed\n");
  14.         return L;
  15.     }
  16.     // initialize
  17.     memset(L, 0, sizeof(sqlist)); // 向数组中除了最后一个,其他全部初始化为0
  18.     L->last = -1;
  19.     return L;
  20. }
  21. int list_clear(sqlink L){
  22.     /*
  23.      * @return: 0-success   -1-failed
  24.      */
  25.     if(L == NULL){
  26.         return -1;
  27.     }
  28.     memset(L, 0, sizeof(sqlist));
  29.     L->last = -1;
  30.     return 0;
  31. }
  32. int list_delete(sqlink L){
  33.     if(L==NULL)
  34.         return -1;
  35.     free(L);  // 删除堆内存
  36.     L=NULL;
  37.     return 0;
  38. }
  39. /*
  40. * list_empty: Is list empty?
  41. * para L: list
  42. * @return: 1-empty   0-not empty
  43. */
  44. int list_empty(sqlink L){
  45.     if(L->last == -1)
  46.         return 1;
  47.     else
  48.         return 0;
  49. }
  50. int list_length(sqlink L){
  51.     if(L==NULL)
  52.         return -1;
  53.     return (L->last+1);
  54. }
  55. int list_locate(sqlink L, data_t value){
  56.     return 0;
  57. }
  58. int list_insert(sqlink L, data_t value, int pos){
  59.     int i;
  60.     // 判断是否满了full?
  61.     if(L->last == N-1){
  62.         printf("list is full\n");
  63.         return -1;
  64.     }
  65.     // check para    0<=pos<=last+1     [0, last+1]
  66.     if(pos<0 || pos>L->last+1){
  67.         printf("Pos is invalid\n");
  68.         return -1;
  69.     }
  70.     //move
  71.     for (i=L->last; i>=pos; i--){
  72.         L->data[i+1] = L->data[i];
  73.     }
  74.     // update value last
  75.     L->data[pos] = value;
  76.     L->last++;
  77.     return 0;
  78. }
  79. int list_show(sqlink L){
  80.     int i;
  81.     if (L==NULL)
  82.         return -1;
  83.     if(L->last == -1)
  84.         printf("list is empty\n");
  85.     for(i=0; i<=L->last; i++){
  86.         printf("%d ", L->data[i]);
  87.     }
  88.     puts(""); // 自动换行
  89.     return 0;
  90. }
复制代码
test.c文件:main函数的执行入口
  1. //
  2. // Created by Lenovo on 2023/9/9.
  3. //
  4. #include <stdio.h>
  5. #include "sqlist.h"
  6. void test_insert(){
  7.     sqlink L;
  8.     L=list_create();
  9.     if (L==NULL)
  10.         return;
  11.     list_insert(L, 10, 0);
  12.     list_insert(L, 20, 0);
  13.     list_insert(L, 30, 0);
  14.     list_insert(L, 40, 0);
  15.     list_insert(L, 50, 0);
  16.     list_show(L);
  17.     list_insert(L, 100, -1000);
  18.     list_show(L);
  19.     list_delete(L);
  20. }
  21. int main(){
  22.     test_insert();
  23.     return 0;
  24. }
复制代码
2.执行步骤

2.1 使用gcc进行编译

c语言程序编译的过程如下:
预编译-编译-汇编-连接
汇编:gcc -c sqlist.c -o sqlist.o
gcc -c test.c -o test.o
连接:可执行文件:gcc sqlist.o test.o -o test
以上3步可直接等价于:gcc *.c -o test
程序运行成功:


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

篮之新喜

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表