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
| #include <iostream>
using namespace std;
#define ok 1 #define error 0 #define maxsize 100 #define overflow -2 typedef int status; typedef int Elemtype; typedef struct { Elemtype* elem; int length; } sqlist;
int initlist(sqlist& l) { l.elem = new Elemtype[maxsize]; if (!l.elem) exit(overflow); l.length = 0; return ok; }
int clearlist(sqlist& l) { l.length = 0; delete[] l.elem; cout << "已清空线性表" << endl; return ok; }
int isempty(sqlist& l) { if (l.length == 0) cout << "线性表为空" << endl; else cout << "线性表不为空" << endl; return ok; }
int qqlength(sqlist l) { cout << "线性表长度为: " << l.length << endl; return ok; }
int getelem(sqlist l, Elemtype& e) { cout << "请输入查询的位置:" << endl; int i; cin >> i; if (i < 1 || i > l.length) { cout << "输入位置错误" << endl; return error; } e = l.elem[i - 1]; cout << "第" << i << "个元素为: " << e << endl; return ok; }
void qqqmqu(sqlist l) { int i; cout << "求第几个元素的前驱" << endl; cin >> i; if (i <= 1 || i > l.length) { cout << "输入数字错误" << endl; } else cout << "(" << i - 1 << "," << l.elem[i - 2] << ")" << endl;
}
void qqhzji(sqlist l) { int i; cout << "求第几个元素的后继" << endl; cin >> i; if (i < 1 || i >= l.length) { cout << "输入错误" << endl; } else cout << "(" << i+1 << "," << l.elem[i] << ")" << endl;
}
status listinsert(sqlist& l, int i, Elemtype e) { int j; if (l.length == maxsize) { cout << "线性表已满" << endl; return error; } if (i<1 || i>l.length + 1) { cout << "插入位置错误" << endl; return error; } if (i >= 1 && i <= l.length + 1) { for (j = l.length - 1; j >= i - 1; j--) { l.elem[j + 1] = l.elem[j]; }
l.elem[i - 1] = e; l.length++; return ok; } return error; }
status listdelete(sqlist& l, int i, Elemtype& e) { int j; if (l.length == 0) return error; if (i < 1 || i > l.length) return error; else { e = l.elem[i - 1]; for (int j = i - 1; j < l.length - 1; j++) l.elem[j] = l.elem[j + 1]; } l.length--; return ok; }
void output(sqlist& l) { cout << "线性表中的元素为: " << endl; for (int i = 0; i < l.length; i++) { cout << "(" << i + 1 << ", " << l.elem[i] << ")" << endl; } cout << endl; }
int main() { sqlist l; int i = 0; initlist(l); Elemtype e = 0; int select = 0; while (true) { cout << "1. 清空顺序表" << endl; cout << "2. 判断线性表是否为空" << endl; cout << "3. 求线性表的长度" << endl; cout << "4. 获取线性表指定位置元素" << endl; cout << "5. 求前驱" << endl; cout << "6. 求后继" << endl; cout << "7. 在线性表指定位置插入元素" << endl; cout << "8.删除线性表指定位置元素 " << endl; cout << "9.显示线性表" << endl; cout << " 退出,输入一个负数" << endl; cin >> select;
switch (select) { case 1: { clearlist(l); break; }
case 2: isempty(l); break; case 3: qqlength(l); break;
case 4: getelem(l, e); break; case 5: qqqmqu(l); break; case 6: qqhzji(l); break; case 7: { int n; cout << "请输入要连续插入的元素数量: "; cin >> n; for (int j = 0; j < n; j++) { int e, i; cout << "请输入插入的位置和元素: "; cin >> i >> e; listinsert(l, i, e); } break; } case 8: { int i, e; cout << "请输入要删除的位置: "; cin >> i; if (listdelete(l, i, e) == ok) cout << "删除成功, 删除的元素为: " << e << endl; else cout << "删除失败" << endl; break; } case 9: output(l); break; default: return 0; } } return 0; }
|