定义动态的顺序表
typedef int SLDataType;
typedef struct Seqlist {
SLDataType* array;
size_t size;
size_t capacity;
}Seqlist;
在顺序表中插入数据
bool ListInsert(Seqlist* l, int i, SLDataType e) {
if (i < 1 || i > l->capacity)
return false;
int j;
for (j = l->capacity; j >= i; j--) {
l->array[j] = l->array[j - 1];
}
l->array[i - 1] = e;
l->size++;
return true;
}
在顺序表中删除数据?
bool ListInsert(Seqlist* l, int i, SLDataType*e) {
if (i<1 || i>l->capacity) {
return false;
}
e = l->array[i - 1];
int j;
for (j = i; j < l->size; j++) {
l->array[j - 1] = l->array[j];
}
l->size--;
return true;
}
按值查找操作
int LocateElem(Seqlist l, SLDataType e) {
int i = 0;
for (i = 0; i < l.size; i++) {
if (l.array[i] == e)
return i + 1;
}
return 0;
}
按位查找操作
int GetElem(Seqlist l, int i) {
if (i<1 || i>l.capacity)
return 0;
return l.array[i - 1];
}
合并两个顺序表
#define MaxSize 20
bool Merge(Seqlist A, Seqlist B, Seqlist* C) {
if (A.size + B.size > C->capacity)
return false;
int i = 0, j = 0, k = 0;
while (i < A.size && j < B.size) {
if (A.array[i] < B.array[j]) {
C->array[k++] = A.array[i++];
}
else {
C->array[k++] = B.array[j++];
}
}
while (i < A.size) {
C->array[k++] = A.array[i++];
}
while (j < B.size) {
C->array[k++] = B.array[j++];
}
C->size = k;
return true;
}
在链表中按序号查找结点值
typedef struct LNode {
SLDataType data;
struct LNode* next;
}LNode,*LinkList;
LNode* GetElem(LinkList L, int i) {
int j = 1;
LNode* p = L->next;
if (i < 0)
return NULL;
if (i == 0)
return L;
while (p && j < i) {
p = p->next;
j++;
}
return p;
}