#include <iostream> using namespace std; #define MAX_SIZE 100 /* typedef struct _SqList SqList; struct _SqList {//顺序表结构地址 int* elems; //顺序表的基地址 int lenth; //顺序表实际元素个数 int size; //顺序表总的空间大小 }; */ //可以用下面的简单方式 typedef struct { int* elems; //顺序表的基地址 int length; //顺序表实际元素个数 int size; //顺序表总的空间大小 }SqList; bool initList(SqList& L)//构造一个空的顺序表,成功返回true { L.elems = new int[MAX_SIZE]; //为顺序表分配MAX_SIZE个int元素的空间 if (!L.elems) return false; //存储分配失败 L.length = 0; L.size = MAX_SIZE; return true; } void listPrint(SqList& L) { cout << "顺序表存储空间Size:" << L.size << ",已经保存元素的个数 length:" << L.length << endl; for (int i = 0; i < L.length - 1; i++) { cout << L.elems[i] << " "; cout << endl; } } int main() { SqList list; cout << "顺序表的初始化...." << endl; //1.初始化 if (initList(list)) { cout << "顺序表初始化成功了!" << endl; } listPrint(list); system("pause"); return 0; }