静态顺序表
数据类型的定义
#define MAX_SIZE 100
typedef int DataType;
typedef struct SeqList
{
DataType data[MAX_SIZE];
int size;
}SeqList;
顺序表的初始化和销毁
//初始化
void SeqInit(SeqList *pSeq)
{
assert(pSeq != NULL);
memset(pSeq,0,MAX_SIZE*sizeof(DataType));
pSeq->size = 0;
}
//销毁
void SeqDestory(SeqList *pSeq)
{
assert(pSeq != NULL);
memset(pSeq,0,MAX_SIZE*sizeof(DataType));
pSeq->size = 0;
}
打印顺序表
void SeqShow(SeqList *pSeq)
{
int i = 0;
assert(pSeq != NULL);
for(i=0; i<pSeq->size; i++)
{
printf("%d ",pSeq->data[i]);
}
printf("\n");
}
查找
int Find(SeqList *pSeq, DataType num)
{
int i = 0;
assert(pSeq != NULL);
for(i=0; i<pSeq->size; i++)
{
if(pSeq->data[i]==num)
{
return i;
}
}
return -1;
}
二分查找
int BinarySearch(SeqList *pSeq, DataType d)
{
int pos = 0;
int left = 0;
int right = pSeq->size;
while(left<right)
{
int mid = (right-left)/2+left;
if(d<pSeq->data[mid])
{
right = mid - 1;
}
else if(d>pSeq->data[mid])
{
left = mid+1;
}
else
{
pos = mid;
break;
}
}
if(left>right)
return -1;
return pos;
}
增加数据
尾插
void PushBack(SeqList *pSeq, DataType num)
{
assert(pSeq != NULL);
assert(pSeq->size != MAX_SIZE);
pSeq->data[pSeq->size] = num;
pSeq->size++;
}
头插
void PushFront(SeqList *pSeq, DataType num)
{
int i = 0;
int j = 0;
assert(pSeq != NULL);
assert(pSeq->size < MAX_SIZE);
#if 1
//i 含义 数据下标 [size-1,0]
for(i=pSeq->size-1; i>=0; i--)
{
pSeq->data[i+1] = pSeq->data[i];
}
pSeq->data[i+1] = num;
pSeq->size++;
#else
//j 含义 空间下标 [size,0)
for(j=pSeq->size; j>0; j--)
{
pSeq->data[j] = pSeq->data[j-1];
}
pSeq->data[j] = num;
pSeq->size++;
#endif
}
在指定位置插入
void Insert(SeqList *pSeq, DataType num, DataType d)
{
int pos = 0;
int i = 0;
int j = 0;
assert(pSeq != NULL);
assert(pSeq->size < MAX_SIZE);
pos = Find(pSeq,num);
#if 1
//i 含义 数据下标 [size-1,pos]
for(i=pSeq->size-1; i>=pos; i--)
{
pSeq->data[i+1] = pSeq->data[i];
}
pSeq->data[pos+1] = d;
pSeq->size++;
#else
//j 含义 空间下标 [size,pos)
for(j=pSeq->size; j>pos; j--)
{
pSeq->data[j] = pSeq->data[j-1];
}
pSeq->[pos] = d;
pSeq->size++;
#endif
}
删除数据
尾删
void PopBack(SeqList *pSeq)
{
assert(pSeq != NULL);
assert(pSeq->size != 0);
pSeq->size--;
}
头删
void PopFront(SeqList *pSeq)
{
int i = 0;
int j = 0;
assert(pSeq != NULL);
assert(pSeq->size != 0);
#if 1
//i 含义 数据下标 [0,size-2]
for(i=0; i<=pSeq->size-2; i++)
{
pSeq->data[i] = pSeq->data[i+1];
}
pSeq->size--;
#else
//j 含义 空间下标 [1,size)
for(j=1; j<pSeq->size; j++)
{
pSeq->data[j-1] = pSeq->data[j];
}
pSeq->size--;
#endif
}
指定位置删除(下标删除)
void Erase(SeqList *pSeq, int pos)
{
int i = 0;
assert(pSeq != NULL);
assert(pSeq->size != 0);
for(i=pos+1; i<pSeq->size; i++)
{
pSeq->data[i-1] = pSeq->data[i];
}
pSeq->size--;
}
指定元素删除(数据删除)
void Remove(SeqList *pSeq, DataType d)
{
int i = 0;
int pos = 0;
assert(pSeq != NULL);
assert(pSeq->size != 0);
pos = Find(pSeq, d);
if(pos == -1)
{
printf("没有找到该元素\n");
return;
}
else
{
for(i=pos; i<pSeq->size; i++)
{
pSeq->data[i] = pSeq->data[i+1];
}
pSeq->size--;
}
}
删除所有指定元素
void RemoveAll(SeqList *pSeq, DataType d)
{
int i = 0;
int j = 0;
assert(pSeq != NULL);
assert(pSeq->size>0 && pSeq->size<MAX_SIZE);
for(i=0,j=0; i<pSeq->size; i++)
{
if(pSeq->data[i]!=d)
{
pSeq->data[j] = pSeq->data[i];
j++;
}
}
pSeq->size = j;
}
顺序表属性
//查看是否为空属性
int Empty(SeqList *pSeq)
{
assert(pSeq != NULL);
if(pSeq->size == 0)
return 0;
else
return -1;
}
//查看顺序表是否满了
int Full(SeqList *pSeq)
{
assert(pSeq != NULL);
if(pSeq->size == MAX_SIZE)
return 0;
else
return -1;
}
//查看顺序表长度
int Size(SeqList *pSeq)
{
return pSeq->size;
}
排序
冒泡排序
void BubbleSort(SeqList *pSeq)
{
int i = 0;
int j = 0;
assert(pSeq != NULL);
for(i=0; i<pSeq->size-1; i++)
{
for(j=i+1; j<pSeq->size; j++)
{
if(pSeq->data[i]>pSeq->data[j])
{
int tmp = pSeq->data[i];
pSeq->data[i] = pSeq->data[j];
pSeq->data[j] = tmp;
}
}
}
}
选择排序
void SelectSort(SeqList *pSeq)
{
int i = 0;
int minSpace = 0;
int maxSpace = 0;
int max = 0;
int min = 0;
int ret1 = 0;
int ret2 = 0;
assert(pSeq != NULL);
for(i=0; i<=pSeq->size; i++)
{
min = pSeq->data[minSpace];
max = pSeq->data[maxSpace];
for(minSpace = i,maxSpace = i; minSpace<pSeq->size; minSpace++,maxSpace++)
{
if(pSeq->data[minSpace]<min)
{
int tmp = pSeq->data[minSpace];
pSeq->data[minSpace] = min;
min = tmp;
}
if(pSeq->data[maxSpace]>max)
{
int tmp = pSeq->data[maxSpace];
pSeq->data[maxSpace] = max;
max = tmp;
}
}
ret1 = max;
ret2 = min;
pSeq->data[minSpace] = ret2;
pSeq->data[maxSpace] = ret1;
}
}
动态顺序表
数据类型的定义
#define CAP 5
typedef int DataType;
typedef struct SeqList
{
DataType *array;
int size;
int capacity;
}SeqList;
顺序表的初始化和销毁
//初始化
void InitSeqList(SeqList *ps)
{
assert(ps);
ps->capacity = CAP;
ps->size = 0;
ps->array = (DataType *)malloc(sizeof(DataType)*CAP);
assert(ps->array);
}
//销毁
void DestroySeqList(SeqList *ps)
{
assert(ps);
free(ps->array);
ps->capacity = 0;
ps->size = 0;
}
顺序表的打印
void PrintSeqList(const SeqList *ps)
{
int i = 0;
assert(ps);
for(i=0; i<ps->size; i++)
{
printf("%d ",ps->array[i]);
}
printf("\n");
}
增加数据
因为动态顺序表同静态顺序表思路一致,由于考虑到扩容问题,在此列出增加数据中的尾插:
//判断是否需要扩容
void SeqIfExpend(SeqList *ps)
{
int i = 0;
DataType *new_array = NULL;
DataType *result = NULL;
assert(ps);
//不需要扩容
if(ps->size<ps->capacity)
{
return ;
}
//需要扩容
new_array = (DataType *)malloc((ps->capacity+CAP)*sizeof(DataType));
if(new_array != NULL)
{
result = new_array;
for(i=0; i<ps->size; i++)
{
result[i] = ps->array[i];
}
ps->array = result;
ps->capacity = (ps->capacity+CAP)*sizeof(DataType);
}
}
//尾插
void PushBack(SeqList *ps, DataType d)
{
assert(ps);
SeqIfExpend(ps);
ps->array[ps->size] = d;
ps->size++;
}
静态与动态的比较
上述内容均为学习过程总结,如有不足之处,请指正