1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| #include <iostream>
#define MaxSize 10
class SqList { public: int data[MaxSize]; int length;
SqList() { length = 0; for (int i = 0; i < MaxSize; ++i) { data[i] = 0; } } };
void InitList(SqList &L); void DestroyList(SqList &L); void ClearList(SqList &L); void PrintList(const SqList &L); bool ListEmpty(const SqList &L); int ListLength(const SqList &L); bool GetElem(const SqList &L, int i, int &e); int LocateElem(const SqList &L, int e); bool PriorElem(const SqList &L, int cur_e, int &pre_e); bool NextElem(const SqList &L, int cur_e, int &next_e); bool ListInsert(SqList &L, int i, int e); bool ListDelete(SqList &L, int i, int &e); void ListTraverse(const SqList &L, void (*visit)(int)); bool LocateChangeElem(SqList &L, int e, int em); bool GetChangeElem(SqList &L, int i, int em); void testModule();
void InitList(SqList &L) { L.length = 0; }
void DestroyList(SqList &L) { }
void ClearList(SqList &L) { L.length = 0; for (int i = 0; i < MaxSize; ++i) { L.data[i] = 0; } }
void PrintList(const SqList &L) { std::cout << "开始打印顺序表\n"; for (int i = 0; i < L.length; ++i) { std::cout << "Data[" << i << "]==" << L.data[i] << "\n"; } std::cout << "打印结束!\n"; }
bool ListEmpty(const SqList &L) { return L.length == 0; }
int ListLength(const SqList &L) { return L.length; }
bool GetElem(const SqList &L, int i, int &e) { if (i < 1 || i > L.length) { return false; } e = L.data[i - 1]; return true; }
int LocateElem(const SqList &L, int e) { for (int i = 0; i < L.length; ++i) { if (L.data[i] == e) return i + 1; } return 0; }
bool PriorElem(const SqList &L, int cur_e, int &pre_e) { for (int i = 1; i < L.length; ++i) { if (L.data[i] == cur_e) { pre_e = L.data[i - 1]; return true; } } return false; }
bool NextElem(const SqList &L, int cur_e, int &next_e) { for (int i = 0; i < L.length - 1; ++i) { if (L.data[i] == cur_e) { next_e = L.data[i + 1]; return true; } } return false; }
bool ListInsert(SqList &L, int i, int e) { if (i < 1 || i > L.length + 1 || L.length >= MaxSize) return false; for (int j = L.length; j >= i; --j) { L.data[j] = L.data[j - 1]; } L.data[i - 1] = e; L.length++; return true; }
bool ListDelete(SqList &L, int i, int &e) { if (i < 1 || i > L.length) { return false; } e = L.data[i - 1]; for (int j = i; j < L.length; ++j) { L.data[j - 1] = L.data[j]; } L.length--; return true; }
void ListTraverse(const SqList &L, void (*visit)(int)) { for (int i = 0; i < L.length; ++i) { visit(L.data[i]); } }
bool LocateChangeElem(SqList &L, int e, int em) { int bitOrder = LocateElem(L, e); if (bitOrder != 0) { L.data[bitOrder - 1] = em; return true; } return false; }
bool GetChangeElem(SqList &L, int i, int em) { if (i < 0 || i >= L.length) return false; L.data[i] = em; return true; }
void visit(int a){ std::cout<<a<<std::endl; }
void testModule() { SqList L; InitList(L);
ListInsert(L, 1, 1); ListInsert(L, 2, 2); ListInsert(L, 3, 3);
if (ListInsert(L, 2, 4)) { std::cout << "插入成功了\n"; } else { std::cout << "插入失败了,i的位置不合法,请检查\n"; }
int e = -1; if (ListDelete(L, 2, e)) { std::cout << "删除成功!删除的值是:" << e << "\n"; } else { std::cout << "删除失败,请检查位序是否正确\n"; }
std::cout << "数组当前长度是多少?" << ListLength(L) << "\n";
if (GetElem(L, 1, e)) { std::cout << "第一个元素是:" << e << "\n"; } else { std::cout << "查找失败,位置不合法\n"; }
std::cout << "第一个值为3的元素在什么位置?" << LocateElem(L, 3) << "\n";
PrintList(L);
int e1 = 2; int em1 = 6; int i = 1; int em2 = 7; std::cout << "开始测试【改】\n" << "第一种方式先按值查找后改值\n"; if (LocateChangeElem(L, e1, em1)) { std::cout << "第一种先按值查找后改值成功啦,改变后的值如下:\n"; PrintList(L); } else { std::cout << "第一种先按值查找后改值失败了,再检查一下吧!\n"; } std::cout << "第二种先按位序查找后改值\n"; if (GetChangeElem(L, i, em2)) { std::cout << "第二种先按位序查找后改值的方式成功啦,改变后的值如下:\n"; PrintList(L); } else { std::cout << "第二种先按位序查找后改值的方式失败了,再检查一下吧!\n"; } if (ListEmpty(L)) { std::cout << "顺序表为空!\n"; } else { std::cout << "顺序表非空!\n"; }
PrintList(L); ListTraverse(L,visit); }
int main() { testModule(); return 0; }
|