静态分配:
#include <stdbool.h>
#include <stdio.h>typedef int ElementType;#define MaxSize 50
typedef struct {ElementType data[MaxSize];int length;
}SqList;//初始化
//SqList L;
void InitList(SqList L) {L.length = 0;
}//插入
bool ListInsert(SqList L, int i, ElementType 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;
}//删除
bool ListDelete(SqList L, int i, ElementType e) {if (i < 1 || i > L.length)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, ElementType e) {int i;for (i = 0; i < L.length; i++)if (L.data[i] == e)return i + 1;return 0;
}
动态分配:
typedef int ElementType;#define InitSize 100
typedef struct {ElementType *data;int MaxSize, length;
}SeqList;//初始化
void InitList(SeqList L) {L.data = (ElementType *)malloc(InitSize*sizeof(ElementType));L.length = 0;L.MaxSize = InitSize;
}