线性表(静态和动态)
base operand of ‘->’ has non-pointer type 'const Comple
->操作符前面的操作数类型不是指针类型
//静态线性表
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef int elemtype;
typedef struct {
elemtype data[MAXSIZE];
int length; // 表中最大存放元素个数
int size; // 当前表中存放元素个数
} sqlist;
sqlist initlist() {
sqlist *p = (sqlist *)malloc(sizeof(sqlist));//
p->length = MAXSIZE;
p->size = 0;
return *p;
}
bool Eleminsert(sqlist *p, int ipos, elemtype e) {
if (ipos < 0 || ipos > p->length) {
return false;
}
if (p->size >= p->length) {
return false;//线性表已满!
}
for (int i = p->size; i > ipos; i--) {
p->data[i] = p->data[i - 1];
}
p->data[ipos] = e;
p->size++;
return true;
}
bool Elemdele(sqlist *p, int dpos, elemtype e) {
if (p->size <= 0) {
return false;
}
for (int i = dpos + 1; i < p->size; i++) {
p->data[i - 1] = p->data[i];
}
p->size--;
return true;
}
int locateElem(sqlist p, elemtype e) {
for (int i = 0; i < p.length; i++) {
if (p.data[i] == e) {
return i;
}
}
return -1;//查找失败
}
void vistElem(sqlist *p) {
for (int i = 0; i < p->size; i++) {
printf("线性表中第%d个元素是:%d\n", i, p->data[i]);
}
}
int main() {
sqlist m;
m = initlist();
bool res;
for (int i = 0; i <= 99; i++) {
Eleminsert(&m, i, i);
}
vistElem(&m);
int g;
g = locateElem(m, 3);
printf("position==%d", g);
/*printf("==============\n");
for (int i = 1; i <= 99; i++) {
Elemdele(&m, i, i);
}
vistElem(&m);
printf("==============\n");
*/
return 0;
}
//动态线性表
//区别是初始化函数和struct的定义
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct {
elemtype *data;
int length; // 表中最大存放元素个数
int size; // 当前表中存放元素个数
} sqlist;
sqlist initlist(int MAX_SIZE) {
sqlist *p = (sqlist *)malloc(sizeof(sqlist));//
p->data = (elemtype *)malloc(sizeof(elemtype) * MAX_SIZE);
p->length = MAX_SIZE;
p->size = 0;
return *p;
}
bool Eleminsert(sqlist *p, int ipos, elemtype e) {
if (ipos < 0 || ipos > p->length) {
return false;
}
if (p->size >= p->length) {
return false;//线性表已满!
}
for (int i = p->size; i > ipos; i--) {
p->data[i] = p->data[i - 1];
}
p->data[ipos] = e;
p->size++;
return true;
}
bool Elemdele(sqlist *p, int dpos, elemtype e) {
if (p->size <= 0) {
return false;
}
for (int i = dpos + 1; i < p->size; i++) {
p->data[i - 1] = p->data[i];
}
p->size--;
return true;
}
int locateElem(sqlist p, elemtype e) {
for (int i = 0; i < p.length; i++) {
if (p.data[i] == e) {
return i;
}
}
return -1;//查找失败
}
void vistElem(sqlist *p) {
for (int i = 0; i < p->size; i++) {
printf("线性表中第%d个元素是:%d\n", i, p->data[i]);
}
}
int main() {
sqlist m;
m = initlist(100);
bool res;
for (int i = 0; i <= 99; i++) {
Eleminsert(&m, i, i);
}
vistElem(&m);
int g;
g = locateElem(m, 3);
printf("position==%d", g);
/*printf("==============\n");
for (int i = 1; i <= 99; i++) {
Elemdele(&m, i, i);
}
vistElem(&m);
printf("==============\n");
*/
return 0;
}