#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define maxsize 100
#define init_size 10
typedef struct {
int a;
float b;
} elemType;
typedef struct {
elemType *elem;
int length;
int listsize;
}SqList;
int InitList(SqList *L); //线性表L的初始化 算法2.3
void DestroyList(SqList *L); //销毁线性表
void ClearList (SqList *L); //清空线性表
int lenList (SqList L); //求线性表的长度
int JudgeList (SqList L); //判断线性表是否为空
bool ListInsert(SqList &L, int i, ElemType e); //在第i 个位置上插入元素e 算法2.4
bool ListDelete (SqList &L, int i, ElemType &e); //将线性表的第i个元素删除 算法2.5
int LocateElem (SqList &L, ElemType e) ; //按值查找
int main (void) {}
//线性表L的初始化 算法2.3
int InitList(SqList *L) {
L->elem = (elemType*)malloc(init_size*sizeof(elemType));
if (!L->elem) {
exit(-2);
}
L->length = 0;
L->listsize = init_size;
return 1;
}
//销毁线性表
void DestroyList(SqList *L) {
if(L->elem) {
free (L->elem);
}
}
//清空线性表
void ClearList (SqList *L) {
L->length = 0;
}
//求线性表的长度
int lenList (SqList L) {
return (L.length);
}
//判断线性表是否为空
int JudgeList(SqList L) {
int ret = 0;
if (!L.length) {
ret = 1;
}
return ret;
}
//在第i 个位置上插入元素e 算法2.4
bool ListInsert(SqList &L, int i, ElemType e) {
if (i < 1 || i > L.length = 1) {
return false;
}
if(L.length >= MaxSize) {
return false;
}
for (int j = L.length; j >= i; j--) {
L.data[j] = L.data[j-1];
}
L.data[i-1] = e;
L.length++;
return true;
}
//将线性表的第i个元素删除 算法2.5
bool ListDelete (SqList &L, int i, ElemType &e) {
if (i < 1 || i > L.length = 1) {
return false;
}
e = L.data[i-1];
for (int j = i; j < L.length; j++) {
L.data[j-1] = L.data[j];
}
L.length--;
return true;
}
//按值查找
int LocateElem (SqList &L, ElemType e) {
int i;
for (i = 0; i < L.length; i++) {
if (L.data[i] == e) {
return i + 1;
}
}
return 0;
}
线性表---算法
最新推荐文章于 2022-10-22 11:25:09 发布