对于静态顺序表的增删改查,下面是头文件,其中声明了用到的函数
#pragma once
#define N 20
typedef int DataType;
typedef struct SeqList
{
DataType data[N];
int size;
}SeqList;
// 初始化
void SeqListInit(SeqList *pSeq);
// 尾部插入
void SeqListPushBack(SeqList *pSeq, DataType data);
// 头部插入
void SeqListPushFront(SeqList *pSeq, DataType data);
// 尾部删除
void SeqListPopBack(SeqList *pSeq);
// 头部删除
void SeqListPopFront(SeqList *pSeq);
//打印顺序表
void SeqListprint(SeqList *pSeq);
// 按下标插入,pos 的范围是 [0, size]
void SeqListInsert(SeqList *pSeq, int pos, DataType data);
// 按下标删除,pos 的范围是 [0, size)
void SeqListErase(SeqList *pSeq, int pos);
// 按值删除,只删遇到的第一个
void SeqListRemove(SeqList *pSeq, DataType data);
// 按值删除,删除所有的
void SeqListRemoveAll(SeqList *pSeq, DataType data);
// 清空
void SeqListClear(SeqList *pSeq);
// 按值查找,返回第一个找到的下标,如果没找到,返回 -1
int SeqListFind(SeqList *pSeq, DataType data);
// 判断是否为空,1 表示空, 0 表示不空
void SeqListEmpty(SeqList *pSeq);
//冒泡排序
void BubbleSort(SeqList *pSeq);
//选择排序
void SelectSort(SeqList *pSeq);
// 返回数量
int SeqListSize(SeqList *pSeq);
下面是各个函数的实现:
1.顺序表的初始化:
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include
#include "seqlist.h"
void SeqListInit(SeqList *pSeq)
{
memset(pSeq->data, 0, sizeof(DataType)*N);
pSeq->size = 0;
}
2 . 顺序表的头插头删,尾插尾删
void SeqListPushBack(SeqList *pSeq, DataType data)
{
assert(pSeq);
if (pSeq->size == N)
{
printf("SeqList is full\n");
}
else
{
pSeq->data[pSeq->size] = data;
pSeq->size++;
}
}
void SeqListPushFront(SeqList *pSeq, DataType data)
{
assert(pSeq);
int end = pSeq->size - 1;
for (; end >= 0; end--)
{
pSeq->data[end + 1] = pSeq->data[end];
}
pSeq->data[0] = data;
pSeq->size++;
}
void SeqListPopBack(SeqList *pSeq)
{
assert(pSeq);
int i = 0;
if (pSeq->size == 0)
{
printf("SeqList is empty\n");
}
else
{
pSeq->size--;
}
}
void SeqListPopFront(SeqList *pSeq)
{
assert(pSeq);
int i = 0;
if (pSeq->size == 0)
{
printf("SeqList is empty\n");
}
else
{
for (i = 0; i < pSeq->size - 1; i++)
{
pSeq->data[i] = pSeq->data[i + 1];
}
pSeq->size--;
}
}
3 . 任意位置插入,删除
void SeqListInsert(SeqList *pSeq, int pos, DataType data)// 按下标插入,pos 的范围是 [0, size]
{
assert(pSeq);
assert(pos <= pSeq->size);//???????????????????????????????
int end = pSeq->size - 1;
if (pSeq->size == N)
{
printf("SeqList is full\n");
}
else
{
for (; end >= pos; end--)
{
pSeq->data[end + 1] = pSeq->data[end];
}
pSeq->data[pos] = data;
pSeq->size++;
}
}
void SeqListErase(SeqList *pSeq, int pos)
{
assert(pSeq);
assert(pos <= pSeq->size);
int i = pos;
if (pSeq->size == 0)
{
printf("SeqList is empty\n");
}
else
{
for (; i < pSeq->size; i++)
{
pSeq->data[i] = pSeq->data[i + 1];
}
pSeq->size--;
}
}
5 . 按值删除,只删遇到的第一个元素
int SeqListFind(SeqList *pSeq, DataType data)
{
int i = 0;
//printf("%d ", pSeq->size);
for (i = 0; i size; i++)
{
if (pSeq->data[i] == data)
{
return i;
}
}
return -1;
}
void SeqListRemove(SeqList *pSeq, DataType data)
{
assert(pSeq);
if (SeqListFind(pSeq,1) != -1)
{
SeqListErase(pSeq, SeqListFind(pSeq, 1));
}
else
{
printf("SeqList have no data\n");
}
}
5 . 按值查找,删除所有的要删除的值
void SeqListRemoveAll(SeqList *pSeq, DataType data)
{
assert(pSeq);
if (SeqListFind(pSeq, 2) != -1)
{
int i = 0;
int index = 0;
for (index = 0; index < pSeq->size; index++)
{
if (pSeq->data[index] != data)
{
pSeq->data[i] = pSeq->data[index];
i++;
}
}
pSeq->size = i;
}
else
{
printf("SeqList have no data\n");
}
}
6 . 将顺序表元素进行冒泡排序,选择排序
void DataSwap(int *a, int *b)
{
int tmp = 0;
tmp = *a;
*a = *b;
*b = tmp;
}
void BubbleSort(SeqList *pSeq)//元素升序
{
int i = 0;
for (; i < pSeq->size; i++)
{
int j = 0;
for (j = 0; j < pSeq->size - i - 1; j++)
{
if (pSeq->data[j] > pSeq->data[j + 1])
{
DataSwap(&(pSeq->data[j]), &(pSeq->data[j + 1]));
}
}
}
}
void SelectSort(SeqList *pSeq)
{
int left = 0;
int right = pSeq->size - 1;
int min = left, max = left;
while (left < right)
{
int i = left;
int min = max = left;
while (i <= right)//遍历一遍找出最大和最小的数的下标
{
if (pSeq->data[i]>pSeq->data[max])
max = i;
if (pSeq->data[i] < pSeq->data[min])
min = i;
i++;
}
DataSwap(&(pSeq->data[left]), &(pSeq->data[min]));
if (left == max)
max = min;
DataSwap(&(pSeq->data[right]), &(pSeq->data[max]));
left++;
right--;
}
}
7 . 顺序表的个数,清空链数据,销毁空间
int SeqListSize(SeqList *pSeq)
{
return pSeq->size;
}
void SeqListprint(SeqList *pSeq)
{
int i = 0;
for (; i < pSeq->size; i++)
{
printf("%d ", pSeq->data[i]);
}
}
int CheckSeqList(SeqList *pSeq)
{
if (pSeq->size == 0)
{
return 1;
}
return 0;
}
void SeqListClear(SeqList *pSeq)
{
pSeq->size = 0;
}
void SeqListEmpty(SeqList *pSeq)
{
if (CheckSeqList(pSeq) == 1)
printf("SeqList is empty\n");
else
printf("SeqList is not empty\n");
}
最后是测试文件test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include.h>
#include.h>
#include.h>
#include "seqlist.h"
int main()
{
SeqList s;
SeqListInit(&s);
SeqListPushBack(&s, 1);
SeqListPushBack(&s, 1);
SeqListPushBack(&s, 2);
SeqListPushBack(&s, 3);
SeqListPushBack(&s, 0);
SeqListPushBack(&s, 13);
SeqListPushBack(&s, 2);
SeqListPushBack(&s, 4);
//SeqListprint(&s);
//printf("count = %d\n",SeqListSize(&s));
SeqListPushFront(&s, 8);
SeqListPushFront(&s, 7);
SeqListPushFront(&s, 2);
SeqListPushFront(&s, 6);
SeqListPushFront(&s, 5);
SeqListprint(&s);
printf("size = %d\n", SeqListSize(&s));
SeqListPopBack(&s);
SeqListPopFront(&s);
SeqListprint(&s);
printf("\n");
SeqListInsert(&s, 0, 9);
SeqListprint(&s);
printf("\n");
SeqListErase(&s, 0);
SeqListprint(&s);
printf("\n");
SeqListRemove(&s, 1);
SeqListprint(&s);
printf("\n");
SeqListRemoveAll(&s, 2);
SeqListprint(&s);
printf("\n");
BubbleSort(&s);
SeqListprint(&s);
printf("\n");
SelectSort(&s);
SeqListprint(&s);
printf("\n");
SeqListEmpty(&s);
printf("\n");
SeqListClear(&s);
printf("SeqList is clearing\n");
printf("\n");
SeqListEmpty(&s);
printf("\n");
system("pause");
return 0;
}