顺序表是线性表中的一种重要的数据结构,也是最基础的数据结构,今天我用C语言实现下线性表的基本操作,以及冒泡排序与选择排序在线性表中的算法实践,代码如下:

seqlist.h:

#ifndef __SEQLIST__
#define __SEQLIST__
#define MAX 5
#include <stdlib.h>
typedef int DataType;
typedef struct SeqList
{
DataType array[MAX];
size_t size;
}SeqList;
void PrintSeqList(SeqList* pSeq);
void InitSeqList(SeqList* pSeq);
void PushBack(SeqList* pSeq,DataType x);
void PopBack(SeqList* pSeq);
void PushFront(SeqList* pSeq,DataType x);
void PopFront(SeqList* pSeq);
void Insert(SeqList* pSeq, size_t pos, DataType x);
int Find(SeqList* pSeq, DataType x);
void Erase(SeqList* pSeq, size_t pos);
void Remove(SeqList* pSeq, DataType x);
void RemoveAll(SeqList* pSeq, DataType x);
void BubbleSort(SeqList* pSeq);
void SeclectSort(SeqList* pSeq);
#endif

SeqList.c:

#include "Seqlist.h"
#include <assert.h>
#include <stdio.h>
void static Swap(DataType *left, DataType *right)//内部实现交换的函数
{
DataType tmp = *left;
*left = *right;
*right = tmp;
}
void PrintSeqList(SeqList* pSeq)//打印线并表的内容
{
assert(pSeq);
size_t i = 0;
if (pSeq->size == 0)
{
printf("顺序表为空\n");
return;
}
for (; i < pSeq->size; i++)
{
printf("%d ", pSeq->array[i]);
}
printf("\n");
return;
}
void InitSeqList(SeqList* pSeq)//初始化线性表
{
assert(pSeq);
pSeq->size = 0;
}
void PushBack(SeqList* pSeq, DataType x)//尾插
{
assert(pSeq);
if (pSeq->size == MAX)
{
printf("线性表已满\n");
return;
}
pSeq->array[pSeq->size++] = x;
return;
}
void PopBack(SeqList* pSeq)//尾删
{
assert(pSeq);
if (pSeq->size == 0)
{
printf("线性表为空,删除失败\n");
return;
}
pSeq->size--;
return;
}
void PushFront(SeqList* pSeq, DataType x)//头插
{
size_t i = pSeq->size;
assert(pSeq);
if (pSeq->size == MAX)
{
printf("线性表已满\n");
return;
}
for (; i>0; i--)
{
pSeq->array[i] = pSeq->array[i-1];
}
pSeq->array[i] = x;
pSeq->size++;
return;
}
void PopFront(SeqList* pSeq)//头删
{
size_t i = 0;
assert(pSeq);
if (pSeq->size == 0)
{
printf("线性表空\n");
return;
}
for (; i<pSeq->size-1; i++)
{
pSeq->array[i+1] = pSeq->array[i];
}
pSeq->size--;
return;
}
void Insert(SeqList* pSeq,size_t pos,DataType x)//指定位置插入
{
assert(pSeq);
size_t i = pSeq->size;
if (pSeq->size == MAX)
{
printf("线性表已满,无法插入\n");
return;
}
for (; i >=pos-1; i--)
{
pSeq->array[i] = pSeq->array[i - 1];
}
pSeq->array[pos - 1] = x;
pSeq->size++;
}
int Find(SeqList* pSeq, DataType x)//查找第一个元素
{
assert(pSeq);
size_t i = 0;
for (; i < pSeq->size; i++)
{
if (pSeq->array[i] == x)
return i+1;
}
return -1;
}
void Erase(SeqList* pSeq, size_t pos )//删除指定位置的元素
{
assert(pSeq);
if (pSeq->size == 0)
{
printf("线性表为空,无法删除\n");
return;
}
if (pos > pSeq->size)
{
printf("给出位置不在线性表之内,无法删除\n");
return;
}
size_t i = pos-1;
for (; i <pSeq->size; i++)
{
pSeq->array[i] = pSeq->array[i+1];
}
pSeq->size--;
}
void Remove(SeqList* pSeq, DataType x)//删除第一个元素
{
Erase(pSeq, Find(pSeq,x));
}
void RemoveAll(SeqList* pSeq, DataType x)//删除全部相同元素
{
assert(pSeq);
size_t i = 0; 
size_t count = 0;
for (; i < pSeq->size; i++)
{
if (pSeq->array[i] == x)
{
count++;
}
else
{
pSeq->array[i - count] = pSeq->array[i];
}
}
pSeq->size -= count;
}
void BubbleSort(SeqList* pSeq)//冒泡排序
{
assert(pSeq);
size_t i = 0;
size_t j = 0;
for (; i < pSeq->size; i++)
{
for (j = 0; j < pSeq->size - i -1; j++)
{
if (pSeq->array[j]>pSeq->array[j + 1])
{
Swap(&pSeq->array[j], &pSeq->array[j + 1]);
}
}
}
}
void SeclectSort(SeqList* pSeq)//选择排序
{
size_t min;
size_t i = 0;
size_t j = 0;
for (; i < pSeq->size; i++)
{
min = i;
for (j = min; j < pSeq->size; j++)
{
if (pSeq->array[min]>pSeq->array[j])
{
min = j;
}
}
Swap(&pSeq->array[i], &pSeq->array[min]);
}
}

测试代码:

#include <stdio.h>
#include "Seqlist.h"
void test1(SeqList*pSeq)//测试代码
{
InitSeqList(pSeq);
PrintSeqList(pSeq);
PushBack(pSeq, 1);
PushBack(pSeq, 2);
PushBack(pSeq, 3);
PushBack(pSeq, 4);
PushBack(pSeq, 5);
PushBack(pSeq, 5);
PrintSeqList(pSeq);
PopBack(pSeq);
PopBack(pSeq);
PopBack(pSeq);
PopBack(pSeq);
PopBack(pSeq);
PopBack(pSeq);
PrintSeqList(pSeq);
PushFront(pSeq, 5);
PushFront(pSeq, 4);
PushFront(pSeq, 3);
PushFront(pSeq, 2);
PushFront(pSeq, 1);
PushFront(pSeq, 1);
PrintSeqList(pSeq);
}
void test2(SeqList*pSeq)
{
InitSeqList(pSeq);
PushBack(pSeq, 1);
PushBack(pSeq, 2);
PushBack(pSeq, 3);
PushBack(pSeq, 4);
PushBack(pSeq, 5);
PrintSeqList(pSeq);
PopFront(pSeq);
PopFront(pSeq);
PopFront(pSeq);
PopFront(pSeq);
PopFront(pSeq);
PopFront(pSeq);
}
void test3(SeqList*pSeq)
{
InitSeqList(pSeq);
PushBack(pSeq, 1);
PushBack(pSeq, 3);
PushBack(pSeq, 4);
PushBack(pSeq, 5);
PrintSeqList(pSeq);
Insert( pSeq,2,2);
PrintSeqList(pSeq);
Insert(pSeq, 2, 2);
}
void test4(SeqList*pSeq)
{
InitSeqList(pSeq);
PushBack(pSeq, 1);
PushBack(pSeq, 2);
PushBack(pSeq, 3);
PushBack(pSeq, 4);
PushBack(pSeq, 5);
PrintSeqList(pSeq);
Erase(pSeq, 2);
PrintSeqList(pSeq);
Erase(pSeq, 5);
PrintSeqList(pSeq);
}
void test5(SeqList*pSeq)
{
InitSeqList(pSeq);
PushBack(pSeq, 1);
PushBack(pSeq, 2);
PushBack(pSeq, 3);
PushBack(pSeq, 1);
PushBack(pSeq, 5);
PrintSeqList(pSeq);
RemoveAll(pSeq, 1);
PrintSeqList(pSeq);
}
void test6(SeqList*pSeq)//冒泡排序验证
{
InitSeqList(pSeq);
PushBack(pSeq, 2);
PushBack(pSeq, 4);
PushBack(pSeq, 3);
PushBack(pSeq, 1);
PushBack(pSeq, 5);
PrintSeqList(pSeq);
BubbleSort(pSeq);
PrintSeqList(pSeq);
}
void test7(SeqList*pSeq)
{
InitSeqList(pSeq);
InitSeqList(pSeq);
PushBack(pSeq, 2);
PushBack(pSeq, 4);
PushBack(pSeq, 3);
PushBack(pSeq, 1);
PushBack(pSeq, 5);
PrintSeqList(pSeq);
SeclectSort(pSeq);
PrintSeqList(pSeq);
}
int main()
{
SeqList SL;
test1(&SL);
test2(&SL);
test3(&SL);
printf("%d\n", Find(&SL, 5));
test4(&SL);
test5(&SL);
test6(&SL);
test7(&SL);
system("pause");
return 0;
}

    如有不足希望批评指正