// ConsoleApplication1.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。
//
#include
using namespace std;
#define MAX_SIZE 100
typedef struct
{
int* elems;
int length;
int size;
}SqList;
bool initList(SqList& L)
{
L.elems = new int[MAX_SIZE];
if (!L.elems) return false;
L.length = 0;
L.size = MAX_SIZE;
return true;
}
bool listAppend(SqList& L, int e)
{
if (L.length == MAX_SIZE) return false;
L.elems[L.length] = e;
L.length++;
return true;
}
bool listInsert(SqList& L, int i, int e)
{
if (i < 0 || i >= L.length)return false;
if (L.length == MAX_SIZE)return false;
for (int j = L.length - 1; j >= i; i--)
{
L.elems[j + 1] = L.elems[j];
}
L.elems[i] = e;
L.length++;
return true;
}
bool listDelete(SqList& L, int i)
{
if (i<0 || i>L.length)return false;
if (i == L.length - 1)
{
L.length–;
return true;
}
for (int j = i; j < L.length - 1; j++)
{
L.elems[j] = L.elems[j + 1];
}
L.length--;
return true;
}
void listPrint(SqList& L)
{
cout << “顺序表元素size:” << L.size << “,已保存元素个数length:”
<< L.length << endl;
for (int j = 0; j <= L.length - 1; j++)
{
cout << L.elems[j] << " ";
}
cout << endl;
}
void destrouList(SqList& L)
{
if (L.elems)delete L.elems;
L.length = 0;
L.size = 0;
}
int main()
{
std::cout << “Hello World!\n”;
SqList list;
int i, e;
cout << "顺序表初始化...." << endl;
if (initList(list))
{
cout << "顺序表初始化成功!" << endl;
}
int count = 0;
cout << "请输入要添加的元素个数:" << endl;
cin >> count;
for (int i = 0; i < count; i++)
{
cout << "请输入要添加元素e:";
cin >> e;
if (listAppend(list, e))
{
cout << "添加成功" << endl;
}
else
{
cout << "添加失败" << endl;
}
}
listPrint(list);
cout << "请输入要插入的位置和要插入的数据e:";
cin >> i >> e;
if (listInsert(list, i, e))
{
cout << "插入成功" << endl;
}
else
{
cout << "插入失败" << endl;
}
listPrint(list);
cout << "请输入要删除的位置i:";
cin >> i;
if (listDelete(list, i))
{
cout << "删除成功" << endl;
}
else
{
cout << "删除失败" << endl;
}
listPrint(list);
cout << "顺序表销毁" << endl;
destrouList(list);
//listPrint(list);
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件