头文件seqlist.h
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#define MAX 10
typedef int DataType;
typedef struct SeqList
{
DataType data[MAX];
int sz;
}SeqList, *pSeqList;
void InitSeqList(pSeqList ps);
void PushBack(pSeqList ps, DataType d);
void PopBack(pSeqList ps);
void Display(const pSeqList ps);
void PushFront(pSeqList ps, DataType d);
void PopFront(pSeqList ps);
int Find(pSeqList ps, DataType d);
void Insert(pSeqList ps, DataType d, int pos);
void Remove(pSeqList ps, DataType d);
void RemoveAll(pSeqList ps, DataType d);
void Reverse(pSeqList ps);
void Sort(pSeqList ps);
int BinarySearch(pSeqList ps, DataType d);
#endif //__SEQLIST_H__
函数实现seqlist.c
#include "seqlist.h"
void InitSeqList(pSeqList ps)
{
assert(ps);
ps->sz = 0;
}
void PushBack(pSeqList ps, DataType d)
{
assert(ps);
assert(ps->sz<MAX);
ps->data[ps->sz] = d;
ps->sz++;
}
void PopBack(pSeqList ps)
{
assert(ps);
if( 0 == ps->sz )
return;
ps->sz--;
}
void Display(const pSeqList ps)
{
int i;
assert(ps);
if( 0 == ps->sz )
{
printf("顺序表为空\n");
return;
}
for( i=0; i<ps->sz; i++)
printf(" %d",ps->data[i]);
printf("\n");
}
void PushFront(pSeqList ps, DataType d)
{
int i;
assert(ps);
assert(ps->sz<MAX);
for( i=ps->sz; i>0; i--)
{
ps->data[i] =ps->data[i-1];
}
ps->data[0] = d;
ps->sz++;
}
void PopFront(pSeqList ps)
{
int i;
assert(ps);
if( 0 == ps->sz )
return;
for( i=0; i<ps->sz-1; i++)
ps->data[i] = ps->data[i+1];
ps->sz--;
}
int Find(pSeqList ps, DataType d)
{
int i;
assert(ps);
for( i=0; i<ps->sz; i++)
{
if( ps->data[i] == d)
return i;
}
return -1;
}
void Insert(pSeqList ps, DataType d, int pos)
{
int i;
assert(ps);
if( pos<0 || pos>ps->sz-1)
return;
assert(ps->sz<MAX);
for( i=ps->sz; i>pos; i--)
ps->data[i] = ps->data[i-1];
ps->data[pos] = d;
ps->sz++;
}
void del(pSeqList ps,int pos)
{
int i;
for( i=pos; i<ps->sz-1; i++)
ps->data[i] = ps->data[i+1];
ps->sz--;
}
void Remove(pSeqList ps, DataType d)
{
int i;
assert(ps);
for( i=0; i<ps->sz; i++)
{
if(ps->data[i] == d)
{
del(ps,i);
return;
}
}
}
void RemoveAll(pSeqList ps, DataType d)
{
int i;
assert(ps);
for( i=0; i<ps->sz; i++)
{
if(ps->data[i] == d)
{
del(ps,i);
i--;
}
}
}
void Reverse(pSeqList ps)
{
int i,j;
assert(ps);
if(ps->sz<=1)
return;
for( (i=0,j=ps->sz-1); i<j; (i++,j--) )
{
DataType tmp = ps->data[i];
ps->data[i] = ps->data[j];
ps->data[j] = tmp;
}
}
int cmp(void* a, void* b)
{
return *(DataType*)a-*(DataType*)b;
}
void Sort(pSeqList ps)
{
assert(ps);
if(ps->sz<=1)
return;
qsort(ps,ps->sz,sizeof(DataType),cmp);
}
int BinarySearch(pSeqList ps, DataType d)
{
int left=0,right=ps->sz-1,mid;
assert(ps);
while(left<=right)
{
mid = left+( (right-left)>>1);
if( d == ps->data[mid])
return mid;
if( d < ps->data[mid])
right = mid-1;
else
left = mid+1;
}
return -1;
}
测试代码test.c
#include "seqlist.h"
int main()
{
int i;
SeqList seq;
pSeqList ps = &seq;
InitSeqList(ps);
PushBack(ps,1);
PushBack(ps,8);
PushBack(ps,23);
PushBack(ps,96);
PushBack(ps,9);
PushBack(ps,0);
PushBack(ps,65);
PushBack(ps,34);
Sort(ps);
Display(ps);
i=BinarySearch(ps,0);
i=BinarySearch(ps,1);
i=BinarySearch(ps,2);
i=BinarySearch(ps,3);
i=BinarySearch(ps,4);
i=BinarySearch(ps,5);
i=BinarySearch(ps,6);
i=BinarySearch(ps,7);
i=BinarySearch(ps,8);
i=BinarySearch(ps,9);
i=BinarySearch(ps,10);
i=BinarySearch(ps,-2);
return 0;
}