顺序表的弱点:插入和删除操作时,需要移动大量元素
顺序表的有点:支持随机访问
SeqList.h
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#define SEQLIST_INIT_SIZE 8
#define INC_SIZE 2
typedef int Elemtype;
typedef struct Seqlist
{
Elemtype *base;
int capacity;
int size;
}Seqlist;
void InitSeqlist(Seqlist* plist);
bool Is_Full(const Seqlist* plist);
bool Is_Empty(const Seqlist* plist);
void push_back(Seqlist *plist, Elemtype x);
void push_front(Seqlist *plist, Elemtype x);
void pop_back(Seqlist *plist);
void pop_front(Seqlist* plist);
void insert_pos(Seqlist* plist, const int pos, Elemtype x);
int Find(const Seqlist* plist, const Elemtype key);
int length(const Seqlist* plist);
void show_list(const Seqlist *plist);
void delete_pos(Seqlist* plist,const int pos);
void delete_val(Seqlist* plist, const Elemtype key);
void sort(Seqlist* plist);
void resver(Seqlist* plist);
void clear(Seqlist* plist);
void Destroy(Seqlist* plist);
bool Inc(Seqlist* plist);
void merge(Seqlist *lt, Seqlist *la, Seqlist *lb);
SeqList.cpp
#include "SeqList.h"
void InitSeqlist(Seqlist* plist)
{
assert(plist != NULL);
plist->base = (Elemtype*)malloc(sizeof(Elemtype)*SEQLIST_INIT_SIZE);
assert(plist->base != NULL);
plist->capacity = SEQLIST_INIT_SIZE;
plist->size = 0;
}
bool Is_Full(const Seqlist* plist)
{
assert(plist != NULL);
return plist->size >= plist->capacity;
}
bool Is_Empty(const Seqlist* plist)
{
assert(plist != NULL);
return plist->size == 0;
}
void push_back(Seqlist *plist, const Elemtype x)
{
assert(plist != NULL);
if (Is_Full(plist) && !Inc(plist))
{
return;
}
plist->base[plist->size++] = x;
}
void push_front(Seqlist *plist, const Elemtype x)
{
assert(plist != NULL);
if (Is_Full(plist) && !Inc(plist))
{
return;
}
for (int i = plist->size; i > 0; --i)
{
plist->base[i] = plist->base[i - 1];
}
plist->base[0] = x;
++plist->size;
}
void pop_back(Seqlist *plist)
{
assert(plist != NULL);
if (Is_Empty(plist))
{
printf("顺序表为空\n");
return;
}
--plist->size;
}
void pop_front(Seqlist* plist)
{
assert(plist != NULL);
if (Is_Empty(plist))
{
printf("顺序表为空\n");
return;
}
for (int i = 0; i < plist->size - 1; ++i)
{
plist->base[i] = plist->base[i + 1];
}
--plist->size;
}
void insert_pos(Seqlist* plist, const int pos, const Elemtype x)
{
assert(plist != NULL);
assert(pos>=0 && pos <= plist->size);
if (Is_Full(plist) && !Inc(plist))
{
return;
}
for (int i = plist->size; i > pos; --i)
{
plist->base[i] = plist->base[i - 1];
}
plist->base[pos] = x;
++plist->size;
}
int Find(const Seqlist* plist,const Elemtype key)
{
assert(plist != NULL);
if (Is_Empty(plist))
{
printf("顺序表为空\n");
return -1;
}
for (int i = 0; i < plist->size; ++i)
{
if (plist->base[i] == key)
{
return i;
}
}
return -1;
}
int length(const Seqlist* plist)
{
assert(plist != NULL);
return plist->size;
}
void show_list(const Seqlist *plist)
{
assert(plist != NULL);
for (int i = 0; i < plist->size; ++i)
{
printf("%d ", plist->base[i]);
}
printf("\n");
}
void delete_pos(Seqlist* plist, const int pos)
{
assert(plist != NULL);
assert(pos >= 0 && pos < plist->size);
if (Is_Empty(plist))
{
printf("顺序表为空\n");
return;
}
for (int i = pos; i < plist->size - 1; ++i)
{
plist->base[i] = plist->base[i + 1];
}
--plist->size;
}
void delete_val(Seqlist* plist, const Elemtype key)
{
assert(plist != NULL);
if (Is_Empty(plist))
{
printf("顺序表为空\n");
return;
}
int pos = Find(plist, key);
if (pos == -1)
{
printf("无此元素\n");
return;
}
delete_pos(plist, pos);
}
void sort(Seqlist* plist)
{
assert(plist != NULL);
if (Is_Empty(plist))
{
return;
}
for (int i = 0; i < plist->size - 1; i++)
{
int tag = 1;
for (int j = 0; j < plist->size - 1 - i; j++)
{
if (plist->base[j] > plist->base[j + 1])
{
tag = 0;
Elemtype tmp = plist->base[j];
plist->base[j] = plist->base[j + 1];
plist->base[j + 1] = tmp;
}
}
if (tag)
{
break;
}
}
}
void resver(Seqlist* plist)
{
assert(plist != NULL);
if (Is_Empty(plist) || plist->size == 1)
{
return;
}
int low = 0;
int high = plist->size - 1;
Elemtype tmp = 0;
while (low < high)
{
tmp = plist->base[high];
plist->base[high] = plist->base[low];
plist->base[low] = tmp;
++low;
--high;
}
}
void clear(Seqlist* plist)
{
assert(plist != NULL);
plist->size = 0;
}
void Destroy(Seqlist* plist)
{
free(plist->base);
plist->base = NULL;
plist->capacity = plist->size = 0;
}
bool Inc(Seqlist* plist)
{
assert(plist != NULL);
Elemtype* newbase = (Elemtype*)realloc(plist->base,sizeof(Elemtype)*plist->capacity*INC_SIZE);
if (newbase == NULL)
{
return false;
}
plist->base = newbase;
plist->capacity *= INC_SIZE;
return true;
}
void merge(Seqlist *lt, Seqlist *la, Seqlist *lb)
{
assert(lt != NULL &&la != NULL&&lb != NULL);
lt->size = lt->capacity = la->size + lb->size;
lt->base = (Elemtype*)malloc(sizeof(Elemtype)*lt->capacity);
int ic = 0;
int ia = 0;
int ib = 0;
while (ia < la->size && ib < lb->size)
{
if (la->base[ia] < lb->base[ib])
{
lt->base[ic++] = la->base[ia++];
}
else
{
lt->base[ic++] = lb->base[ib++];
}
}
while (ia < la->size)
{
lt->base[ic++] = la->base[ia++];
}
while (ib < lb->size)
{
lt->base[ic++] = lb->base[ib++];
}
}