顺序表————————————————————————————
是用一段地址连续的存储单元依次存储数据元素的线性结构
Seqlist.h文件
#define MAX_SIZE 10
typedef unsigned int size_t;
typedef int DataType;
typedef struct SeqList
{
DataType _array[MAX_SIZE];
int _size; // 顺序表中有效元素的个数
}SeqList, *PSeqList;
// 初始化顺序表
void SeqListInit(PSeqList ps);
// 尾插
void SeqListPushBack(PSeqList ps, DataType data);
// 尾删
void SeqListPopBack(PSeqList ps);
// 头插
void SeqListPushFront(PSeqList ps, DataType data);
// 头删
void SeqListPopFront(PSeqList ps);
// 任意位置中插入值为data的元素
void SeqListInsert(PSeqList ps, size_t pos, DataType data);
// 删除任意位置中的元素
void SeqListErase(PSeqList ps, size_t pos);
// 在顺序表中查找值为data的元素,返回该元素在顺序表中的下标
int SeqListFind(PSeqList ps, DataType data);
// 删除顺序表中值为data的元素
void SeqListRemove(PSeqList ps, DataType data);
// 删除顺序表中所有值为data的元素
void SeqListRemoveAll(PSeqList ps, DataType data);
// 判断顺序表是否为空
int SeqListEmpty(PSeqList ps);
// 获取顺序表中元素的个数
int SeqListSize(PSeqList ps);
// 打印顺序表中的元素
void PrintSeqList(PSeqList ps);
// 用冒泡排序对顺序表中的元素进行排序
void BubbleSort(int* array, int size);
// 用选择排序对顺序表中的元素进行排序
void SelectSort(int* array, int size);
// 选择排序优化---一次找出最大最小元素所在的位置
void SelectSort_OP(int* array, int size);
Seqlist.c文件
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include"SeqList.h"
void SeqListInit(PSeqList ps)// 初始化顺序表
{
assert(ps);
ps->_size = 0;
memset(ps->_array, 0, sizeof(ps->_array));
}
void SeqListPushBack(PSeqList ps, DataType data)// 尾插
{
assert(ps);
if (ps->_size == MAX_SIZE)
{
return;
}
ps->_array[ps->_size] = data;
ps->_size += 1;
}
void SeqListPopBack(PSeqList ps)// 尾删
{
assert(ps);
if (ps->_size == 0)
{
return;
}
ps->_size--;
}
void SeqListPushFront(PSeqList ps, DataType data)// 头插
{
int i = 0;
assert(ps);
if (ps->_size == MAX_SIZE)
{
return;
}
for (i = ps->_size; i>0; i--)
{
ps->_array[i] = ps->_array[i - 1];
}
ps->_array[0] = data;
ps->_size++;
}
void SeqListPopFront(PSeqList ps)// 头删
{
assert(ps);
if (ps->_size != 0)
{
int i = 0;
for (i = 0; i<ps->_size; i++)
{
ps->_array[i] = ps->_array[i + 1];
}
}
ps->_size--;
}
// 任意位置中插入值为data的元素
void SeqListInsert(PSeqList ps, size_t pos, DataType data)
{
int i = 0;
assert(ps);
if (ps->_size == MAX_SIZE)
{
return;
}
for (i = ps->_size; i > pos; i--)
{
ps->_array[i] = ps->_array[i - 1];
}
ps->_array[pos] = data;
ps->_size++;
}
// 删除任意位置中的元素
void SeqListErase(PSeqList ps, size_t pos)
{
int i = 0;
assert(ps);
if (pos >= ps->_size)
return;
for (i = pos + 1; i < ps->_size; ++i)
{
ps->_array[i - 1] = ps->_array[i];
}
ps->_size--;
}
// 在顺序表中查找值为data的元素,返回该元素在顺序表中的下标
int SeqListFind(PSeqList ps, DataType data)
{
int i = 0;
assert(ps);
for (i = 0; i<ps->_size; i++)
{
if (ps->_array[i] == data)
{
return i;
}
}
return -1;
}
// 删除顺序表中值为data的元素
void SeqListRemove(PSeqList ps, DataType data)
{
int ret = 0;
int i = 0;
assert(ps);
if (ps->_size == 0)
return;
ret = SeqListFind(ps, data); //1.查找
if (ret != -1) //2.删除
{
for (i = ret; i<ps->_size - 1; i++)
{
ps->_array[i] = ps->_array[i + 1];
}
ps->_size--;
}
}
// 删除顺序表中所有值为data的元素
void SeqListRemoveAll(PSeqList ps, DataType data)
{
int i = 0;
int count = 0;
assert(ps);
for (i = 0; i < ps->_size; ++i)
{
if (ps->_array[i] == data)
count++;
else
ps->_array[i - count] = ps->_array[i];
}
ps->_size -= count;
}
int SeqListEmpty(PSeqList ps)// 判断顺序表是否为空
{
/*assert(ps);*/
if (NULL == ps)
return 1;
return 0 == ps->_size;
}
int SeqListSize(PSeqList ps)// 获取顺序表中元素的个数
{
assert(ps);
return ps->_size;
}
void PrintSeqList(PSeqList ps)// 打印顺序表中的元素
{
int i = 0;
assert(ps);
for (i = 0; i<ps->_size; i++)
{
printf("%d ", ps->_array[i]);
}
printf("\n");
}
void Swap(int *pleft, int *pright)//交换
{
int tmp = *pleft;
*pleft = *pright;
*pright = tmp;
}
// 用冒泡排序对顺序表中的元素进行排序
void BubbleSort(int* array, int size)
{
int i = 0;
int j = 0;
for (i = 0; i < size - 1; i++)
{
for (j = 0; j < size - 1 - i; j++)
{
if (array[j] > array[j + 1])
{
DataType tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
}
}
}
// 用选择排序对顺序表中的元素进行排序
void SelectSort(int* array, int size)
{
int i = 0;
int j = 0;
int maxPos = 0;
for (i = 0; i < size - 1; ++i)
{
maxPos = 0;
for (j = 1; j < size - i; ++j)
{
if (array[maxPos] < array[j])
{
maxPos = j;
}
if (maxPos != size - 1 - i)
Swap(&array[maxPos], &array[size - 1 - i]);
}
}
}
// 选择排序优化---一次找出最大最小元素所在的位置
void SelectSort_OP(int* array, int size)
{
int begin = 0;
int end = size - 1;
int maxPos = 0;
int minPos = 0;
maxPos = begin;
minPos = begin;
int j = begin + 1;
for (int i = begin; i <= end; ++i)
{
if (array[maxPos]<array[i])
maxPos = i;
if (array[minPos]>array[i])
minPos = i;
}
if (maxPos != end)
Swap(&array[maxPos], &array[end]);
if (maxPos == end)
maxPos == minPos;
if (minPos != begin)
Swap(&array[minPos], &array[begin]);
}
test.c文件
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include"SeqList.h"
void test()
{
SeqList list;
SeqListInit(&list); //初始化
SeqListPushBack(&list, 1); //尾插
SeqListPushBack(&list, 2);
SeqListPushBack(&list, 3);
SeqListPushBack(&list, 4);
SeqListPushBack(&list, 5);
PrintSeqList(&list);
SeqListPopBack(&list);//尾删
PrintSeqList(&list);
SeqListPushFront(&list, 5);//头插
PrintSeqList(&list);
SeqListPopFront(&list);//头删
PrintSeqList(&list);
int pos = SeqListFind(&list, 3);//找到值为3的元素,返回下标
SeqListInsert(&list, pos, 6);//在3的位置插入6
PrintSeqList(&list);
int del = SeqListFind(&list, 6);
SeqListErase(&list, del);//删除任意位置中的元素
PrintSeqList(&list);
SeqListRemove(&list, 2); //删除值为data的元素
PrintSeqList(&list);
SeqListPushFront(&list, 2);//头插入2
SeqListPushBack(&list, 2);//尾插入2
int pos1 = SeqListFind(&list, 1);
SeqListInsert(&list, pos1, 2);//在1的位置插入2
PrintSeqList(&list);
SeqListRemoveAll(&list, 2);// 删除所有值为data的元素
PrintSeqList(&list);
int ret = SeqListEmpty(&list);// 判断顺序表是否为空
printf("%d\n", ret);//不为空
SeqListPopFront(&list);
PrintSeqList(&list);
SeqListPopFront(&list);
PrintSeqList(&list);
SeqListPopFront(&list);
PrintSeqList(&list);
int ret1 = SeqListEmpty(&list);
printf("%d\n", ret1);//为空
SeqListPushFront(&list, 2);//头插
SeqListPushFront(&list, 1);
SeqListPushFront(&list, 4);
SeqListPushFront(&list, 3);
SeqListPushFront(&list, 5);
PrintSeqList(&list);
int num = SeqListSize(&list);//元素的个数
printf("%d\n", num);
BubbleSort(&list, 5);
PrintSeqList(&list);
SelectSort(&list, 5);
PrintSeqList(&list);
SelectSort_OP(&list, 5);
PrintSeqList(&list);
}
int main()
{
test();
system("pause");
return 0;
}
结果图与测试代码对应看