3.18数据结构——创建顺序表
运行结果截图:
#define _CRT_SECURE_NO_WARNINGS
#define MAXSIZE 100
#include<stdio.h>
#include<string.h>
#include<iostream>
typedef int ElemType;
typedef struct //定义结构
{
ElemType* element;
int length;
int listsize;
} SqList;
//初始化顺序表
int initList(SqList& L)
{
L.element = new ElemType[MAXSIZE];
if (!L.element)
return 0;
//内存分配失败,存储空间不够
L.length = 0;
return 1;
}
SqList createList_sq() //创建顺序表
{
SqList* list = (SqList*)malloc(sizeof(SqList));
return *list;
}
SqList L;
int Find(SqList& L)//赋值 并查找
{
//赋值
int i;
for (i = 1; i <= 10; i++)
{
L.element[i] = i;
}
return L.element[3];
}
int main()
{
SqList L;
initList(L);
createList_sq();
int n;
n = Find(L);
printf("该元素为:%d\n", n);
return 0;
}