“Seq_D.h”
#ifndef __SEQ_LIST_H__
#define __SEQ_LIST_H__
#define INIT_SIZE 2
#define INC 2
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
typedef int DataType;
typedef struct SeqList
{
DataType *data;//指向空间的指针
int size;//有效个数
int capacity;//容量
}SeqList, *pSeqList;
void InitSeqList(pSeqList pSeq);//初始化
void CheckCapacity(pSeqList pSeq);//扩容
void Destory(pSeqList pSeq);//销毁
void PrintSeqList(SeqList Seq);//打印
void PushBack(pSeqList pSeq, DataType x);//尾插
void PopBack(pSeqList pSeq);//尾删
void PushFront(pSeqList pSeq, DataType x);//头插
void PopFront(pSeqList pSeq);//头删
void Insert(pSeqList pSeq, int pos, DataType x);//在某一位置上插入某一元素
void Remove(pSeqList pSeq, DataType x);//删除x元素
void RemoveAll(pSeqList pSeq, DataType x);//删除所有x元素
int Find(SeqList Seq, DataType x);//查找某一元素
void ReverseList(pSeqList pSeq);//逆置
void SortList(pSeqList pSeq);//排序
int BinarySearch(SeqList Seq, DataType x);//二分查找
void InitSeqList(pSeqList pSeq)
{
assert(pSeq);
pSeq->data = (DataType *)malloc(INIT_SIZE*sizeof(DataType));
memset(pSeq->data,0,INIT_SIZE*sizeof(DataType));
pSeq->capacity = INIT_SIZE;
pSeq->size = 0;
}
void CheckCapacity(pSeqList pSeq)
{
assert(pSeq);
if(pSeq->capacity == pSeq->size)
{
//printf("扩容");
pSeq->data = (DataType *)realloc(pSeq->data,(pSeq->capacity+INC)*sizeof(DataType));
pSeq->capacity = pSeq->capacity+INC;
}
}
void PushBack(pSeqList pSeq, DataType x)
{
CheckCapacity(pSeq);
pSeq->data[pSeq->size] = x;
pSeq->size++;
}
void PrintSeqList(SeqList Seq) //打印
{
int i = 0;
for(i=0;i<Seq.size;i++)
{
printf("%d ",Seq.data[i]);
}
printf("over\n");
}
void Destory(pSeqList pSeq)
{
assert(pSeq);
free(pSeq);
pSeq->data = NULL;
pSeq->size = 0;
pSeq->capacity = 0;
}
void PopBack(pSeqList pSeq)
{
assert(pSeq);
if(pSeq->size == 0)
{
printf("顺序表已空\n");
return;
}
else
{
pSeq->size--;
}
}
void PushFront(pSeqList pSeq, DataType x)
{
int i = 0;
assert(pSeq);
CheckCapacity(pSeq);
if(pSeq->size == 0)
{
pSeq->data[pSeq->size] = x;
pSeq->size++;
}
else
{
for(i=pSeq->size-1;i>=0;i--)
{
pSeq->data[i+1] = pSeq->data[i];
}
pSeq->data[0] = x;
pSeq->size++;
}
}
void PopFront(pSeqList pSeq)
{
int i = 0;
assert(pSeq);
if(pSeq->size == 1)
{
pSeq->size--;
}
else
{
for(i=0;i<pSeq->size;i++)
{
pSeq->data[i] = pSeq->data[i+1];
}
pSeq->size--;
}
}
void Insert(pSeqList pSeq, int pos, DataType x)
{
int j = 0;
assert(pSeq);
CheckCapacity(pSeq);
if(pos<=0 || pos>pSeq->capacity)
{
printf("插入的位置不合法");
return;
}
for(j=pSeq->size-1;j>=pos-1;j--)
{
pSeq->data[j+1]=pSeq->data[j];
}
pSeq->data[pos-1] = x;
pSeq->size++;
}
void Remove(pSeqList pSeq, DataType x)
{
int i = 0;
int j = 0;
assert(pSeq);
if(pSeq->size == 1)
{
pSeq->size--;
return;
}
for(i=0;i<pSeq->size;i++)
{
if(pSeq->data[i] == x)
{
for(j=i;j<pSeq->size;j++)
{
pSeq->data[j] = pSeq->data[j+1];
}
pSeq->size--;
return;
}
}
}
void RemoveAll(pSeqList pSeq, DataType x)
{
int i = 0;
int j = 0;
assert(pSeq);
if(pSeq->size == 1)
{
pSeq->size--;
return;
}
for(i=0;i<pSeq->size;i++)
{
if(pSeq->data[i] == x)
{
for(j=i;j<pSeq->size;j++)
{
pSeq->data[j] = pSeq->data[j+1];
}
pSeq->size--;
}
}
}
void ReverseList(pSeqList pSeq)
{
DataType left = 0;
DataType right = pSeq->size-1;
DataType tmp;
assert(pSeq);
while(left<right)
{
tmp = pSeq->data[left];
pSeq->data[left] = pSeq->data[right];
pSeq->data[right] = tmp;
left++;
right--;
}
}
void SortList(pSeqList pSeq)//冒泡
{
DataType i = 0;
DataType j = 0;
DataType tmp;
assert(pSeq);
for(i=0;i<pSeq->size-1;i++)
{
for(j=0;j<pSeq->size-1-i;j++)
{
if(pSeq->data[j]>pSeq->data[j+1])
{
tmp = pSeq->data[j];
pSeq->data[j] = pSeq->data[j+1];
pSeq->data[j+1] = tmp;
}
}
}
}
int Find(SeqList Seq, DataType x)
{
DataType i = 0;
for(i=0;i<Seq.size;i++)
{
if(Seq.data[i] == x)
{
return i;
}
}
return -1;
}
int BinarySearch(SeqList Seq, DataType x)
{
DataType left = 0;
DataType right = Seq.size-1;
DataType mid = (left+right)/2;
while(left<=right)
{
if(Seq.data[mid]<x)
{
left = mid+1;
}
if(Seq.data[mid]>x)
{
right = mid-1;
}
if(Seq.data[mid] == x)
{
return Seq.data[mid];
}
}
}
#endif //__SEQ_LIST_H__
“test.c”
#include "Seq_D.h"
void Test1() //尾插尾删
{
SeqList mylist;
InitSeqList(&mylist);
PushBack(&mylist,1);
PushBack(&mylist,2);
PushBack(&mylist,3);
PushBack(&mylist,4);
//PopBack(&mylist);
PrintSeqList(mylist);
}
void Test2()//头插头删
{
SeqList mylist;
InitSeqList(&mylist);
PushFront(&mylist,1);
PushFront(&mylist,2);
PushFront(&mylist,3);
PushFront(&mylist,4);
//PopFront(&mylist);
PrintSeqList(mylist);
}
void Test3()
{
SeqList mylist;
InitSeqList(&mylist);
PushBack(&mylist,1);
PushBack(&mylist,2);
PushBack(&mylist,3);
PushBack(&mylist,4);
PrintSeqList(mylist);
printf("\n");
Insert(&mylist,2,5);
PrintSeqList(mylist);
}
void Test4()//删除
{
SeqList mylist;
InitSeqList(&mylist);
PushBack(&mylist,1);
PushBack(&mylist,3);
PushBack(&mylist,2);
PushBack(&mylist,3);
PushBack(&mylist,4);
Remove(&mylist, 3);
//RemoveAll(&mylist, 3);
PrintSeqList(mylist);
}
void Test5()//逆置
{
SeqList mylist;
InitSeqList(&mylist);
PushBack(&mylist,1);
PushBack(&mylist,3);
PushBack(&mylist,2);
PushBack(&mylist,3);
PushBack(&mylist,4);
ReverseList(&mylist);
PrintSeqList(mylist);
}
void Test6()//冒泡
{
SeqList mylist;
InitSeqList(&mylist);
PushFront(&mylist,1);
PushFront(&mylist,2);
PushFront(&mylist,3);
PushFront(&mylist,4);
SortList(&mylist);
PrintSeqList(mylist);
}
void Test7()//二分查找
{
SeqList mylist;
DataType ret = 0;
InitSeqList(&mylist);
PushFront(&mylist,1);
PushFront(&mylist,2);
PushFront(&mylist,3);
PushFront(&mylist,4);
ret=Find(mylist,3);
printf("%d ",ret);
/*ret=BinarySearch(mylist,3);
printf("%d ",ret);*/
}
int main()
{
Test7();
return 0;
}