|
发表于 2016-3-26 23:32:54
|
显示全部楼层
#include <iostream>
using namespace std;
#define MAX 10
#define INC 5
typedef int DataType;
struct LineTable
{
DataType *s;
int last;
int max;
};
bool AddData(LineTable *p, DataType e);
bool DeleteData(LineTable *p, int i);
int main()
{
int n,i;
LineTable *p = (LineTable*)malloc(sizeof(LineTable));
if (!p)
exit(0);
p->s = (DataType*)malloc(sizeof(DataType)*MAX);
p->last = 0;
p->max = MAX;
cout << "请输入要存储的数字" << endl;
while (cin >> n && n!=0)
{
AddData(p, n);
}
i = 0;
while (i<p->last)
{
cout << p->s[i] << ' ';
i++;
}
cout << endl;
DeleteData(p, 5);
i = 0;
while (i<p->last)
{
cout << p->s[i] << ' ';
i++;
}
system("pause");
return 0;
}
bool AddData(LineTable *p,DataType e)
{
if (p->last >= p->max)
{
p->s = (DataType*)realloc(p->s, sizeof(DataType)*(p->max + INC));//如果重新分配失败,就返回假
if (!p->s)
return false;
p->max = p->max + INC;
}
p->s[p->last] = e;
p->last = p->last + 1;
return true;
}
bool DeleteData(LineTable *p, int i)
{
if (i < 0 && i >= p->last)
return false;
while (i + 1 < p->last)
{
p->s[i] = p->s[i + 1];
i++;
}
p->last = p->last - 1;
return true;
} |
|