1 #include "stdafx.h"
2 #include <iostream>
3 using namespace std;
4 #define maxSize 100
5
6 //length要改变 使用引用类型
7 int insertElem(int sqList[], int &length, int p, int e) {
8 if (p < 0||p>length||length==maxSize) {
9 return 0;
10 }
11 for (int i = length - 1; i >= p; --i) {
12 sqList[i + 1] = sqList[i];
13 }
14 sqList[p] = e;
15 ++length;
16 return 1;
17 }
18
19 //可删除元素下标p的取值范围为:0~length-1
20 //当表长length等于0的时候不可以再删除元素,移动元素从前往后进行
21 //&e删除元素取出来 p是位置
22
23 int deleteElem(int sqList[], int &length, int p, int &e) {
24 if (p<0||p>length-1) {
25 return 0;
26 }
27 e = sqList[p];
28 for (int i = p; i < length - 1;++i) {
29 sqList[i] = sqList[i + 1];
30 }
31 --length;
32 return 1;
33 }
34
35 void main() {
36
37 int sqList[maxSize] = { 1,2,3,4,5,6,7 };
38 int length = 7;
39 for (int i = 0; i < length - 1;i++) {
40 cout << "原数组第"<<i<<"个位置: " << sqList[i] << endl;
41 }
42 int l = sizeof(sqList) / sizeof(sqList[0]);
43
44
45 int r = insertElem(sqList, length, 3, 9);
46 cout << "插入元素返回 " << r << endl;
47
48 for (int i = 0; i < 7; i++) {
49 cout << "新数组第" << i << "个位置: " << sqList[i] << endl;
50 }
51
52
53 }