【数据结构】线性表顺序结构之增删操作

来源:互联网 发布:牙签弩淘宝20¥ 编辑:程序博客网 时间:2024/06/14 05:41

先上图:


时间效率分析:是比较差的。



代码:

#include <iostream>using namespace std;const int max=100;struct LIST{int abc[max];//末尾标志位int last;};typedef int position;position End(LIST L){return (L.last + 1);}//插入void Insert(int x, position p, LIST &L){position q;if (L.last >= max - 1)cout << "错误" << endl;//末尾下标不越界else if ((p > L.last + 1) || (p < 1))cout << "不存在" << endl;else{for (q = L.last; q >= p; q--){//从后往前,将前一个的值赋给后一个L.abc[q + 1] = L.abc[q];}//修改last标志位L.last = L.last + 1;//放入xL.abc[p] = x;}}//剔除int out(position p, LIST &L){int X=0;X = L.abc[p];position q=p;for (; q<L.last; q++){L.abc[q] = L.abc[q + 1];}L.last = L.last - 1;return X;}void main(){int x;position p;LIST L = { {6,9,8,7,5,6}, 6};cout << "输入要插入的x和插入的位置" << endl;cin >> x>>p;Insert(x, p, L);for (int i = 0; i < L.last; i++){cout << L.abc[i] << endl;}cout << endl;out(2, L);for (int i = 0; i < L.last; i++){cout << L.abc[i] << endl;}system("pause");}


测试:


总结:


优点:随机访问

缺点:需要移动大量元素,时间效率低。存储空间为定值。

阅读全文
0 0
原创粉丝点击