#include<iostream>
using namespace std;
#define size 20
#define stepsize 10
struct array
{
int* spot;//数组的名字
int curlen;//数组的当前长度
int arraylen;//数组存储空间的长度
};
void initarray(array* p)//数组的初始化
{
p->spot = new int[size];
p->curlen = 0;
p->arraylen = size;
}
void output(array p)
{
cout << "当前数组的长度为:" << p.curlen << endl;
cout << "当前数组的内存空间为:" << p.arraylen << endl;
if (p.curlen != 0)
{
cout << "该数组的内容为:";
for (int i = 0; i < p.curlen; i++)
{
cout <<p. spot[i] << ",";
}
cout << endl;
}
}
void insert(array* p,int pos,int value)
{
if ((pos<0) || (pos>p->curlen))
{
cout << "插入的位置有错误" << endl;
}
else
if ((p->curlen + 1) < p->arraylen)//判断是否越界
{
int* temp = new int[p->arraylen + stepsize];
for (int l = 0; l < p->curlen; l++)
{
temp[l] = p->spot[l];
}
delete[]p->spot;
}
else
{
int i, j;
j = p->curlen+1;
i = j - 1;
for (int k = 0; k < p->curlen - pos + 1; k++,i--,j--)
{
p->spot[j] = p->spot[i];
}
p->spot[i] = value;
p->curlen++;
}
}
int main()
{
array myarray;
initarray(&myarray);
output(myarray);
for (int i = 0; i < 5; i++)
{
insert(&myarray, 0, i);
}
output(myarray);
cout << endl;
return 0;
}