实现基于静态数组的顺序表的以下基本操作:
1.查找任意下标元素的值
2.给任意位置的元素设定值
3.查找对应值的下标
4.删除顺序表中指定的值,如果存在重复元素,只删除第一个
5.删除顺序表中所有的指定的值,另外要实现一个时间复杂度为O(N)的优化版本
6.获取顺序表元素个数
7.判定顺序表是否为空
8.普通的冒泡排序
9.利用回调函数进行冒泡排序
seqlist.h
#include<stddef.h>
#include<stdio.h>
#include<stdlib.h>
#define SeqlistMaxSize 20
#define Header_Print printf("\n==========%s==========\n",__FUNCTION__);
typedef char SeqlistType;
typedef struct Seqlist{
SeqlistType SeqlistArr[SeqlistMaxSize];
size_t size;
}Seqlist;
void SeqlistInit(Seqlist* seqlist);
void SeqlistRemoveAll2(Seqlist* seqlist,SeqlistType value);
void SeqlistGet(Seqlist* seqlist,size_t pos);
void SeqlistSet(Seqlist* seqlist,size_t pos,SeqlistType value);
void SeqlistFind(Seqlist* seqlist,SeqlistType value);
void SeqlistRemove(Seqlist* seqlist,SeqlistType to_delete);
void SeqlistRemoveAll(Seqlist* seqlist,SeqlistType to_delete);
size_t SeqlistSize(Seqlist* seqlist);
int SeqlistEmpty(Seqlist* seqlist);
void SeqlistBubbleSort(Seqlist* seqlist);
void SeqlistBubbleSortEx(Seqlist* seqlist,Cmp cmp);
void SeqlistPrint(Seqlist* seqlist,SeqlistType* ch);
void SeqlistPushBack(Seqlist* seqlist,SeqlistType value);
void SeqlistErase(Seqlist* seqlist,size_t pos);
typedef int (*Cmp)(SeqlistType a,SeqlistType b);
seqlist.c
#include"seqlist.h"
#include<stdio.h>
void SeqlistGet(Seqlist* seqlist,size_t pos){
if(seqlist == NULL){
return;
}
if(pos >= seqlist->size){
return;
}
size_t value = seqlist->SeqlistArr[pos];
printf("%c",value);
}
void SeqlistSet(Seqlist* seqlist,size_t pos,SeqlistType value){
if(seqlist == NULL){
return;
}
if(pos >= seqlist->size){
return;
}
size_t i = seqlist->size - 1;
for(;i >= pos;--i){
seqlist->SeqlistArr[i+1]=seqlist->SeqlistArr[i];
}
seqlist->SeqlistArr[pos]=value;
seqlist->size++;
}
size_t SeqlistFind(Seqlist* seqlist,SeqlistType value){
if(seqlist == NULL){
return;
}
size_t i = 0;
for(;i < seqlist->size;++i){
if(seqlist->SeqlistArr[i] == value){
printf("pos = %d ",i);
return i;
}
}
return -1;
}
void SeqlistRemove(Seqlist* seqlist,SeqlistType to_delete){
if(seqlist == NULL){
return;
}
if(seqlist->size == 0){
return;
}
size_t i = 0;
size_t pos = 0;
for(;i < seqlist->size;++i){
if(seqlist->SeqlistArr[i] == to_delete)
pos = i;
}
SeqlistErase(seqlist,pos);
}
void SeqlistRemoveAll(Seqlist* seqlist,SeqlistType to_delete){
if(seqlist == NULL){
return;
}
if(seqlist->size == 0){
return;
}
size_t i = 0;
size_t pos=0;
for(;i<seqlist->size;i++){
if(seqlist->SeqlistArr[i] == to_delete){
pos = i;
for(pos=i;pos<seqlist->size;pos++){
seqlist->SeqlistArr[pos]=seqlist->SeqlistArr[pos+1];
}
seqlist->size--;
}
}
}
size_t SeqlistSize(Seqlist* seqlist){
if(seqlist == NULL){
return;
}
size_t i=0;
size_t size=0;
for(;i<seqlist->size;i++)
size++;
printf("the number of seqlist is %ld\n ",size);
return size;
}
int SeqlistEmpty(Seqlist* seqlist){
if(seqlist == NULL){
return;
}
if(seqlist->size == 0){
printf("the seqlist is empty\n");
return 1;
}
else{
printf("the seqlist is not empty\n");
return -1;
}
}
void SeqlistBubbleSort(Seqlist* seqlist){
if(seqlist == NULL){
return;
}
int i=0;
for(;i<seqlist->size-1;i++){
int j=0;
for(;j<seqlist->size-1-i;j++){
if(seqlist->SeqlistArr[j]>seqlist->SeqlistArr[j+1]){
int tmp = seqlist->SeqlistArr[j];
seqlist->SeqlistArr[j]=seqlist->SeqlistArr[j+1];
seqlist->SeqlistArr[j+1]=tmp;
}
}
}
}
int Greater(SeqlistType a,SeqlistType b){
return a > b?1 : 0;
}
int Less(SeqlistType a,SeqlistType b){
return a < b?1 : 0;
}
Swap(SeqlistType *a,SeqlistType *b){
SeqlistType tmp=*a;
*a=*b;
*b=tmp;
}
void SeqlistBubbleSortEx(Seqlist* seqlist,Cmp cmp){
if(seqlist == NULL){
return;
}
size_t i=0;
size_t j=0;
for(i=0;i<seqlist->size-1;i++){
for(j=0;j<seqlist->size-1-i;j++){
if(cmp(seqlist->SeqlistArr[j],seqlist->SeqlistArr[j+1])){
Swap(&seqlist->SeqlistArr[j],&seqlist->SeqlistArr[j+1]);
}
}
}
}
void SeqlistRemoveAll2(Seqlist* seqlist,SeqlistType value){
if(seqlist == NULL){
return;
}
if(seqlist->size == 0){
return;
}
int i=0;
int count=0;
for(;i<seqlist->size;i++){
if(seqlist->SeqlistArr[i]==value){
count++;
}
else{
seqlist->SeqlistArr[i-count]=seqlist->SeqlistArr[i];
}
}
seqlist->size-=count;
}
test.c
#include"sxb.h"
#include<stdio.h>
void SeqlistInit(Seqlist* seqlist);
void SeqlistPushBack(Seqlist* seqlist,SeqlistType value);
void TestSeqlistGet(){
Header_Print;
Seqlist seqlist;
SeqlistInit(&seqlist);
SeqlistPushBack(&seqlist,'a');
SeqlistPushBack(&seqlist,'b');
SeqlistPushBack(&seqlist,'c');
SeqlistPushBack(&seqlist,'d');
SeqlistGet(&seqlist,1);
}
void TestSeqlistSet(){
Header_Print;
Seqlist seqlist;
SeqlistInit(&seqlist);
SeqlistPushBack(&seqlist,'a');
SeqlistPushBack(&seqlist,'c');
SeqlistPushBack(&seqlist,'d');
SeqlistSet(&seqlist,1,'b');
SeqlistPrint(&seqlist,"insert a value to the specific position");
}
void TestSeqlistFind(){
Header_Print;
Seqlist seqlist;
SeqlistInit(&seqlist);
SeqlistPushBack(&seqlist,'a');
SeqlistPushBack(&seqlist,'b');
SeqlistPushBack(&seqlist,'c');
SeqlistPushBack(&seqlist,'d');
SeqlistFind(&seqlist,'b');
}
void TestSeqlistRemove(){
Header_Print;
Seqlist seqlist;
SeqlistInit(&seqlist);
SeqlistPushBack(&seqlist,'a');
SeqlistPushBack(&seqlist,'b');
SeqlistPushBack(&seqlist,'c');
SeqlistPushBack(&seqlist,'d');
SeqlistRemove(&seqlist,'b');
SeqlistPrint(&seqlist,"delete a value to the specific value");
}
void TestSeqlistRemoveAll(){
Header_Print;
Seqlist seqlist;
SeqlistInit(&seqlist);
SeqlistPushBack(&seqlist,'a');
SeqlistPushBack(&seqlist,'b');
SeqlistPushBack(&seqlist,'c');
SeqlistPushBack(&seqlist,'d');
SeqlistPushBack(&seqlist,'b');
SeqlistRemoveAll(&seqlist,'b');
SeqlistPrint(&seqlist,"delete all the values of the specific value");
}
void TestSeqlistSize(){
Header_Print;
Seqlist seqlist;
SeqlistInit(&seqlist);
SeqlistPushBack(&seqlist,'a');
SeqlistPushBack(&seqlist,'b');
SeqlistPushBack(&seqlist,'c');
SeqlistPushBack(&seqlist,'d');
SeqlistSize(&seqlist);
}
void TestSeqlistEmpty(){
Header_Print;
Seqlist seqlist;
SeqlistInit(&seqlist);
SeqlistPushBack(&seqlist,'a');
SeqlistPushBack(&seqlist,'b');
SeqlistEmpty(&seqlist);
}
void TestSeqlistBubbleSort(){
Header_Print;
Seqlist seqlist;
SeqlistInit(&seqlist);
SeqlistPushBack(&seqlist,'a');
SeqlistPushBack(&seqlist,'c');
SeqlistPushBack(&seqlist,'b');
SeqlistPushBack(&seqlist,'d');
SeqlistPushBack(&seqlist,'a');
SeqlistPushBack(&seqlist,'x');
SeqlistPushBack(&seqlist,'s');
SeqlistPushBack(&seqlist,'f');
SeqlistBubbleSort(&seqlist);
SeqlistPrint(&seqlist,"bubble sort\n");
}
void TestSeqlistBubbleSortEx(){
int Greater(SeqlistType a,SeqlistType b);
int Less(SeqlistType a,SeqlistType b);
Header_Print;
Seqlist seqlist;
SeqlistInit(&seqlist);
SeqlistPushBack(&seqlist,'a');
SeqlistPushBack(&seqlist,'c');
SeqlistPushBack(&seqlist,'b');
SeqlistPushBack(&seqlist,'d');
SeqlistPushBack(&seqlist,'a');
SeqlistPushBack(&seqlist,'x');
SeqlistPushBack(&seqlist,'s');
SeqlistPushBack(&seqlist,'f');
SeqlistBubbleSortEx(&seqlist,Less);
SeqlistPrint(&seqlist,"use the callback function for the bubble sort");
}
void TestSeqlistRemoveAll2(){
Header_Print;
Seqlist seqlist;
SeqlistInit(&seqlist);
SeqlistPushBack(&seqlist,'a');
SeqlistPushBack(&seqlist,'b');
SeqlistPushBack(&seqlist,'c');
SeqlistPushBack(&seqlist,'d');
SeqlistPushBack(&seqlist,'b');
SeqlistRemoveAll2(&seqlist,'b');
SeqlistPrint(&seqlist,"delete all the values of the specific value");
}
int main(){
TestSeqlistGet();
TestSeqlistSet();
TestSeqlistFind();
TestSeqlistRemove();
TestSeqlistRemoveAll();
TestSeqlistSize();
TestSeqlistEmpty();
TestSeqlistBubbleSort();
TestSeqlistBubbleSortEx();
TestSeqlistRemoveAll2();
}