实现功能:
1.打印顺序表
2.初始化顺序表
3.销毁顺序表
4.判满+扩容
5.尾部插入
6.尾部删除
7.头部插入
8.头部删除
9.指定位置插入
10.指定位置删除
11.查找指定元素
12.替换指定位置元素
13.冒泡排序
14.选择排序
15.二分查找
(1)SeqList.h(头文件)
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include <stdio.h>
#include <windows.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#define MAX 10
typedef int DataType; //类型重定义
typedef struct SeqList
{
DataType* _a;
size_t _size;
size_t _capicity;
}SeqList;
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); //二分查找
#endif
(2)SeqList.c(功能实现部分)
#include "SeqList.h"
void SeqPrint(SeqList* pSeq) //打印顺序表
{
size_t i = 0;
for (i = 0; i < pSeq->_size; i++)
{
printf("%-2d", pSeq->_a[i]);
}
printf("\n");
}
void SeqInit(SeqList* pSeq) //初始化顺序表
{
pSeq->_size = 0;
pSeq->_capicity = MAX;
pSeq->_a = (DataType*)malloc(sizeof(DataType)*pSeq->_capicity);
memset(pSeq->_a, 0, sizeof(DataType)*pSeq->_capicity);
}
void SeqDestory(SeqList* pSeq) //释放
{
free(pSeq->_a);
pSeq->_size = 0;
}
SeqList* SeqCheckFull(SeqList* pSeq) //判满+扩容
{
if (pSeq->_size == pSeq->_capicity)
{
DataType* tem = (DataType*)realloc(pSeq->_a, sizeof(DataType)*pSeq->_capicity * 2);
if (NULL == tem)
{
printf("扩容失败!\n");
return NULL;
}
pSeq->_capicity *= 2 ;
pSeq->_a = tem;
}
return pSeq;
}
void SeqPushBack(SeqList* pSeq, DataType x) //尾插
{
assert(pSeq != NULL);
SeqInsert(pSeq, pSeq->_size, x);
}
void SeqPopBack(SeqList* pSeq) //尾删
{
assert(pSeq != NULL);
//if (0 == pSeq)
//{
// printf("SeqLisr is Emtpy!\n");
// return;
//}
//else
//{
// --pSeq->_size;
//}
SeqErase(pSeq, pSeq->_size - 1);
}
void SeqPushFront(SeqList* pSeq, DataType x)//头插
{
assert(pSeq != NULL);
SeqInsert(pSeq, 0, x);
}
void SeqPopFront(SeqList* pSeq)//头删
{
assert(pSeq != NULL);
SeqErase(pSeq, 0);
}
void SeqInsert(SeqList* pSeq, size_t pos, DataType x)//随机位置插入
{
assert(pSeq != NULL);
pSeq = SeqCheckFull(pSeq);
if (pSeq != NULL)
{
if (0 == pSeq->_size)
{
pSeq->_a[pSeq->_size] = x;
pSeq->_size++;
}
else
{
int end = pSeq->_size - 1;
while (end >= (int)pos)
{
pSeq->_a[end + 1] = pSeq->_a[end];
end--;
}
pSeq->_a[end + 1] = x;
++pSeq->_size;
}
}
}
void SeqErase(SeqList* pSeq, size_t pos)//随机位置删除
{
assert(pSeq != NULL);
assert(pos < pSeq->_size); //删除的位置必须在[0, pSeq->_size-1]
if (0 == pSeq->_size)
{
printf("SeqList is empty!\n");
return;
}
size_t start = pos;
while (start < pSeq->_size - 1)
{
pSeq->_a[start] = pSeq->_a[start + 1];
start++;
}
--pSeq->_size;
}
int SeqFind(SeqList* pSeq, DataType x) //查找
{
assert(pSeq != NULL);
size_t i = 0;
for (i = 0; i < pSeq->_size; i++)
{
if (x == pSeq->_a[i])
{
return i;
}
}
return -1;
}
void SeqAt(SeqList* pSeq, size_t pos, DataType x)//替换
{
assert(pSeq != NULL);
assert(pos < pSeq->_size);
pSeq->_a[pos] = x;
}
void Swap(DataType* x, DataType* y)
{
DataType tem = *x;
*x = *y;
*y = tem;
}
void BubbleSort(SeqList* pSeq) //冒泡排序
{
size_t i = 0;
size_t j = 0;
for (i = 0; i < pSeq->_size - 1; i++)
{
int flag = 0;
for (j = 0; j < pSeq->_size - i - 1; j++)
{
if (pSeq->_a[j] > pSeq->_a[j + 1])
{
Swap(&(pSeq->_a[j]), &(pSeq->_a[j + 1]));
flag = 1;
}
}
if (0 == flag)
{
return;
}
}
}
void SelectSort(SeqList* pSeq)//选择排序
{
size_t left = 0;
size_t right = pSeq->_size - 1;
size_t min = left;//存储最小值的下标
size_t max = left;//存储最大值的下标
while (left <= right)
{
min = left;
max = left;
for (size_t i = left; i <= right; ++i)
{
if (pSeq->_a[i] < pSeq->_a[min])
{
min = i;
}
if (pSeq->_a[i] > pSeq->_a[max])
{
max = i;
}
}
Swap(&(pSeq->_a[left]), &(pSeq->_a[min]));
if (left == max)
max = min;
Swap(&(pSeq->_a[right]), &(pSeq->_a[max]));
++left;
--right;
}
}
int BinarySearch(SeqList* pSeq, DataType x)//二分查找
{
int left = 0;
int right = pSeq->_size - 1;
int mid = 0;
while (left <= right)
{
mid = left + ((right - left) >> 1);
if (x > pSeq->_a[mid])
{
left = mid + 1;
}
else if (x < pSeq->_a[mid])
{
right = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
(3) test.c(测试用例)
#include "SeqList.h"
SeqList list;
void testSeqPushFront() //测试头插
{
SeqInit(&list);
SeqPushFront(&list, 0);
SeqPushFront(&list, 1);
SeqPushFront(&list, 2);
SeqPushFront(&list, 3);
SeqPushFront(&list, 4);
SeqPrint(&list);
}
void testSeqPopFront() //测试头删
{
SeqInit(&list);
SeqPushFront(&list, 0);
SeqPushFront(&list, 1);
SeqPushFront(&list, 2);
SeqPushFront(&list, 3);
SeqPrint(&list);
SeqPopFront(&list);
SeqPrint(&list);
SeqPopFront(&list);
SeqPrint(&list);
SeqPopFront(&list);
SeqPrint(&list);
SeqPopFront(&list);
SeqPrint(&list);
SeqPopFront(&list);
SeqPrint(&list);
}
void testSeqPushBack()//测试尾插
{
SeqInit(&list);
SeqPushBack(&list, 0);
SeqPushBack(&list, 1);
SeqPushBack(&list, 2);
SeqPushBack(&list, 3);
SeqPushBack(&list, 4);
SeqPrint(&list);
}
void testSeqPopBack()//测试尾删
{
SeqInit(&list);
SeqPushBack(&list, 0);
SeqPushBack(&list, 1);
SeqPushBack(&list, 2);
SeqPushBack(&list, 3);
SeqPrint(&list);
SeqPopBack(&list);
SeqPrint(&list);
SeqPopBack(&list);
SeqPrint(&list);
SeqPopBack(&list);
SeqPrint(&list);
SeqPopBack(&list);
SeqPrint(&list);
SeqPopBack(&list);
SeqPrint(&list);
}
void testSeqInsert()//测试随机插
{
SeqInit(&list);
SeqPushBack(&list, 0);
SeqPushBack(&list, 1);
SeqPushBack(&list, 2);
SeqPrint(&list);
SeqInsert(&list, 0, -1);
SeqPrint(&list);
SeqInsert(&list, 3, -1);
SeqPrint(&list);
}
void testSeqErase()//测试随机删
{
SeqInit(&list);
SeqPushBack(&list, 0);
SeqPushBack(&list, 1);
SeqPushBack(&list, 2);
SeqPushBack(&list, 3);
SeqPushBack(&list, 4);
SeqPrint(&list);
SeqErase(&list, 0);
SeqPrint(&list);
SeqErase(&list, 0);
SeqPrint(&list);
SeqErase(&list, 3);
SeqPrint(&list);
}
void testSeqFind()//查找
{
SeqInit(&list);
SeqPushBack(&list, 0);
SeqPushBack(&list, 1);
SeqPushBack(&list, 2);
SeqPushBack(&list, 3);
SeqPushBack(&list, 4);
SeqPrint(&list);
printf("%d\n", SeqFind(&list, 0));
printf("%d\n", SeqFind(&list, 1));
printf("%d\n", SeqFind(&list, 2));
printf("%d\n", SeqFind(&list, 3));
printf("%d\n", SeqFind(&list, 4));
printf("%d\n", SeqFind(&list, 5));
}
void testSeqAt() //替换
{
SeqInit(&list);
SeqPushBack(&list, 0);
SeqPushBack(&list, 1);
SeqPushBack(&list, 5);
SeqPushBack(&list, 3);
SeqPushBack(&list, 4);
SeqPrint(&list);
SeqAt(&list, 2, 2);
SeqPrint(&list);
SeqAt(&list, 0, -1);
SeqPrint(&list);
}
void testBubbleSort()//冒泡排序
{
SeqInit(&list);
SeqPushBack(&list, 8);
SeqPushBack(&list, 1);
SeqPushBack(&list, 5);
SeqPushBack(&list, 0);
SeqPushBack(&list, 4);
SeqPrint(&list);
BubbleSort(&list);
SeqPrint(&list);
}
void testSelectSort() //选择排序
{
SeqInit(&list);
SeqPushBack(&list, 8);
SeqPushBack(&list, 1);
SeqPushBack(&list, 5);
SeqPushBack(&list, 0);
SeqPushBack(&list, 4);
SeqPrint(&list);
SelectSort(&list);
SeqPrint(&list);
}
void testBinarySearch()//二分查找
{
SeqInit(&list);
SeqPushBack(&list, 1);
SeqPushBack(&list, 2);
SeqPushBack(&list, 3);
SeqPushBack(&list, 4);
SeqPushBack(&list, 5);
SeqPrint(&list);
int ret = BinarySearch(&list, 3);
if (-1 == ret)
{
printf("没找到\n");
}
else
{
printf("下标是%d\n", ret);
}
}
int main()
{
testSeqPushFront();//测试头插
//testSeqPopFront(); //测试头删
//testSeqPushBack(); //测试尾插
//testSeqPopBack(); //测试尾删
//testSeqInsert(); //测试随机插
//testSeqErase(); //测试随机删
//testSeqFind(); //查找
//testSeqAt(); //替换
//testBubbleSort(); //冒泡排序
//testSelectSort(); //选择排序
//testBinarySearch();//二分查找
system("pause");
return 0;
}