顺序表
typedef int DataType;
typedef struct SeqList
{
DataType *_a;
size_t _size;
size_t _capacity;
}SeqList;
_size:顺序表的大小
_capacity:顺序表的容量
顺序表的结构已经定义好了,接下来就是写几个操作顺序表的函数:
void SeqPrint(SeqList* pSeq); //打印
void SeqInit(SeqList* pSeq); //初始化
void SeqDestory(SeqList* pSeq); //释放
SeqList* SeqCheckFull(SeqList* pSeq); //判满、扩容
void SeqPushBack(SeqList* pSeq, DataType x); //尾插
void SeqPopBack(SeqList* pSeq); //尾删
void SeqPushFront(SeqList* pSeq, DataType x); //头插
void SeqPopFront(SeqList* pSeq); //头删
void SeqInsert(SeqList* pSeq, size_t pos, DataType x); //指定位置插入
void SeqErase(SeqList* pSeq, size_t pos); //指定位置删除
int SeqFind(SeqList* pSeq, DataType x); //查找指定元素
void SeqAt(SeqList* pSeq, size_t pos, DataType x); //替换指定位置元素
void BubbleSort(SeqList* pSeq); //冒泡排序
void SelectSort(SeqList* pSeq); //选择排序
int BinarySearch(SeqList* pSeq, DataType x; //二分查找
代码实现如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "code.h"
void SeqPrint(SeqList* pSeq)
{
assert(pSeq);
for (size_t i=0; i < pSeq->_size;i++)
{
printf("%d ", pSeq->_a[i]);
}
printf("\n");
}
void SeqInit(SeqList* pSeq)
{
assert(pSeq);
pSeq->_size = 0;
pSeq->_capacity = MAX;
pSeq->_a = (DataType*)malloc(sizeof(DataType)*pSeq->_capacity);
memset(pSeq->_a, 0, sizeof(DataType)*pSeq->_capacity);
}
void SeqDestory(SeqList* pSeq)
{
free(pSeq->_a);
pSeq->_size = 0;
}
///////////////////////////////////////////////////////////
SeqList* SeqCheckFull(SeqList* pSeq)
{
assert(pSeq);
DataType* temp;
if (pSeq->_capacity == pSeq->_size)
{
temp = (DataType*)malloc(sizeof(DataType)*pSeq->_capacity * 2);
if (temp == NULL)
{
printf("扩容失败\n");
return NULL;
}
else
{
pSeq->_a = temp;
pSeq->_capacity *= 2;
}
}
return pSeq;
}
///////////////////////////////////////////////////////////
void SeqPushBack(SeqList* pSeq, DataType x)
{
assert(pSeq);
if (pSeq->_a==NULL)
{
SeqInit(pSeq);
pSeq->_a[0] = x;
pSeq->_size = 1;
}
else
{
pSeq->_a[pSeq->_size] = x;
pSeq->_size++;
}
}
void SeqPopBack(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->_size == 0)
{
printf("THE SeqList is NULL");
return;
}
else
{
pSeq->_size--;
}
}
void SeqPushFront(SeqList* pSeq, DataType x)
{
assert(pSeq);
if (pSeq->_a==NULL)
{
SeqInit(pSeq);
pSeq->_a[0] = x;
pSeq->_size = 1;
}
else if (0 == pSeq->_size)
{
pSeq->_a[0] = x;
}
else
{
int i = pSeq->_size;
while (i)
{
pSeq->_a[i] = pSeq->_a[i-1];
i--;
}
pSeq->_a[0] = x;
}
pSeq->_size++;
}
void SeqPopFront(SeqList* pSeq)
{
assert(pSeq);
if (0 == pSeq->_size)
{
printf("THE SeqList is NULL");
return;
}
else
{
for (size_t i = 0; i < pSeq->_size-1;i++)
{
pSeq->_a[i] = pSeq->_a[i+1];
}
}
pSeq->_size--;
}
/////////////////////////////////////////////////////////
void SeqInsert(SeqList* pSeq, size_t pos, DataType x)
{
assert(pSeq);
assert(pos <= pSeq->_size);
if (pSeq->_a==NULL)
{
SeqInit(pSeq);
pSeq->_a[0] = x;
pSeq->_size = 1;
}
else if (0 == pos)
{
SeqPushFront(pSeq, x);
}
else if (pos == pSeq->_size)
{
SeqPushBack(pSeq, x);
}
else
{
size_t i = pSeq->_size;
while (i > pos)
{
pSeq->_a[i] = pSeq->_a[i - 1];
i--;
}
pSeq->_a[pos] = x;
pSeq->_size++;
}
}
void SeqErase(SeqList* pSeq, size_t pos)
{
assert(pSeq);
assert(pos < pSeq->_size);
if (0 == pos)
{
SeqPopFront(pSeq);
}
else if (pos == pSeq->_size - 1)
{
SeqPopBack(pSeq);
}
else
{
size_t i = pos;
while (i < pSeq->_size-1)
{
pSeq->_a[i] = pSeq->_a[i + 1];
i++;
}
pSeq->_size--;
}
}
/////////////////////////////////////////////////////////
int SeqFind(SeqList* pSeq, DataType x)
{
assert(pSeq);
size_t i = 0;
while (i < pSeq->_size - 1)
{
if (pSeq->_a[i] == x)
{
return i;
}
else
{
i++;
}
}
return -1;
}
void SeqAt(SeqList* pSeq, size_t pos, DataType x)
{
assert(pSeq);
assert(pos < pSeq->_size);
pSeq->_a[pos] = x;
}
//////////////////////////////////////////////////////
void BubbleSort(SeqList* pSeq)
{
assert(pSeq);
size_t i, j;
for (i = 0; i < pSeq->_size; i++)
{
int temp = 0;
for (j = 0; j < pSeq->_size - i-1; j++)
{
DataType x;
if (pSeq->_a[j]>pSeq->_a[j + 1])
{
x = pSeq->_a[j];
pSeq->_a[j] = pSeq->_a[j + 1];
pSeq->_a[j + 1] = x;
temp = 1;
}
}
if (0 == temp)
{
return;
}
}
}
void SelectSort(SeqList* pSeq)
{
assert(pSeq);
size_t min=0;
size_t max=0;
size_t left = 0;
size_t right = pSeq->_size-1;
while (left<right)
{
for (size_t i = left; i <= right; i++)
{
if (pSeq->_a[min]>pSeq->_a[i])
{
min = i;
}
if (pSeq->_a[max] < pSeq->_a[i])
{
max = i;
}
}
DataType x = pSeq->_a[min];
pSeq->_a[min] = pSeq->_a[left];
pSeq->_a[left] = x;
if (max == left)
{
max = min;
}
x = pSeq->_a[max];
pSeq->_a[max] = pSeq->_a[right];
pSeq->_a[right] = x;
left++;
right--;
}
}
int BinarySearch(Seq, DataType x)
{
assert(pSeq);
int left = 0;
int right = pSeq->_size - 1;
int mid = ((right - left) >> 1) + left;
while (left <= right)
{
if (x < pSeq->_a[mid])
{
right = mid - 1;
}
else if (x > pSeq->_a[mid])
{
left = mid + 1;
}
else
{
return mid;
}
}
return -1;
}