程序代码:
SeqList.h文件:
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include <stdio.h>
#include <Windows.h>
#include <assert.h>
#include <stdlib.h>
#define MAX 10
typedef int DataType;
typedef struct SeqList
{
DataType data[MAX];
int sz;
}SeqList, *pSeqList;
void PrintSeqList(pSeqList pSeq);
void InitSeqList(pSeqList pSeq);
void PushBack(pSeqList pSeq, DataType data);
void PopBack(pSeqList pSeq);
void PushFront(pSeqList pSeq, DataType data);
void PopFront(pSeqList pSeq);
int Find(pSeqList pSeq, DataType data);
void Insert(pSeqList pSeq, int pos, DataType data);
void Erase(pSeqList pSeq, int pos);
void Remove(pSeqList pSeq, DataType data);
void RemoveAll(pSeqList pSeq, DataType data);
int Size(pSeqList pSeq);
int Empty(pSeqList pSeq);
void BubbleSort(pSeqList pSeq);
void SelectSort(pSeqList pSeq);
void SelectSortOP(pSeqList pSeq);
int BinarySearch(pSeqList pSeq, DataType data);
int BinarySearch_R(pSeqList pSeq, int left, int right, DataType d);
#endif // __SRQLIST_H__
SeqList.c文件(对所有用到的函数进行定义)
#define _CRT_SRCURE_NO_WARNINGS 1
#include "SeqList.h"
void InitSeqList(pSeqList pSeq)
{
assert(pSeq != NULL);
pSeq->sz = 0;
memset(pSeq->data, 0, sizeof(pSeq->data));
}
void PushBack(pSeqList pSeq, DataType data)
{
assert(pSeq != NULL);
if (pSeq->sz == MAX)
{
printf("顺序表已满,无法插入!\n");
return 0;
}
pSeq->data[pSeq->sz] = data;
pSeq->sz++;
}
void PrintSeqList(const pSeqList pSeq)
{
assert(pSeq != NULL);
int i = 0;
for (i = 0; i < pSeq->sz; i++)
{
printf("%d ", pSeq->data[i]);
}
printf("\n");
}
void PopBack(pSeqList pSeq)
{
assert(pSeq != NULL);
if (pSeq->sz == 0)
{
printf("顺序表为空,无法删除!\n");
return;
}
pSeq->sz--;
}
//头部插入
void PushFront(pSeqList pSeq, DataType data)
{
assert(pSeq != NULL);
int i = 0;
if (pSeq->sz == MAX)
{
printf("顺序表已满,无法插入!\n");
return;
}
for (i = pSeq->sz; i > 0; i
{
pSeq->data[i] = pSeq->data[i - 1];
}
pSeq->data[0] = data;
pSeq->sz++;
}
//头部删除
void PopFront(pSeqList pSeq)
{
assert(pSeq != NULL);
int i = 0;
if (pSeq->sz == 0)
{
printf("顺序表为空,无法删除!\n");
return;
}
for (i = 0; i < pSeq->sz - 1; i++)
{
pSeq->data[i] = pSeq->data[i + 1];
}
pSeq->sz
}
int Find(pSeqList pSeq, DataType data)
{
assert(pSeq != NULL);
int i = 0;
if (pSeq->sz == 0)
{
return -1;
}
for (i = 0; i < pSeq->sz; i++)
{
if (pSeq->data[i] == data)
{
return i;
}
}
return -1;
}
//指定位置插入
void Insert(pSeqList pSeq, int pos, DataType data)
{
assert((pSeq != NULL) && pos >= 0 && pos <= pSeq->sz);
int i = 0;
if (pSeq->sz == MAX)
{
printf("顺序表已满,无法插入!\n");
return 0;
}
for (i = pSeq->sz; i > pos; i
{
pSeq->data[i] = pSeq->data[i - 1];
}
pSeq->data[pos] = data;
pSeq->sz++;
}
//删除指定位置元素
void Erase(pSeqList pSeq, int pos)
{
assert((pSeq != NULL) && pos >= 0 && pos <= pSeq->sz);
int i = 0;
if (pSeq->sz == 0)
{
printf("顺序表为空,无法删除!\n");
return;
}
for (i = pos; i < pSeq->sz - 1; i++)
{
pSeq->data[i] = pSeq->data[i + 1];
}
pSeq->sz
}
//删除指定元素
void Remove(pSeqList pSeq, DataType data)
{
assert(pSeq != NULL);
int i = 0;
int j = 0;
if (pSeq->sz == 0)
{
printf("顺序表为空,无法删除!\n");
return;
}
for (i = 0; i < pSeq->sz; i++)
{
if (pSeq->data[i] == data)
{
break;
}
}
if (i == pSeq->sz)
{
printf("未找到该元素,无法删除!\n");
return;
}
for (j = i; j < pSeq->sz - 1; j++)
{
pSeq->data[j] = pSeq->data[j + 1];
}
pSeq->sz
}
//删除所有的指定元素
void RemoveAll(pSeqList pSeq, DataType data)
{
assert(pSeq != NULL);
int i = 0;
int j = 0;
if (pSeq->sz == 0)
{
printf("顺序表为空,无法删除!\n");
return;
}
for (i = 0; i < pSeq->sz; i++)
{
if (pSeq->data[i] != data)
{
pSeq->data[j] = pSeq->data[i];
j++;
}
}
pSeq->sz = j;
}
int Size(pSeqList pSeq)
{
assert(pSeq != NULL);
return pSeq->sz;
}
int Empty(pSeqList pSeq)
{
assert(pSeq != NULL);
return pSeq->sz == 0;
}
//排序用到的数组交换
void Swap(DataType* x, DataType* y)
{
DataType temp = *x;
*x = *y;
*y = temp;
}
//冒泡排序
void BubbleSort(pSeqList pSeq)
{
assert(pSeq != NULL);
int i = 0;
int j = 0;
int flag = 0;
for (i = 0; i < pSeq->sz - 1; i++)
{
flag = 0;
for (j = 0; j < pSeq->sz - 1 - i; j++)
{
if (pSeq->data[j] > pSeq->data[j + 1])
{
Swap(pSeq->data + j, pSeq->data + j + 1);
flag = 1;
}
}
if (flag == 0)
{
return;
}
}
}
//选择排序
void SelectSort(pSeqList pSeq)
{
assert(pSeq != NULL);
int i = 0;
int j = 0;
for (i = 0; i < pSeq->sz - 1; i++)
{
int temp = 0;
for (j = 1; j < pSeq->sz - i; j++)
{
if (pSeq->data[temp] < pSeq->data[j])
{
temp = j;
}
}
if (temp != pSeq->sz - 1 - i)
{
Swap(pSeq->data + temp, pSeq->data + pSeq->sz - 1 - i);
}
}
}
//选择排序的优化
void SelectSortOP(pSeqList pSeq)
{
int i = 0;
int j = 0;
assert(pSeq != NULL);
for (i = 0; i < (pSeq->sz - 1) / 2; i++)
{
int temp = i;
int temp2 = i;
for (j = i; j < pSeq->sz - i; j++)
{
if (pSeq->data[temp] < pSeq->data[j])
{
temp = j;
}
if (pSeq->data[temp2] > pSeq->data[j])
{
temp2 = j;
}
}
if (temp2 != i)
{
Swap(pSeq->data + temp2, pSeq->data + i);
}
if (temp == i)
{
temp = temp2;
}
if (temp != j)
{
Swap(pSeq->data + temp, pSeq->data + pSeq->sz - i - 1);
}
}
}
//二分查找
int BinarySearch(pSeqList pSeq, DataType data)
{
assert(pSeq != NULL);
int left = 0;
int right = pSeq->sz;
int mid = 0;
while (left <= right)
{
mid = left + (right - left) / 2;
if (pSeq->data[mid] < data)
{
left = mid + 1;
}
else if (pSeq->data[mid] > data)
{
right = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
//二分查找递归写法
int BinarySearch_R(pSeqList pSeq, int left, int right, DataType data)
{
assert(pSeq != NULL);
int mid = left + (right - left) / 2;
if (left > right)
{
return -1;
}
if (pSeq->data[mid] < data)
{
return BinarySearch_R(pSeq, mid + 1, right, data);
}
else if (pSeq->data[mid] > data)
{
return BinarySearch_R(pSeq, left, mid - 1, data);
}
else
{
return mid;
}
}
test.c文件:(测试文件,检测每个函数是否能达到想要的要求)
#define _CRT_SRCURE_NO_WARNINGS 1
#include "SeqList.h"
void TestPushBack()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PrintSeqList(&seq);
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/e6977827a2795c4bd24ddc0862fe1323.png)
void TestPopBack()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PrintSeqList(&seq);
PopBack(&seq);
PopBack(&seq);
PopBack(&seq);
PrintSeqList(&seq);
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/f10261e07878a68d8800b4c27fd0dbd5.png)
void TestPushFront()
{
SeqList seq;
InitSeqList(&seq);
PushFront(&seq, 1);
PushFront(&seq, 2);
PushFront(&seq, 3);
PushFront(&seq, 4);
PrintSeqList(&seq);
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/7e44f9562d20f293b99ce93936cf0825.png)
void TestPopFront()
{
SeqList seq;
InitSeqList(&seq);
PushFront(&seq, 1);
PushFront(&seq, 2);
PushFront(&seq, 3);
PushFront(&seq, 4);
PrintSeqList(&seq);
PopFront(&seq);
PopFront(&seq);
PopFront(&seq);
PrintSeqList(&seq);
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/6566b408f143a90ec339c561ced05d66.png)
void TestFind()
{
int temp = 0;
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PrintSeqList(&seq);
temp = Find(&seq, 3);
if (temp == -1)
{
printf("未找到!\n");
}
else
{
printf("找到了,下标为%d\n", temp);
}
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/0e609c92913e5e241bfa8ba2eb8a1ac3.png)
void TestInsert()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PrintSeqList(&seq);
Insert(&seq, 2, 5);
PrintSeqList(&seq);
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/664c37993a9e2eba1dd53240491c0a0d.png)
void TestErase()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PrintSeqList(&seq);
Erase(&seq, 2);
PrintSeqList(&seq);
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/f0e6f5a9c1464c45820c9fb8a8a38a1d.png)
void TestRemove()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PrintSeqList(&seq);
Remove(&seq, 3);
PrintSeqList(&seq);
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/c73a640d9aa8694a688a0b2ef00c972c.png)
void TestRemoveAll()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 3);
PushBack(&seq, 1);
PushBack(&seq, 3);
PushBack(&seq, 3);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PushBack(&seq, 3);
PrintSeqList(&seq);
RemoveAll(&seq, 3);
PrintSeqList(&seq);
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/e0fd6ab2e252e5e460ad915734f434c4.png)
void TestSize()
{
int temp = 0;
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PrintSeqList(&seq);
temp = Size(&seq);
printf("顺序表的大小为:%d\n", temp);
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/0ee3e9a567b5637105097d1fba9d6ab2.png)
void TestEmpty()
{
int temp = 0;
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PrintSeqList(&seq);
temp = Empty(&seq);
if (temp == 1)
{
printf("顺序表为空!\n");
}
else
{
printf("顺序表不为空\n");
}
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/26781595255ee676c4abfdf6488792ea.png)
void TestBubbleSort()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 6);
PushBack(&seq, 7);
PushBack(&seq, 5);
PushBack(&seq, 3);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 4);
PrintSeqList(&seq);
BubbleSort(&seq);
PrintSeqList(&seq);
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/15b5921c264c771b5b1230fbc70fed87.png)
void TestSelectSort()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 6);
PushBack(&seq, 7);
PushBack(&seq, 5);
PushBack(&seq, 3);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 4);
PrintSeqList(&seq);
SelectSort(&seq);
PrintSeqList(&seq);
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/457dee7caa6bbca0c723396d50915028.png)
void TestSelectSortOP()
{
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 6);
PushBack(&seq, 7);
PushBack(&seq, 5);
PushBack(&seq, 3);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 4);
PrintSeqList(&seq);
SelectSortOP(&seq);
PrintSeqList(&seq);
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/3516e4b76745f7feae0a51499c45eeed.png)
void TestBinarySearch()
{
int temp = 0;
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PushBack(&seq, 5);
PushBack(&seq, 6);
PushBack(&seq, 7);
PushBack(&seq, 8);
PrintSeqList(&seq);
temp = BinarySearch(&seq, 5);
if (temp == -1)
{
printf("未找到该元素!\n");
}
else
{
printf("找到了,下标为:%d\n", temp);
}
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/df59c45d929a55ce9abc537b92ffb0a9.png)
void TestBinarySearch_R()
{
int temp = 0;
SeqList seq;
InitSeqList(&seq);
PushBack(&seq, 1);
PushBack(&seq, 2);
PushBack(&seq, 3);
PushBack(&seq, 4);
PushBack(&seq, 5);
PushBack(&seq, 6);
PushBack(&seq, 7);
PushBack(&seq, 8);
PrintSeqList(&seq);
temp = BinarySearch_R(&seq, 0, seq.sz - 1, 7);
if (temp == -1)
{
printf("未找到该元素!\n");
}
else
{
printf("找到了,下标为:%d\n", temp);
}
}
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/8497fcdd2b678e4c5aa725ad6d8cac19.png)
int main()
{
system("pause");
return 0;
}