顺序表的增删改查
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#define MAX_SIZE (100)
typedef int DataType;
typedef struct Seqlist
{
DataType array[MAX_SIZE];//存放数据的地方
int size;//有效个数
}Seqlist;
//初始化
void SeqlistInit(Seqlist *pSL)
{
assert(pSL != NULL);
//memset(pSL->array, 0, MAX_SIZE*sizeof(DataType));//数组内容初始化
pSL->size = 0;//size==0;
}
//销毁
void SeqlistDstroy(Seqlist *pSL)
{
assert(pSL != NULL);
pSL->size = 0;
}
//头插
void SeqlistPushFront(Seqlist *pSL,DataType data)
{
assert(pSL != NULL);
assert(pSL->size < MAX_SIZE);
#if 0
int space;//以要搬运到的空间做循环的指示 指向有效数据后面一个space
for (space = pSL->size; space; space--){
pSL->array[space] = pSL->array[space - 1];
}
#endif
#if 0
int pos;//以要搬运的数字做循环的指示 指向有效数据
for (pos = pSL->size - 1; pos >= 0; pos--){
pSL->array[pos + 1] = pSL->array[pos];
}
#endif
//以循环次数作指示
int i = 0;
for (; i < pSL->size; i++){
pSL->array[pSL->size - i] = pSL->array[pSL->size - i - 1];
}
pSL->array[0] = data;
pSL->size++;//多插入一个数据
}
//尾插
void SeqlistPushBack(Seqlist *pSL, DataType data)
{
assert(pSL != NULL);
assert(pSL->size < MAX_SIZE);
pSL->array[pSL->size] = data;
pSL->size++;
}
//根据下标插入
void SeqlistInsert(Seqlist *pSL, int pos,DataType data)
{
assert(pSL != NULL);
assert(pos >= 0 && pos <= pSL->size);
assert(pSL->size < MAX_SIZE);
int space;
for (space = pSL->size; space > pos;space--){//>pos;pos前面的数字不要搬运;pos后面的数字按序向后移动
pSL->array[space] = pSL->array[space - 1];
}
pSL->array[pos] = data;
pSL->size++;
}
//头删
void SeqlistPopFront(Seqlist *pSL)
{
assert(pSL != NULL);
assert(pSL->size > 0);
int space;
for (space = 0; space < pSL->size;space++){
pSL->array[space] = pSL->array[space+1];
}
pSL->size--;
}
//尾删
void SeqlistPopBack(Seqlist *pSL)
{
assert(pSL != NULL);
assert(pSL->size >0);
pSL->size--;
}
//根据下标删除
void SeqlistErase(Seqlist *pSL, int pos)
{
assert(pSL != NULL);
assert(pos >= 0 && pos < pSL->size);
assert(pSL->size>0);
int space;
for (space = pos; space < pSL->size-1; space++){//删除一个 所以要放的位置肯定是size-1;
pSL->array[space] = pSL->array[space + 1];
}
pSL->size--;
}
//根据数据删除,删除遇到的第一个数据
void SeqlistRemove(Seqlist *pSL,DataType data)
{
assert(pSL != NULL);
assert(pSL->size >0);
int pos = SeqlistFind(pSL,data);
if (pos != -1){
SeqlistErase(pSL, pos);
}
}
//根据数据删除,删掉遇到的所有的数据
void SeqlistRemoveAll(Seqlist *pSL,DataType data)
{
assert(pSL != NULL);
assert(pSL->size >0);
//循环
#if 0
int pos;
while (1){
pos = SeqlistFind(pSL, data);
if (pos == -1){
break;
}
SeqlistErase(pSL, pos);
}
#endif
#if 0
while (pos = SeqlistFind(pSL, data) != -1){
SeqlistErase(pSL, pos);
}
#endif
#if 0
//构造新数组存放其他数据
DataType *newarray = (DataType *)malloc(sizeof(DataType)*pSL->size);
int i, j, k;
for (i = 0, j = 0; i < pSL->size; i++){
if (pSL->array[i] != data){
newarray[j] = pSL->array[i];//array[j]是没有指定data的数组
j++;
}
}
pSL->size = j;
for (k = 0; k < pSL->size; k++){
pSL->array[k] = newarray[k];//新数据放回数组
}
free(newarray);
#endif
int i, j;
for (i = 0, j = 0; i < pSL->size; i++){
if (pSL->array[i] != data){
pSL->array[j] = pSL->array[i];
j++;
}
}
pSL->size = j;
}
//根据下标更新数据
void SeqlistUpdate(Seqlist *pSL, int pos, DataType data)
{
assert(pSL != NULL);
assert(pos >= 0 && pos < pSL->size);
pSL->array[pos] = data;
}
//查询
int SeqlistFind(Seqlist *pSL,DataType data)
{
assert(pSL != NULL);
int i = 0;
for (; i < pSL->size; i++){
if (pSL->array[i] == data){
return i;
}
}
return -1;
}
void swap(DataType *m, DataType *n)
{
DataType t = *m;
*m = *n;
*n = t;
}
//排序
void sort(Seqlist *pSL)
{
int minspace = 0; //minspace存放 找到的最小数的 下标 向两端缩进
int maxspace = pSL->size - 1;//maxspace存放 找到的最大数的 下标 向两端缩进
int minindex, maxindex;//存放 数列中找到最小最大数的下标,
int i;
while (minspace < maxspace){
minindex = minspace;
maxindex = maxspace;
for (i = minspace; i <= maxspace;i++){
if (pSL->array[i] < pSL->array[minindex]){
minindex = i; //找到的最小数的下标
}
if (pSL->array[i] > pSL->array[maxindex]) {
maxindex = i;//找到的最大数的下标
}
}
swap(pSL->array + minindex, pSL->array + minspace);
if (minspace == maxindex) {// 特殊情况如 8 0 1 4 5
maxindex = minindex;
}
swap(pSL->array + maxindex, pSL->array + maxspace);
minspace++;
maxspace--;
}
}
void SeqListClear(Seqlist *pSL)
{
assert(pSL != 0);
}
void SeqlistPrint(Seqlist *pSL)
{
int i = 0;
for (; i < pSL->size; i++){
printf("%d ",pSL->array[i]);
}
printf("\n");
}
void SeqlistTest()
{
Seqlist sl;
SeqlistInit(&sl);
SeqlistPushFront(&sl, 1);
SeqlistPushBack(&sl, 2);
SeqlistPushBack(&sl, 3);
SeqlistPushBack(&sl, 4);
SeqlistPushBack(&sl, 5);
SeqlistInsert(&sl, 2, 4);//下标
SeqlistPopFront(&sl);
SeqlistPopBack(&sl);
SeqlistErase(&sl, 2);
//SeqlistRemove(&sl, 4);
SeqlistRemoveAll(&sl, 4);
SeqlistPushBack(&sl, 8);
SeqlistPushBack(&sl, 1);
SeqlistPushBack(&sl, 15);
sort(&sl);
SeqlistPrint(&sl);
SeqlistDstroy(&sl);
}
int main()
{
SeqlistTest();
system("pause");
return 0;
}