鱼C论坛

 找回密码
 立即注册
查看: 2390|回复: 0

[学习笔记] 顺序表的操作完整代码(c语言)

[复制链接]
发表于 2019-9-17 13:37:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>

  4. #define maxsize 100  //定义顺序表的最大储存个数是100
  5. typedef int ElemType;

  6. //定义顺序表的结构,并用ElemType的数组来储存数组元素,并且元素的个数为length
  7. typedef struct
  8. {
  9.         ElemType data[maxsize];
  10.         int length;  
  11. }List;

  12. //建立顺序表(定义一个函数建立顺序表,将数组a的元素给顺序表)
  13. void creatList(List*& L, ElemType a[], int n)
  14. {
  15.         int length = 0;
  16.         L = (List*)malloc(sizeof(List));
  17.         for (int i = 0; i < n; i++)
  18.         {
  19.                 L->data[i] = a[i];
  20.                 length += 1;
  21.         }
  22.         L->length = length;
  23. }

  24. //初始化顺序表
  25. void initList(List*& L)
  26. {
  27.         L = (List*)malloc(sizeof(List));  //给顺序表L分配空间
  28.         L->length = 0;   //定义L的长度length为0
  29. }

  30. //销毁顺序表
  31. void destroyList(List*& L)
  32. {
  33.         free(L);  //释放顺序表L的空间  
  34. }

  35. //判断顺序表是否为空
  36. bool listEmpty(List*& L)
  37. {
  38.         return(L->length == 0);  //若为空,返回true,如果不为空,返回false
  39. }

  40. //求线性表的长度
  41. int ListLength(List*& L)
  42. {
  43.         return (L->length);
  44. }

  45. //输出线性表
  46. void outputList(List*& L)
  47. {
  48.         if (listEmpty(L))  //如果顺序表为空
  49.         {
  50.                 printf("顺序表为空!\n");
  51.                 return;
  52.         }
  53.         else  //否则逐个输出元素
  54.         {
  55.                 for (int i = 0; i < L->length; i++)
  56.                 {
  57.                         printf("%d ", L->data[i]);
  58.                 }
  59.                 printf("\n");
  60.         }
  61. }

  62. //取顺序表中某个位置的元素
  63. void GetElem(List*& L, int i, int& e)  //在线性表L中取第i位置的值并给e
  64. {
  65.         if (i < 0 || i > L->length || L->length == 0)  //如果i不合法或者L为空
  66.         {
  67.                 printf("顺序表为空!\n");
  68.                 return;
  69.         }
  70.         else
  71.         {
  72.                 e = L->data[i-1];
  73.         }
  74. }

  75. //判断顺序表中是否存在某个元素,并返回所在位置
  76. void LocateElem(List*& L, ElemType e)
  77. {
  78.         for (int i = 0; i < L->length; i++)
  79.         {
  80.                 if (e == L->data[i])
  81.                 {
  82.                         printf("位置在%d处\n", i + 1);
  83.                         return;
  84.                 }
  85.         }       
  86. }

  87. //插入元素(在线性表的第i个位置插入元素e)
  88. void ListInsert(List*& L, int i, ElemType e)
  89. {
  90.         if (i<0 || i>L->length + 1)  //如果i不合法
  91.         {
  92.                 printf("输入位置不合法!\n");
  93.                 return;
  94.         }
  95.         else
  96.         {
  97.                 for (int j = L->length-1; j >= i-1; j--)
  98.                 {
  99.                         L->data[j + 1] = L->data[j];
  100.                 }
  101.                 L->data[i - 1] = e;
  102.                 L->length += 1;
  103.         }
  104. }


  105. //删除元素
  106. void ListDelete(List*& L, int i)  //在顺序表中删除第i个元素
  107. {
  108.         if (i<0 || i>L->length)  
  109.         {
  110.                 printf("删除位置不合法!\n");
  111.                 return;
  112.         }
  113.         else
  114.         {
  115.                 for (int j = i-1; j < L->length; j++)
  116.                 {
  117.                         L->data[j] = L->data[j + 1];
  118.                 }
  119.                 L->length -= 1;
  120.         }
  121. }

  122. int main()
  123. {
  124.         List *L;
  125.         int e=99;
  126.         int a[4] = { 0,1,2,3 };
  127.         creatList(L, a, 4);
  128.         //initList(L);
  129.         outputList(L);
  130.         printf("该顺序表的长度是%d\n", ListLength(L));
  131.         //取线性表某个位置的值
  132.         GetElem(L, 1, e);
  133.         printf("取出来的值是:%d\n", e);

  134.         //判断是否有某个元素
  135.         LocateElem(L, 1);

  136.         //插入
  137.         ListInsert(L, 10, 100);
  138.         ListInsert(L, 2, 100);
  139.         outputList(L);

  140.         //删除
  141.         ListDelete(L, 1);
  142.         outputList(L);

  143.         //销毁
  144.         destroyList(L);
  145.         outputList(L);
  146.        
  147.         return 0;
  148. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-14 00:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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