055.链表操作(2)

举报
C语言与CPP编程 发表于 2022/05/01 01:00:14 2022/05/01
【摘要】 #include "stdafx.h"#include "stdio.h"#ifndef SQLIST_H#define SQLIST_H#include <stdlib.h>#define List_INIT_SIZE 100 //线性表的存储空间初始大小#define LIST_INCREMENT 10 /...

  
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #ifndef SQLIST_H
  4. #define SQLIST_H
  5. #include <stdlib.h>
  6. #define List_INIT_SIZE 100 //线性表的存储空间初始大小
  7. #define LIST_INCREMENT 10 //分配增量
  8. #define OVERFLOW -2
  9. #define OK 1
  10. #define ERROR -1
  11. #define TRUE 1
  12. #define FALSE 0
  13. typedef int ElemType;
  14. typedef struct{
  15. ElemType *elem; //存储空间基址
  16. int length; //当前长度
  17. int size; //当前存储容量(sizeof(ElemType)为单位)
  18. }SqList;
  19. int InitList(SqList &L)
  20. {
  21. //构建一个线性表L
  22. L.elem = (ElemType*)malloc(List_INIT_SIZE*sizeof(ElemType));
  23. if(!L.elem)
  24. {
  25. exit(OVERFLOW);
  26. }
  27. L.length = 0;
  28. L.size = List_INIT_SIZE;
  29. return OK;
  30. }//InitList
  31. void DestoryList(SqList &L)
  32. {
  33. if(!L.elem)
  34. {
  35. delete L.elem;
  36. L.elem = NULL;
  37. }
  38. L.length = 0;
  39. L.size = 0;
  40. }//DestoryList
  41. void ClearList(SqList &L)
  42. {
  43. L.elem = NULL;
  44. L.length = 0;
  45. L.size = List_INIT_SIZE;
  46. }//ClearList
  47. int ListEmpty(SqList L)
  48. {
  49. if(L.length > 0)
  50. {
  51. return TRUE;
  52. }
  53. else
  54. {
  55. return FALSE;
  56. }
  57. }//ListEmpty
  58. int ListLength(SqList L)
  59. {
  60. return L.length;
  61. }//ListLength
  62. int ListInsert(SqList &L, int i, ElemType e)
  63. {
  64. if(i<1 || i>ListLength(L)+1) //需满足条件1<i<ListLength(L)+1
  65. {
  66. return ERROR;
  67. }
  68. ElemType * NewBase; //新的基址
  69. if(L.length > L.size) //空间已满
  70. {
  71. NewBase = (ElemType*)malloc((L.size+LIST_INCREMENT)*sizeof(ElemType)); //申请新的空间
  72. if(!NewBase)
  73. {
  74. exit(OVERFLOW);
  75. }
  76. L.elem = NewBase;
  77. L.size +=LIST_INCREMENT;
  78. delete NewBase;
  79. NewBase = NULL;
  80. }
  81. ElemType *p;
  82. ElemType *temp;
  83. p = &(L.elem[i-1]); //取得i的位置,即插入位置
  84. for(temp = &(L.elem[L.length-1]);temp>p;--temp) //将插入点后的所有元素向后移动一位
  85. {
  86. *(temp+1) = *temp;
  87. }
  88. *p = e;
  89. ++L.length;
  90. return OK;
  91. }//ListInsert
  92. int ListDelete(SqList &L, int i, ElemType &e)
  93. {
  94. if (i<0 || i>ListLength(L)) {
  95. return ERROR;
  96. }
  97. ElemType *p;
  98. p = &(L.elem[i-1]);
  99. e = *p;
  100. ElemType *pos;
  101. pos =&(L.elem[L.length-1]);
  102. for(++p; p<=pos; ++p){
  103. *(p-1) = *p;
  104. }
  105. --L.length;
  106. return OK;
  107. }
  108. #endif //SQLIST_H
  109. int main(){
  110. SqList list;
  111. ElemType e;
  112. InitList(list);
  113. ListInsert(list, 1, 1);
  114. ListInsert(list, 2, 2);
  115. ListInsert(list, 3, 3);
  116. ListInsert(list, 4, 4);
  117. ListInsert(list, 5, 5);
  118. for(int i =0; i<list.length; i++){
  119. printf("%d\n",list.elem[i]);
  120. }
  121. ListDelete(list, 2, e);
  122. for(i =0; i<list.length; i++){
  123. printf("%d\n",list.elem[i]);
  124. } printf("%d\n", list.length);
  125. return 1;
  126. }

文章来源: blog.csdn.net,作者:程序员编程指南,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_41055260/article/details/124494993

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。