当前位置:   article > 正文

【数据结构(C语言版) 线性表顺序表示与实现】_c语言status initlist

c语言status initlist

终极版
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <windows.h>
  5. #define TRUE 1
  6. #define FALSE 0
  7. #define OK 1
  8. #define ERROR 0
  9. #define INFEASIBLE -1
  10. #define OVERFLOW -2
  11. #define LIST_INIT_SIZE 100
  12. #define LISTINCREMENT 10
  13. //Status是函数的类型,其值是函数结果状态代码
  14. typedef int Status;
  15. typedef int ElemType;
  16. typedef struct
  17. {
  18. ElemType *elem;
  19. int length;
  20. int listsize;
  21. }SqList;
  22. using namespace std;
  23. Status InitList(SqList &L)
  24. {
  25. L.elem=(ElemType *) malloc(LIST_INIT_SIZE*sizeof(ElemType));
  26. if (!L.elem)
  27. exit(OVERFLOW);
  28. L.length=0;
  29. L.listsize=LIST_INIT_SIZE;
  30. return OK;
  31. }
  32. Status ListInsert_Sq(SqList &L, int i, ElemType e)
  33. {
  34. ElemType *newbase, *p,*q;
  35. if (i<1||i>L.length+1)
  36. return ERROR;
  37. if (L.length>=L.listsize)
  38. {
  39. newbase = (ElemType *)realloc(L.elem,
  40. (L.listsize+LISTINCREMENT)*sizeof(ElemType)) ;
  41. if (!newbase) exit (OVERFLOW);
  42. L.elem = newbase;
  43. L.listsize=L.listsize+LISTINCREMENT;
  44. }
  45. q=&(L.elem[i-1]);
  46. for (p=&(L.elem[L.length-1]);p>=q; --p) *(p+1)=*p;
  47. *q=e;
  48. L.length++;
  49. return OK;
  50. }
  51. Status ListDelete_Sq(SqList &L, int i, ElemType &e)
  52. {
  53. ElemType *p,*q;
  54. if ((i<1)||i>L.length) return ERROR;
  55. p=&L.elem[i-1];
  56. e=*p;
  57. q=L.elem+L.length-1;
  58. for (++p; p<=q; ++p)
  59. *(p-1)=*p;
  60. --L.length;
  61. return OK;
  62. } //ListDelete_Sq
  63. Status out(SqList L){
  64. cout << "该链表中含有的元素:" << endl;
  65. for(int i = 0;i < L.length;i++)
  66. cout << L.elem[i] << " " ;
  67. cout << endl;
  68. }
  69. Status in(SqList &L, int i){
  70. cout << "请依次" << i << "个输入数据" << endl;
  71. for(int j = 0;j < i; j++)
  72. cin >> L.elem[j];
  73. L.length = i;
  74. return OK;
  75. }
  76. int main()
  77. {
  78. SqList L;
  79. InitList(L);
  80. while(1)
  81. {
  82. int i = 0;
  83. cout << "请选择操作:" << endl
  84. << "1.输入数据" << " "
  85. << "2.插入数据" << " "
  86. << "3.删除数据" << " "
  87. << "4.输出" << " "
  88. <<"5.结束"<< endl;
  89. cin >> i;
  90. if(i == 1){
  91. int num = 0;
  92. cout << "请输入需要输入的个数:" << endl;
  93. cin >> num;
  94. in(L,num);
  95. }
  96. if(i == 2){
  97. int h,e;
  98. cout << "请输入插入位置和插入元素:" << endl;
  99. cin >> h >> e;
  100. ListInsert_Sq(L,h,e);
  101. }
  102. if(i == 3){
  103. int h,e;
  104. cout << "请输入删除位置:" << endl;
  105. cin >> h;
  106. ListDelete_Sq(L, h, e);
  107. }
  108. if(i == 4){
  109. out(L);
  110. }
  111. if(i == 5) break;
  112. cout << "任意键进行下一步:\n";
  113. getchar();
  114. getchar();
  115. system("cls");
  116. }
  117. cout << "game over" << endl;
  118. return 0;
  119. }
  1. #include <cstdlib>
  2. #include <iostream>
  3. #define TRUE 1
  4. #define FALSE 0 // 正错
  5. #define OK 1
  6. #define ERROR 0 //状态
  7. #define INFEASIBLE -1 //不可行
  8. #define OVERFLOW -2 //溢出
  9. #define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
  10. #define LISTINCREMENT 10 // 线性表存储空间的分配增量
  11. // 一些常用表示
  12. typedef int ElemType; //类型,可改为 double ,long long、、、、
  13. typedef int Status; //状态
  14. typedef struct{
  15. int * elem; //存储空间基址
  16. int length; //当前长度
  17. int listsize; //当前分配的存储容量(以sizeof(Elemtype)为单位)
  18. }SqList;
  19. Status InitList(SqList &L){
  20. L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));//分配一个ElemType类型的LIST_INIT_SIZE长度的动态空间
  21. if(!L.elem) exit(OVERFLOW);//检查分配
  22. L.length = 0;//将当前长度赋值为0
  23. L.listsize = LIST_INIT_SIZE;//初始存储容量
  24. return OK;//创建成功,返回状态
  25. }
  26. using namespace std;
  27. int main()
  28. {
  29. SqList L;//定义一个链表
  30. InitList(L);//调用函数InitList
  31. return 0;
  32. }

草稿里躺了四年了,发出来吧。。。。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/529461
推荐阅读
相关标签
  

闽ICP备14008679号