一、SeqList.h
#ifndef __SeqList_h__
#define __SeqList_h__
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <windows.h>
typedef int DataType;
typedef struct SeqList
{
DataType* _a;
size_t _size;
size_t _capacity;
}SeqList;
void SeqInit(SeqList* pSeq);
void CheckCapacity(SeqList* pSeq);
void SeqPrint(SeqList* pSeq);
void SeqDestroy(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 SeqModify(SeqList* pSeq, size_t pos, DataType x);
void BubbleSort(SeqList* pSeq);
void SelectSort(SeqList* pSeq);
int BinarySearch(SeqList* pSeq, DataType x);
#endif
二、SeqList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
void SeqInit(SeqList* pSeq)
{
assert(pSeq);
pSeq->_size = 0;
pSeq->_capacity = 5;
pSeq->_a = (DataType *)malloc(pSeq->_capacity * sizeof(DataType));
if (pSeq->_a == NULL)
{
perror("SeqInit");
return;
}
memset(pSeq->_a, 0, pSeq->_capacity * sizeof(DataType));
printf("初始化成功\n");
}
void CheckCapacity(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->_size >= pSeq->_capacity)
{
DataType* ptr = (DataType *)realloc(pSeq->_a, (pSeq->_capacity + 2) * sizeof(DataType));
if (ptr == NULL)
{
perror("CheckCapacity");
}
else
{
pSeq->_a = ptr;
pSeq->_capacity += 2;
}
}
}
void SeqPrint(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->_size == 0)
{
printf("该顺序表为空\n");
}
else
{
for (int i = 0; i < (int)pSeq->_size; i++)
{
printf("%d ", pSeq->_a[i]);
}
printf("\n");
}
}
void SeqDestroy(SeqList* pSeq)
{
assert(pSeq);
free(pSeq->_a);
pSeq->_a = NULL;
pSeq->_capacity = 0;
pSeq->_size = 0;
printf("销毁成功\n");
}
void SeqPushBack(SeqList* pSeq, DataType x)
{
assert(pSeq);
CheckCapacity(pSeq);
pSeq->_a[pSeq->_size] = x;
pSeq->_size++;
}
void SeqPushFront(SeqList* pSeq, DataType x)
{
assert(pSeq);
CheckCapacity(pSeq);
for (int i = pSeq->_size; i > 0; i--)
{
pSeq->_a[i] = pSeq->_a[i - 1];
}
pSeq->_a[0] = x;
pSeq->_size++;
}
void SeqPopBack(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->_size == 0)
{
printf("该顺序表为空\n");
}
else
{
pSeq->_size--;
printf("删除成功\n");
}
}
void SeqPopFront(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->_size == 0)
{
printf("该顺序表为空\n");
}
else
{
for (int i = 1; i < (int)pSeq->_size; i++)
{
pSeq->_a[i - 1] = pSeq->_a[i];
}
pSeq->_size--;
printf("删除成功\n");
}
}
void SeqInsert(SeqList* pSeq, size_t pos, DataType x)
{
assert(pSeq);
assert(pos <= pSeq->_size);
CheckCapacity(pSeq);
for (int i = (int)pSeq->_size; i > (int)pos; i--)
{
pSeq->_a[i] = pSeq->_a[i - 1];
}
pSeq->_a[pos] = x;
pSeq->_size++;
}
void SeqErase(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");
}
}
int SeqFind(SeqList* pSeq, DataType x)
{
assert(pSeq);
if (pSeq->_size == 0)
{
printf("该顺序表为空\n");
return -1;
}
else
{
for (int i = 0; i < (int)pSeq->_size; i++)
{
if (pSeq->_a[i] == x)
{
return i;
}
}
return -1;
}
}
void SeqModify(SeqList* pSeq, size_t pos, DataType x)
{
assert(pSeq);
if (pSeq->_size == 0)
{
printf("该顺序表为空\n");
}
else
{
assert(pos < pSeq->_size);
pSeq->_a[pos] = x;
printf("修改成功\n");
}
}
void Swap(DataType *a, DataType *b)
{
DataType tmp = *a;
*a = *b;
*b = tmp;
}
void BubbleSort(SeqList* pSeq)
{
assert(pSeq);
if (pSeq->_size == 0)
{
printf("该顺序表为空\n");
return;
}
for (int i = 0; i < (int)pSeq->_size - 1; i++)
{
int flag = 0;
for (int j = 0; 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 SelectSort(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");
}
int BinarySearch(SeqList* pSeq, DataType x)
{
assert(pSeq);
if (pSeq->_size == 0)
{
printf("该顺序表为空\n");
return -1;
}
int left = 0;
int right = pSeq->_size - 1;
while (left <= right)
{
int mid = left + ((right - left) >> 1);
if (pSeq->_a[mid] < x)
{
left = mid + 1;
}
else if (pSeq->_a[mid] > x)
{
right = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
三、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("** 10.冒泡排序 **\n");
printf("** 11.选择排序 **\n");
printf("** 12.二分查找 **\n");
printf("** 13.输出 **\n");
printf("** 14.销毁 **\n");
printf("**************************\n");
}
int main()
{
SeqList pSeq;
int op = 0;
do{
Menu();
printf("请选择:");
scanf("%d", &op);
switch (op)
{
case 0:
break;
case 1:
SeqInit(&pSeq);
break;
case 2:
{
DataType x = 0;
printf("请输入要插入的值:");
scanf("%d", &x);
SeqPushFront(&pSeq, x);
printf("插入成功\n");
}
break;
case 3:
{
DataType x = 0;
printf("请输入要插入的值:");
scanf("%d", &x);
SeqPushBack(&pSeq, x);
printf("插入成功\n");
}
break;
case 4:
SeqPopFront(&pSeq);
break;
case 5:
SeqPopBack(&pSeq);
break;
case 6:
{
DataType x = 0;
size_t pos = 0;
printf("请输入要插入的位置:");
scanf("%d", &pos);
printf("请输入要插入的值:");
scanf("%d", &x);
SeqInsert(&pSeq, pos, x);
printf("插入成功\n");
}
break;
case 7:
{
size_t pos = 0;
printf("请输入要删除的位置:");
scanf("%d", &pos);
SeqErase(&pSeq, pos);
}
break;
case 8:
{
DataType x = 0;
printf("请输入要查找的值:");
scanf("%d", &x);
printf("%d\n", SeqFind(&pSeq, x));
}
break;
case 9:
{
DataType x = 0;
size_t pos = 0;
printf("请输入要修改的位置:");
scanf("%d", &pos);
printf("请输入修改后的值:");
scanf("%d", &x);
SeqModify(&pSeq, pos, x);
}
break;
case 10:
BubbleSort(&pSeq);
break;
case 11:
SelectSort(&pSeq);
break;
case 12:
{
DataType x = 0;
printf("请输入要查找的值:");
scanf("%d", &x);
printf("%d\n", BinarySearch(&pSeq, x));
}
break;
case 13:
SeqPrint(&pSeq);
break;
case 14:
SeqDestroy(&pSeq);
break;
default:
printf("请重新选择...\n");
break;
}
} while (op);
system("pause");
return 0;
}