静态顺序表

代码实现顺序表的操作函数

// 值类型
typedef int DataType;

typedef struct SeqList {
DataType data; // 值
int size; // 数量
} SeqList;

// 初始化
void SeqListInit(SeqList *pSL);

// 尾部插入
void SeqListPushBack(SeqList *pSL, DataType data);

// 头部插入
void SeqListPushFront(SeqList *pSL, DataType data);

// 尾部删除
void SeqListPopBack(SeqList *pSL);

// 头部删除
void SeqListPopFront(SeqList *pSL);

// 按下标插入,pos 的范围是 [0, size]
void SeqListInsert(SeqList *pSL, int pos, DataType data);

// 按下标删除,pos 的范围是 [0, size)
void SeqListErase(SeqList *pSL, int pos);

// 按值删除,只删遇到的第一个
void SeqListRemove(SeqList *pSL, DataType data);

// 按值删除,删除所有的
void SeqListRemoveAll(SeqList *pSL, DataType data);

// 清空
void SeqListClear(SeqList *pSL);

// 按值查找,返回第一个找到的下标,如果没找到,返回 -1
int SeqListFind(SeqList *pSL, DataType data);

// 判断是否为空,1 表示空, 0 表示不空
int SeqListEmpty(SeqList *pSL);

// 返回数量
int SeqListSize(SeqList *pSL);

// 销毁
void SeqListDestroy(SeqList *pSL); 






#pragma once

#ifndef _Seqlist_H_
#define _Seqlist__h_
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>


#define MAX 100
typedef int DataType;


typedef struct Seqlist {
int arr[MAX]; 
int size; 
} Seqlist, *pSeqlist;


void TestSeqlist();
void SeqlistInit(Seqlist *pSl);
void SeqlistPushBack(Seqlist *pSl, DataType data);
void SeqlistPushFront(Seqlist *pSl, DataType data);
void SeqlistPopBack(Seqlist *pSl);
void SeqlistPopFront(Seqlist *pSl);
void SeqlistInsert(Seqlist *pSl, int pos, DataType data);
void SeqlistErase(Seqlist *pSl, int pos);
void SeqlistRemove(Seqlist *pSl, DataType data);
void SeqlistRemoveAll(Seqlist *pSl, DataType data);
void SeqlistClear(Seqlist *pSl);
int SeqlistFind(Seqlist *pSl, DataType data);
void SeqlistBubbleSort(Seqlist *pSl);
void SeqListSelectSort(Seqlist *pSl);
int SeqlistEmpty(Seqlist *pSl);
int SeqlistSize(Seqlist *pSl);
void SeqlistDestroy(Seqlist *pSl);
void Seqlistprint(Seqlist *pSl);


#endif




int main()
{
TestSeqlist();
system("pause");
return 0;
}




//初始化
void SeqlistInit(Seqlist *pSl)
{
pSl->size = 0;
memset(pSl->arr, 0, sizeof(DataType)*MAX);
}




//尾插
void SeqlistPushBack(Seqlist *pSl, DataType data)
{
assert(pSl != NULL);
if (pSl->size >= MAX)
{
printf("顺序表已满\n");
return;
}
pSl->arr[pSl->size] = data;
pSl->size++;
}




//头插
void SeqlistPushFront(Seqlist *pSl, DataType data)
{
assert(pSl != NULL);
if (pSl->size >= MAX)
{
printf("顺序表已满\n");
return;
}
int i = 0;
for (i = pSl->size - 1; i >= 0; i--)
{
pSl->arr[i + 1] = pSl->arr[i];
}
pSl->arr[0] = data;
pSl->size++;
}




//尾删
void SeqlistPopBack(Seqlist *pSl)
{


assert(pSl != NULL);
assert(pSl->size > 0);
if (pSl->size == 0)
{
printf("顺序表为空\n");
return;
}
pSl->size--;
}




//头删
void SeqlistPopFront(Seqlist *pSl)
{
assert(pSl != NULL);
if (pSl->size == 0)
{
printf("顺序表为空\n");
return;
}
int i = 0;
for (i = 1; i<pSl->size; i++)
{
pSl->arr[i - 1] = pSl->arr[i];
}


pSl->size--;
}




//随机位置插入
void SeqlistInsert(Seqlist *pSl, int pos, DataType data)
{
assert(pSl != NULL);
assert(pos >= 0 && pos <= pSl->size);


if (pSl->size >= MAX)
{
printf("静态表已满");
return;
}


int i = 0;
for (i = pSl->size - 1; i >= pos; i--)
{
pSl->arr[i + 1] = pSl->arr[i];
}
pSl->arr[pos] = data;
pSl->size++;
}




//随机位置删除
void SeqlistErase(Seqlist *pSl, int pos)
{
assert(pSl != NULL);
if (pSl->size == 0)
{
printf("顺序表为空\n");
return;
}
assert(pos >= 0 && pos < pSl->size);
int i = 0;
for (i = pos; i < pSl->size - 1; i++)
{
pSl->arr[i] = pSl->arr[i + 1];
}
pSl->size--;
}




//查找数
int SeqlistFind(Seqlist *pSl, DataType data)
{
assert(pSl != NULL);
int i = 0;
for (i = 0; i < pSl->size; i++)
{
if (pSl->arr[i] == data)
{
return i;
}
}
return -1;
}




//删除第一个出现的数
void SeqlistRemove(Seqlist *pSl, DataType data)
{
int pos = SeqlistFind(pSl, data);
if (pos != -1)
{
SeqlistErase(pSl, pos);
}
}




//删除所有出现的数
void SeqlistRemoveAll(Seqlist *pSl, DataType data)
{
int spacel = 0;
int count = 0;
int i = 0;
for (i = 0; i < pSl->size; i++)
{
if (pSl->arr[i] == data)
{
count++;
}
else
{
pSl->arr[spacel] = pSl->arr[i];
spacel++;
}


}


pSl->size -= count;


}
void Swap(DataType *a, DataType *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}




void SeqlistBubbleSort(Seqlist *pSl)
{
int i = 0;
int j = 0;
for (i = 0; i < pSl->size - 1; i++)
{
for (j = 0; j < pSl->size - i - 1; j++)
{
if (pSl->arr[j] > pSl->arr[j + 1])
{
Swap(pSl->arr + j, pSl->arr + (j + 1));


}
}


}
}
void SeqListSelectSort(Seqlist *pSl)
{
int i, j;
int minPos, maxPos;
for (i = 0; i < pSl->size / 2; i++)
{
minPos = maxPos = i;
for (j = i; j < pSl->size - i; j++)
{
if (pSl->arr[j] < pSl->arr[minPos])
{
minPos = j;
}
if (pSl->arr[j] > pSl->arr[maxPos])
{
maxPos = j;
}
}
Swap(pSl->arr + i, pSl->arr + minPos);
if (i == maxPos)
{
maxPos = minPos;
}
Swap(pSl->arr + pSl->size - i - 1, pSl->arr + maxPos);
}


}




//顺序表的大小
int SeqlistSize(Seqlist *pSl)
{
return pSl->size;
}


//判断是否为空
int SeqlistEmpty(Seqlist *pSl)
{
return pSl->size == 0 ? 1 : 0;
}




//清空顺序表
void SeqlistClear(Seqlist *pSl)
{
pSl->size = 0;
}


//销毁
void SeqlistDestroy(Seqlist *pSl)
{
;
}






void Seqlistprint(Seqlist *pSl)
{
int i;
for (i = 0; i < pSl->size; i++)
{
printf("%d ", pSl->arr[i]);
}
printf("\n");
}






void TestSeqlist()
{
Seqlist sl;
SeqlistInit(&sl);//初始化顺序表


SeqlistPushBack(&sl, 1);//尾插
SeqlistPushBack(&sl, 2);
SeqlistPushBack(&sl, 3);
SeqlistPushBack(&sl, 4);
Seqlistprint(&sl);


SeqlistPushFront(&sl, 1);//头插
SeqlistPushFront(&sl, 2);
SeqlistPushFront(&sl, 3);
SeqlistPushFront(&sl, 4);
Seqlistprint(&sl);




SeqlistPopBack(&sl);//尾删
Seqlistprint(&sl);




SeqlistPopFront(&sl);//头删
Seqlistprint(&sl);


SeqlistInsert(&sl, 2, 5);//随机位置插入
SeqlistInsert(&sl, 4, 15);
Seqlistprint(&sl);


SeqlistErase(&sl, 3);//随机位置删除
Seqlistprint(&sl);


int ret = SeqlistFind(&sl, 15);//查找数


if (ret == -1)
{
printf("没有找到\n");
}
else
{
printf("找到了,该数下标为:%d\n", ret);
}


SeqlistRemove(&sl, 3);//删除第一次出现的数
Seqlistprint(&sl);


SeqlistRemoveAll(&sl, 2);//删除所有出现的数
Seqlistprint(&sl);


SeqlistPushBack(&sl, 9);
SeqlistPushBack(&sl, 12);
SeqlistPushBack(&sl, 10);
SeqlistPushBack(&sl, 11);
SeqlistPushBack(&sl, 6);
Seqlistprint(&sl);




SeqlistBubbleSort(&sl);//冒泡排序
Seqlistprint(&sl);


SeqListSelectSort(&sl);//选择排序
Seqlistprint(&sl);


int tmp = SeqlistSize(&sl);//顺序表大小
printf("%d\n", tmp);


int r = SeqlistEmpty(&sl);//判断顺序表是否为空
if (r == 1)
{
printf("为空\n");
}
else
{
printf("不为空\n");
}


SeqlistClear(&sl);//清空顺序表
Seqlistprint(&sl);


}
阅读更多
换一批

没有更多推荐了,返回首页