seqList.h
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef _SEQLIST_H__
#define _SEQLIST_H__
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#define MAX 10
#define DataType int
typedef struct Seqlist
{
DataType data[MAX];
int sz;
}*PSeqList, Seqlist;
void InitSeqList(PSeqList pSeq);
void PushBack(PSeqList pSeq, DataType data);
void PopBack(PSeqList pSeq);
void PushFront(PSeqList pSeq, DataType data);
void PopFront(PSeqList pSeq);
int Find(PSeqList pSeq, DataType data);
void Insert(PSeqList pSeq, int pos, DataType data);
void Erase(PSeqList pSeq, int pos);
void Remove(PSeqList pSeq, DataType data);
void RemoveAll(PSeqList pSeq, DataType data);
int Size(PSeqList pSeq);
int Empty(PSeqList pSeq);
void BubbleSort(PSeqList pSeq);
void SelectSort(PSeqList pSeq);
void SelectSortOP(PSeqList pSeq);
int BinarySearch(PSeqList pSeq, DataType data);
int BinarySearch_R(PSeqList pSeq, int left, int right, DataType d);
void PrintSeqList(PSeqList pSeq);
#endif//_SEQLIST_H__
sedList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"seqlist.h"
void InitSeqList(PSeqList pSeq)
{
assert(pSeq != NULL);
memset(pSeq->data,0,sizeof(DataType)*MAX);
pSeq->sz = 0;
}
void PrintSeqList(PSeqList pSeq)
{
int i = 0;
assert(pSeq != NULL);
if(pSeq->sz ==0)
{
printf("该顺序表没有元素\n");
return;
}
for(i=0; i<pSeq->sz;i++)
{
printf("%d ",pSeq->data[i]);
}
printf("\n");
}
void PushBack(PSeqList pSeq, DataType data)
{
assert(pSeq != NULL);
if(pSeq->sz == MAX)
{
printf("该顺序表已满\n");
return;
}
pSeq->data[pSeq->sz] = data;
pSeq->sz++;
}
void PopBack(PSeqList pSeq)
{
assert(pSeq != NULL);
if(pSeq->sz == 0)
{
printf("该顺序表为空顺序表\n");
return;
}
pSeq->sz
}
void PushFront(PSeqList pSeq, DataType data)
{
int i = 0;
assert(pSeq != NULL);
if(pSeq->sz == MAX)
{
printf("该顺序表已满\n");
return;
}
for(i=pSeq->sz; i>0; i
{
pSeq->data[i] = pSeq->data[i-1];
}
pSeq->data[0] = data;
pSeq->sz++;
}
void PopFront(PSeqList pSeq)
{
int i = 0;
assert(pSeq != NULL);
if(pSeq->sz == 0)
{
printf("该顺序表为空顺序表\n");
return;
}
for(i=0; i<pSeq->sz-1;i++)
{
pSeq->data[i] = pSeq->data[i+1];
}
pSeq->sz
}
int Find(PSeqList pSeq, DataType data)
{
int i = 0;
assert( pSeq != NULL);
if(pSeq->sz == 0)
{
printf("该顺序表为空顺序表\n");
return -1;
}
while( i < pSeq->sz)
{
if(data == pSeq->data[i] )
return i;
i++;
}
return -1;
}
void Insert(PSeqList pSeq, int pos, DataType data)
{
int i = 0;
assert(pSeq != NULL);
if(pSeq->sz == MAX)
{
printf("该顺序表已满\n");
return;
}
for(i=pSeq->sz; i>pos; i
{
pSeq->data[i] = pSeq->data[i-1];
}
pSeq->data[pos] = data;
pSeq->sz++;
}
void Erase(PSeqList pSeq, int pos)
{
int i = 0;
assert(pSeq != NULL);
if(pSeq->sz == 0)
{
printf("该顺序表为空顺序表\n");
return;
}
for(i=pos; i<pSeq->sz-1;i++)
{
pSeq->data[i] = pSeq->data[i+1];
}
pSeq->sz
}
void Remove(PSeqList pSeq, DataType data)
{
int i = 0;
int pos = 0;
assert(pSeq != NULL);
if(pSeq->sz == 0)
{
printf("该顺序表为空顺序表\n");
return;
}
while(pos < pSeq->sz)
{
if(data == pSeq->data[pos])
break;
pos++;
}
for(i=pos;i<pSeq->sz-1;i++)
{
pSeq->data[i] = pSeq->data[i+1];
}
pSeq->sz
}
void RemoveAll(PSeqList pSeq, DataType data)
{
int i = 0;
int pos = 0;
assert(pSeq != NULL);
if(pSeq->sz == 0)
{
printf("该顺序表为空顺序表\n");
return;
}
while(pos < pSeq->sz)
{
if(data == pSeq->data[pos])
{
for(i=pos;i<pSeq->sz-1;i++)
{
pSeq->data[i] = pSeq->data[i+1];
}
pSeq->sz
}
pos++;
}
}
int Size(PSeqList pSeq)
{
return pSeq->sz;
}
int Empty(PSeqList pSeq)
{
return pSeq->sz == 0 ? 0: 1;
}
void Swap(int *p, int *q)
{
int tmp = 0;
tmp = *p;
*p = *q;
*q = tmp;
}
void BubbleSort(PSeqList pSeq)
{
int i = 0;
int j = 0;
assert(pSeq != NULL);
for(i=0; i<pSeq->sz-1; i++)
{
for(j=0; j<pSeq->sz-1-i; j++)
{
if(pSeq->data[j] > pSeq->data[j+1])
{
Swap(&pSeq->data[j],&pSeq->data[j+1]);
}
}
}
}
void SelectSort(PSeqList pSeq)
{
int i = 0;
int j = 0;
assert(pSeq != NULL);
for(i=0; i<pSeq->sz-1; i++)
{
int max = i;
for(j=i+1; j<pSeq->sz; j++)
{
if(pSeq->data[max]<pSeq->data[j])
{
max = j;
}
}
if(max != i)
{
Swap(&pSeq->data[max],&pSeq->data[i]);
}
}
}
void SelectSortOP(PSeqList pSeq)
{
int i = 0;
int start = 0;
int end = pSeq->sz;
assert(pSeq != NULL);
while(i < pSeq->sz/2)
{
int max = i;
int min = i;
for(start=i+1; start<end; start++)
{
if(pSeq->data[max] < pSeq->data[start])
{
max = start;
}
else if(pSeq->data[min] > pSeq->data[start])
min = start;
}
Swap(&pSeq->data[min],&pSeq->data[i]);
if(max == i)
max = min;
Swap(&pSeq->data[max],&pSeq->data[
i++;
}
}
int BinarySearch(PSeqList pSeq, DataType data)
{
int left = 0;
int right = pSeq->sz-1;
assert(pSeq != NULL);
while(left<=right)
{
int mid = left+(right-left)/2;
if(pSeq->data[mid] == data)
{
return mid;
}
else if(pSeq->data[mid] < data)
{
left = mid+1;
}
else
right = mid-1;
}
return -1;
}
int BinarySearch_R(PSeqList pSeq, int left, int right, DataType d)
{
assert(pSeq != NULL);
while(left <= right)
{
int mid = left + (right-left)/2;
if(pSeq->data[mid] == d)
return mid;
else if(pSeq->data[mid] > d)
BinarySearch_R(pSeq,left,right = mid-1,d);
else
BinarySearch_R(pSeq,left = mid+1,right,d);
}
return -1;
}
test.c
void test()
{
Seqlist s;
int ret = 0;
InitSeqList(&s);//初始化
printf("尾插\n");
PushBack(&s,1);
PushBack(&s,2);
PushBack(&s,3);
PushBack(&s,4);
PushBack(&s,5);
PrintSeqList(&s);
printf("尾删\n");
PopBack(&s);
PrintSeqList(&s);
printf("头插\n");
PushFront(&s, 5);
PushFront(&s, 8);
PushFront(&s, 7);
PushFront(&s, 8);
PushFront(&s, 9);
PushFront(&s, 10);
PrintSeqList(&s);
printf("头删\n");
PopFront(&s);
PrintSeqList(&s);
printf("查找指定元素\n");
ret = Find(&s, 6);
printf("%d\n",ret);
printf("在指定位置插入元素\n");
Insert(&s, 5, 12);
PrintSeqList(&s);
printf("删除指定位置元素\n");
Erase(&s, 5);
PrintSeqList(&s);
printf("删除指定元素\n");
Remove(&s, 5);
PrintSeqList(&s);
printf("删除所有指定元素\n");
RemoveAll(&s,8);
PrintSeqList(&s);
printf("返回顺序表的大小\n");
ret = Size(&s);
printf("%d\n",ret);
printf("判断顺序表是否为空\n");
ret = Empty(&s);
if(ret == 1)
{
printf("顺序表不为空\n");
}
if(ret == 0)
{
printf("顺序表为空\n");
}
printf("冒泡排序\n");
BubbleSort(&s);
PrintSeqList(&s);
printf("选择排序\n");
SelectSort(&s);
PrintSeqList(&s);
printf("选择排序的优化\n");
SelectSortOP(&s);
PrintSeqList(&s);
printf("二分查找\n");
ret = BinarySearch(&s,9);
printf("%d\n",ret);
printf("递归二分查找\n");
ret = BinarySearch_R(&s,0,s.sz-1,7 );
printf("%d\n",ret);
}
int main()
{
test();
return 0;
}