一、SeqList.h
#ifndef __SeqList_h__
#define __SeqList_h__
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <Windows.h>
#define N 5
typedef int DataType;
typedef struct SeqList
{
DataType a[N];
size_t size;
}SeqList;
void SeqList_Init(SeqList *pSeq);
void SeqList_Print(SeqList *pSeq);
void SeqList_Insert(SeqList *pSeq, size_t pos, DataType x);
void SeqList_Delete(SeqList *pSeq, size_t pos);
void SeqList_Remove(SeqList *pSeq, DataType x);
void SeqList_RemoveAll(SeqList *pSeq, DataType x);
void SeqList_Search(SeqList *pSeq, DataType x);
void SeqList_Modify(SeqList *pSeq, size_t pos, DataType x);
void Bubble_Sort(SeqList *pSeq);
void Select_Sort(SeqList *pSeq);
void Binary_Search(SeqList *pSeq, DataType x);
#endif
二、SeqList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
void SeqList_Init(SeqList *pSeq)
{
assert(pSeq);
memset(pSeq, 0, N*sizeof(DataType));
pSeq->size = 0;
printf("初始化成功\n");
}
void Swap(DataType *a, DataType *b)
{
DataType tmp = *a;
*a = *b;
*b = tmp;
}
void SeqList_Insert(SeqList *pSeq, size_t pos, DataType x)
{
assert(pSeq);
if (pSeq->size == N)
{
printf("该顺序表已满\n");
}
else
{
assert(pos <= pSeq->size);
for (int i = pSeq->size; i > (int)pos; i--)
{
pSeq->a[i] = pSeq->a[i - 1];
}
pSeq->a[pos] = x;
pSeq->size++;
printf("插入成功\n");
}
}
void SeqList_Delete(SeqList *pSeq, size_t pos)
{
assert(pSeq);
if (pSeq->size == 0)
{
printf("该顺序表为空\n");
}
else
{
assert(pos < pSeq->size);
for (int i = pos + 1; i < (int)pSeq->size; i++)
{
pSeq->a[i - 1] = pSeq->a[i];
}
pSeq->size--;
printf("删除成功\n");
}
}
void SeqList_Remove(SeqList *pSeq, DataType x)
{
assert(pSeq);
if (pSeq->size == 0)
{
printf("该顺序表为空\n");
return;
}
int i = 0;
int j = 0;
for (i = 0; i < (int)pSeq->size; i++)
{
if (pSeq->a[i] == x)
{
for (j = i + 1; j < (int)pSeq->size; j++)
{
pSeq->a[j - 1] = pSeq->a[j];
}
pSeq->size--;
printf("删除成功\n");
return;
}
}
if (i == (int)pSeq->size)
{
printf("该顺序表没有此值\n");
}
}
void SeqList_RemoveAll(SeqList *pSeq, DataType x)
{
assert(pSeq);
if (pSeq->size == 0)
{
printf("该顺序表为空\n");
return;
}
int p = 0;
int q = 0;
int count = 0;
for (p = 0; p < (int)pSeq->size;)
{
if (pSeq->a[p] == x)
{
p++;
count++;
}
else
{
pSeq->a[q++] = pSeq->a[p++];
}
}
pSeq->size -= count;
printf("删除成功\n");
}
void SeqList_Search(SeqList *pSeq, DataType x)
{
assert(pSeq);
if (pSeq->size == 0)
{
printf("该顺序表为空\n");
return;
}
for (int i = 0; i < (int)pSeq->size; i++)
{
if (pSeq->a[i] == x)
{
printf("找到了,下标为%d\n", i);
return;
}
}
printf("未找到\n");
}
void SeqList_Modify(SeqList *pSeq, size_t pos, DataType x)
{
assert(pSeq);
if (pSeq->size == 0)
{
printf("该顺序表为空\n");
return;
}
assert(pos < pSeq->size);
pSeq->a[pos] = x;
printf("修改成功\n");
}
void SeqList_Print(SeqList *pSeq)
{
assert(pSeq);
if (pSeq->size == 0)
{
printf("该顺序表为空\n");
return;
}
for (int i = 0; i < (int)pSeq->size; i++)
{
printf("%d ", pSeq->a[i]);
}
printf("\n");
}
void Bubble_Sort(SeqList *pSeq)
{
assert(pSeq);
if (pSeq->size == 0)
{
printf("该顺序表为空\n");
return;
}
for (int i = 0; i < (int)pSeq->size - 1; i++)
{
int j = 0;
int flag = 0;
for (; j < (int)pSeq->size - i - 1; j++)
{
if (pSeq->a[j] > pSeq->a[j + 1])
{
flag = 1;
Swap(&pSeq->a[j], &pSeq->a[j + 1]);
}
}
if (flag == 0)
{
break;
}
}
printf("排序成功\n");
}
void Select_Sort(SeqList *pSeq)
{
assert(pSeq);
if (pSeq->size == 0)
{
printf("该顺序表为空\n");
return;
}
int left = 0;
int right = pSeq->size - 1;
while (left < right)
{
int i = 0;
int min = left;
int max = left;
for (i = left; i <= right; i++)
{
if (pSeq->a[i] < pSeq->a[min])
{
min = i;
}
if (pSeq->a[i] > pSeq->a[max])
{
max = i;
}
}
if (min != left)
{
Swap(&pSeq->a[min], &pSeq->a[left]);
}
if (max == left)
{
max = min;
}
if (max != right)
{
Swap(&pSeq->a[max], &pSeq->a[right]);
}
left++;
right--;
}
printf("排序成功\n");
}
void Binary_Search(SeqList *pSeq, DataType x)
{
assert(pSeq);
if (pSeq->size == 0)
{
printf("该顺序表为空\n");
return;
}
int left = 0;
int right = pSeq->size - 1;
while (left <= right)
{
int mid = left + ((right - left) >> 1);
if (pSeq->a[mid] > x)
{
right = mid - 1;
}
else if (pSeq->a[mid] < x)
{
left = mid + 1;
}
else
{
printf("找到了,下标为%d\n", mid);
return;
}
}
printf("未找到\n");
}
三、test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
void Menu()
{
printf("********静态顺序表********\n");
printf("** 0.退出 **\n");
printf("** 1.初始化 **\n");
printf("** 2.插入 **\n");
printf("** 3.删除 **\n");
printf("** 4.查找 **\n");
printf("** 5.修改 **\n");
printf("** 6.冒泡排序 **\n");
printf("** 7.选择排序 **\n");
printf("** 8.二分查找 **\n");
printf("** 9.输出 **\n");
printf("**************************\n");
}
int main()
{
SeqList pSeq;
int op = 0;
do{
Menu();
printf("请选择:");
scanf("%d", &op);
switch (op)
{
case 0:
break;
case 1:
SeqList_Init(&pSeq);
break;
case 2:
{
DataType x = 0;
size_t pos = 0;
printf("请输入要插入的位置:");
scanf("%d", &pos);
printf("请输入要插入的值:");
scanf("%d", &x);
SeqList_Insert(&pSeq, pos, x);
}
break;
case 3:
{
size_t pos = 0;
printf("请输入要删除的位置:");
scanf("%d", &pos);
SeqList_Delete(&pSeq, pos);
}
break;
case 4:
{
DataType x = 0;
printf("请输入要查找的值:");
scanf("%d", &x);
SeqList_Search(&pSeq, x);
}
break;
case 5:
{
DataType x = 0;
size_t pos = 0;
printf("请输入要修改的位置:");
scanf("%d", &pos);
printf("请输入修改后的值:");
scanf("%d", &x);
SeqList_Modify(&pSeq, pos, x);
}
break;
case 6:
Bubble_Sort(&pSeq);
break;
case 7:
Select_Sort(&pSeq);
break;
case 8:
{
DataType x = 0;
printf("请输入要查找的值:");
scanf("%d", &x);
Binary_Search(&pSeq, x);
}
break;
case 9:
SeqList_Print(&pSeq);
break;
default:
printf("请重新选择...\n");
break;
}
} while (op);
system("pause");
return 0;
}