• 数据机构——顺序表的基本操作


    一、实验要求  

     二、实验代码

    1. # include "bits/stdc++.h"
    2. using namespace std;
    3. #define OK 1
    4. #define ERROR 0
    5. #define OVERFLOW -2
    6. #define MAXSIZE 100 //定义顺序表的最大长度
    7. //定义数据结构体
    8. typedef struct book
    9. {
    10. int number; //编号
    11. string name; //名称
    12. float price; //定价
    13. }book;
    14. //定义顺序表结构体
    15. typedef struct
    16. {
    17. book data[MAXSIZE]; //顺序表数据
    18. int length; //顺序表长度
    19. }SqlList;
    20. //顺序表的初始化函数
    21. bool InitList_Sq(SqlList &L)
    22. {
    23. for (int i = 0; i < MAXSIZE; i++)
    24. {
    25. L.data[i].number = 0;
    26. L.data[i].name = 'a';
    27. L.data[i].price = 0;
    28. }
    29. L.length = 0;
    30. SqlList *elem = &L;
    31. if(elem != NULL)
    32. return OK;
    33. else
    34. return ERROR;
    35. }
    36. //顺序表的插入操作
    37. bool ListInsert_Sq(SqlList &L, int que, book num)
    38. {
    39. //判断插入的位置是否合法
    40. if(que<0 || que>L.length)
    41. return ERROR;
    42. if(L.length >= MAXSIZE)
    43. return ERROR;
    44. for (int i = L.length; i > que; i--)
    45. {
    46. L.data[i] = L.data[i-1];
    47. }
    48. //添加新元素
    49. L.data[que].number = num.number;
    50. L.data[que].name = num.name;
    51. L.data[que].price = num.price;
    52. L.length ++;
    53. return true;
    54. }
    55. //顺序表的打印输出操作
    56. void Printout_Sq(SqlList &L)
    57. {
    58. cout << "编号" << setw(15) << "书名" << setw(15) << "定价" << endl;
    59. for (int i = 0; i < L.length; i++)
    60. {
    61. cout << L.data[i].number << setw(15) << L.data[i].name << setw(15) << L.data[i].price << endl;
    62. }
    63. cout<
    64. }
    65. //顺序表的取出(按位置取出)
    66. bool GetElem(SqlList &L, int i)
    67. {
    68. //判断查询的位置是否合理
    69. if(i<0 || i>=L.length)
    70. return ERROR;
    71. cout << "第" << i << "条的图书信息为: " << endl;
    72. cout << L.data[i].number << setw(15) << L.data[i].name << setw(15) << L.data[i].price << endl;
    73. }
    74. //顺序表的查找
    75. int LocateElem_Sq(SqlList &L, float price)
    76. {
    77. for (int i = 0; i < L.length; i++)
    78. {
    79. if(L.data[i].price == price)
    80. {
    81. cout << "查找成功" << endl;
    82. cout << "该价格对应的书名为: " << L.data[i].name << endl;
    83. return 0;
    84. }
    85. }
    86. cout << "查询失败" << endl;
    87. return 0;
    88. }
    89. //顺序表的删除
    90. bool ListDelete_Sq(SqlList &L, int que)
    91. {
    92. if(que<0 || que>=L.length)
    93. return ERROR;
    94. for (int i = que; i < L.length; i++)
    95. {
    96. L.data[i].number = L.data[i+1].number;
    97. L.data[i].price = L.data[i+1].price;
    98. L.data[i].name = L.data[i+1].name;
    99. }
    100. L.length --;
    101. return OK;
    102. }
    103. int main()
    104. {
    105. setlocale(LC_ALL,"zh-CN"); //设置程序为中文编码
    106. //========第一问===========
    107. SqlList L;
    108. if(InitList_Sq(L))
    109. cout << "成功建立顺序表!!!" << endl;
    110. else
    111. cout << "顺序表建立失败!!!" << endl;
    112. //========第二问===========
    113. for (int i = 0; i < 3; i++)
    114. {
    115. book book;
    116. cin >> book.number; //编号
    117. cin >> book.name; //书名
    118. cin >> book.price; //定价
    119. ListInsert_Sq(L, i, book);
    120. }
    121. Printout_Sq(L); //输出
    122. //========第三问===========
    123. cout << "请输入一个位置用来获取数据:" << endl;
    124. for (int i = 0; i < 2; i++)
    125. {
    126. int num;
    127. cin >> num;
    128. GetElem(L, num);
    129. }
    130. //========第四问===========
    131. cout << "请输入所查找的书本价格:" << endl;
    132. float price_elect;
    133. cin >> price_elect;
    134. LocateElem_Sq(L, price_elect);
    135. //========第五问===========
    136. cout << "请输入所要删除的书籍的编号:";
    137. int num_elect;
    138. cin >> num_elect;
    139. if(ListDelete_Sq(L, num_elect))
    140. cout << "删除成功" << endl;
    141. else
    142. cout << "删除失败" << endl;
    143. Printout_Sq(L); //输出
    144. return 0;
    145. }

  • 相关阅读:
    蚂蚁链发布全新Web3品牌ZAN,涉及RWA、合规等服务
    AT800(3000) +昇腾300V 之 第一个例子图片分类
    【 OpenGauss源码学习 —— (hash_search)】
    TypeScript 学习笔记 — 类型兼容 (十)
    中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用
    Oracle 学习之 DML 语句
    亚马逊第三大卖家国家巴基斯坦电商营商环境如何?且看跨境电商数字身份验证服务商ADVANCE.AI要参解析
    Codeforces Round #832 (Div. 2)C,D ICPC澳门 D(模拟) F(构造) G(博弈)
    MySQL基础篇之多表查询(内连接、外连接、自连接、子查询、union)
    浅谈压力测试的作用是什么
  • 原文地址:https://blog.csdn.net/qq_58011370/article/details/128085060