顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。线性表采用顺序存储的方式存储就称之为顺序表。顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。分为静态顺序表和动态顺序表。
SeqList.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#define MAX_SIZE 10
typedef int DataType;
typedef struct SeqList
{
DataType array[MAX_SIZE];
size_t size;
}SeqList;
void InitSeqList(SeqList* seq);//初始化顺序表
void DestorySeqList(SeqList* seq);//销毁顺序表
//增删改查
void PushBack(SeqList* seq,DataType x);//从尾部插入元素
void Popback(SeqList* seq);//从尾部删除元素
void PushFront(SeqList* seq,DataType x);//从头部插入元素
void PopFront(SeqList* seq);//从头部删除元素
void Insert(SeqList* seq,size_t pos,DataType x);//指定位置插入元素
int find(SeqList* seq, DataType x);// 寻找元素
void Erase(SeqList* seq, size_t pos);// 删除指定位置元素
void remove(SeqList* seq, DataType x);//删除元素X
void removeAll(SeqList* seq, DataType x);//删除顺序表中所有的X
void BubbleSort(SeqList* seq);//冒泡排序
void SelectSort(SeqList* seq);//选择排序
void InsertSort(SeqList* seq);//插入排序
int BinarySearch(SeqList* seq,DataType x);//二分查找
int BinarySearchD(SeqList* seq, size_t begin, size_t end, DataType x);//递归,二分查找
void PrintSeqList(SeqList* seq);//输入顺序表
test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void PrintSeqList(SeqList* seq)
{
assert(seq);
for (size_t i = 0; i <(seq->size); i++)
{
printf("%d ", seq->array[i]);
}
printf("\n");
}
void InitSeqList(SeqList* seq)
{
assert(seq);
memset(seq->array, 0, sizeof(DataType)*(MAX_SIZE));
seq->size = 0;
}
void DestorySeqList(SeqList* seq)
{
assert(seq);
seq->size = 0;
}
void PushBack(SeqList* seq, DataType x)
{
assert(seq);
if (seq->size >= MAX_SIZE)
{
printf("The SeqLIst is full\n");
return;
}
seq->array[seq->size] = x;
seq->size++;
}
void Popback(SeqList* seq)
{
assert(seq);
if (seq->size <= 0)
{
printf("The SeqList is empty\n");
return;
}
seq->size--;
}
void PushFront(SeqList* seq, DataType x)
{
assert(seq);
if (seq->size >= MAX_SIZE)
{
printf("The SeqLIst is full\n");
return;
}
int end = seq->size;
while (end >= 0)
{
seq->array[end] = seq->array[end-1];
end--;
}
seq->size++;
seq->array[0] = x;
}
void PopFront(SeqList* seq)
{
assert(seq);
if (seq->size <= 0)
{
printf("The SeqList is empty\n");
return;
}
for (size_t i = 0; i < seq->size - 1; i++)
{
seq->array[i] = seq->array[i + 1];
}
seq->size--;
}
void Insert(SeqList* seq, size_t pos, DataType x)
{
assert(seq);
assert(pos > 0);
assert(pos <= seq->size);
if (seq->size >= MAX_SIZE)
{
printf("The SeqLIst is full\n");
return;
}
int end = seq->size;
while (end >= pos)
{
seq->array[end] = seq->array[end - 1];
end--;
}
seq->size++;
seq->array[pos-1] = x;
}
int find(SeqList* seq, DataType x)
{
assert(seq);
for (size_t i = 0; i < seq->size; i++)
{
if (seq->array[i] == x)
{
return i+1;
}
}
return -1;
}
void Erase(SeqList* seq, size_t pos)
{
assert(seq);
assert(pos > 0);
assert(pos <= seq->size);
if (seq->size <= 0)
{
printf("The SeqList is empty\n");
return;
}
for (size_t i = pos-1; i < seq->size - 1; i++)
{
seq->array[i] = seq->array[i + 1];
}
seq->size--;
}
void remove(SeqList* seq, DataType x)
{
assert(seq);
if (seq->size <= 0)
{
printf("The SeqList is empty\n");
return;
}
size_t i = find(seq, x);
if (i != -1)
{
Erase(seq, i);
}
else
printf("The SeqList do not have %d\n", x);
}
void removeAll(SeqList* seq, DataType x)
{
assert(seq);
if (seq->size <= 0)
{
printf("The SeqList is empty\n");
return;
}
int i = 0;
int j = 0;
int count = 0;
for (i = 0; j < seq->size; i++)
{
if (seq->array[i] == x)
{
count++;
}
else
{
seq->array[j] = seq->array[i];
j++;
}
}
seq->size -= count;
}
void swap(int *a, int *b)
{
int c;
c = *a;
*a = *b;
*b = c;
}
void BubbleSort(SeqList* seq)
{
assert(seq);
if (seq->size <= 0)
{
printf("The SeqList is empty\n");
return;
}
for (size_t i = 0; i < seq->size; i++)
{
for (size_t j = i+1; j < seq->size; j++)
{
if (seq->array[i]>seq->array[j])
swap(&(seq->array[i]), &(seq->array[j]));
}
}
}
void SelectSort(SeqList* seq)
{
assert(seq);
if (seq->size <= 0)
{
printf("The SeqList is empty\n");
return;
}
int begin = 0;
int end = seq->size-1;
while (begin <= end)
{
int MinIndex = begin;
int MaxIndex = end;
for (int i = begin; i <= end; i++)
{
if (seq->array[i] < seq->array[MinIndex])
MinIndex = i;
if (seq->array[i] > seq->array[MaxIndex])
MaxIndex = i;
}
swap(&(seq->array[begin]), &(seq->array[MinIndex]));
if (MaxIndex == begin)
{
MaxIndex = MinIndex;
}
swap(&(seq->array[end]), &(seq->array[MaxIndex]));
begin++;
end--;
}
}
void InsertSort(SeqList* seq)
{
assert(seq);
if (seq->size <= 0)
{
printf("The SeqList is empty\n");
return;
}
for (int i = 0; i < seq->size - 1; i++)
{
int end = i;
int tmp = seq->array[end + 1];
while (end>=0 && seq->array[end] > tmp)
{
seq->array[end + 1] = seq->array[end];
end--;
}
seq->array[end + 1] = tmp;
}
}
int BinarySearch(SeqList* seq, DataType x)
{
assert(seq);
if (seq->size <= 0)
{
printf("The SeqList is empty\n");
return -1;
}
int begin = 0;
int end = seq->size - 1;
while (begin <= end)
{
int mid = begin + (end - begin) / 2;
if (seq->array[mid] < x)
{
begin = mid + 1;
}
else if (seq->array[mid] > x)
{
end = mid - 1;
}
else
return mid + 1;
}
return -1;
}
int BinarySearchD(SeqList* seq, size_t begin, size_t end, DataType x) // 递归
{//1 2 3 4 5 6
assert(seq);
if (seq->size <= 0)
{
printf("The SeqList is empty\n");
return -1;
}
size_t mid = begin + (end - begin) / 2;
if (seq->array[mid] == x)
{
return mid + 1;
}
else if(x < seq->array[mid])
{
return BinarySearchD(seq, begin, mid - 1, x);
}
else if (x > seq->array[mid])
{
return BinarySearchD(seq, mid + 1, end, x);
}
else
return -1;
}