动态顺序表可以克服静态顺序表的一些相应问题,使用时可以根据储存数据的数量对存储空间进行动态开辟,可以节省空间,在这将其一般接口功能进行实现。
#include"Sql_D.h"
#include<stdio.h>
void test1()//初始化
{
SqlList_D mylist;
Init(&mylist);
}
void test2()//打印
{
SqlList_D mylist;
PrintList(&mylist);
}
void test3()//尾插
{
SqlList_D mylist;
Init(&mylist);
PushBack(&mylist, 3);
PushBack(&mylist, 1);
PushBack(&mylist, 4);
PrintList(&mylist);
}
void test4()//头插
{
SqlList_D mylist;
Init(&mylist);
PushBack(&mylist, 3);
PushBack(&mylist, 1);
PushBack(&mylist, 4);
PushFront(&mylist, 5);
PrintList(&mylist);
}
void test5()//尾删
{
SqlList_D mylist;
Init(&mylist);
PushBack(&mylist, 3);
PushBack(&mylist, 1);
PushBack(&mylist, 4);
PopBack(&mylist);
PrintList(&mylist);
}
void test6()//头删
{
SqlList_D mylist;
Init(&mylist);
PushBack(&mylist, 3);
PushBack(&mylist, 1);
PushBack(&mylist, 4);
PopFront(&mylist);
PrintList(&mylist);
}
void test7()//插入
{
SqlList_D mylist;
Init(&mylist);
PushBack(&mylist, 3);
PushBack(&mylist, 1);
PushBack(&mylist, 4);
InsertList(&mylist, 1, 2);
PrintList(&mylist);
}
void test8()//删除
{
SqlList_D mylist;
Init(&mylist);
PushBack(&mylist, 3);
PushBack(&mylist, 1);
PushBack(&mylist, 4);
RemoveList(&mylist, 4);
PrintList(&mylist);
}
void test9()//全部删除
{
SqlList_D mylist;
Init(&mylist);
PushBack(&mylist, 3);
PushBack(&mylist, 1);
PushBack(&mylist, 4);
PushBack(&mylist, 3);
RemoveAllList(&mylist, 3);
PrintList(&mylist);
}
void test10()//逆序
{
SqlList_D mylist;
Init(&mylist);
PushBack(&mylist, 3);
PushBack(&mylist, 1);
PushBack(&mylist, 4);
ReverseList(&mylist);
PrintList(&mylist);
}
void test11()//排序
{
SqlList_D mylist;
Init(&mylist);
PushBack(&mylist, 3);
PushBack(&mylist, 1);
PushBack(&mylist, 4);
SortList(&mylist);
PrintList(&mylist);
}
void test12()//二分查找
{
int ret = 0;
SqlList_D mylist;
Init(&mylist);
PushBack(&mylist, 3);
PushBack(&mylist, 1);
PushBack(&mylist, 4);
PushBack(&mylist, 5);
SortList(&mylist);
ret=BinaryList(&mylist, 4);
printf("%d\n",ret);
}
int main()
{
test12();
system("pause");
return 0;
}
#ifndef __Sql_D_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#define OldCapacity 3
#define InCapacity 3
typedef int DataType;
typedef struct SqlList_D
{
DataType *Data;
int Capacity;
int size;
}SqlList_D;
void Init(SqlList_D *pmylist);
void PrintList(SqlList_D *pmylist);
void PushBack(SqlList_D *pmylist,DataType x);
void PushFront(SqlList_D *pmylist, DataType x);
void PopBack(SqlList_D *pmylist);
void PopFront(SqlList_D *pmylist);
void InsertList(SqlList_D *pmylist, int pos, DataType x);
void RemoveList(SqlList_D *pmylist, DataType x);
void RemoveAllList(SqlList_D *pmylist, DataType x);
void ReverseList(SqlList_D *pmylist);
void SortList(SqlList_D *pmylist);
int BinaryList(SqlList_D *pmylist, DataType x);
int FindList(SqlList_D *pmylist,DataType x)//查找
{
int i = 0;
assert(pmylist);
for (; i < pmylist->size; i++)
{
if (pmylist->Data[i] == x)
return i;
}
return -1;
}
void IncreaseCapacity(SqlList_D *pmylist)//括容
{
assert(pmylist);
if (pmylist->size == pmylist->Capacity)
{
pmylist->Data = (DataType*)realloc(pmylist->Data, InCapacity);
}
}
void Init(SqlList_D *pmylist)//初始化
{
assert(pmylist);
pmylist->Data = (DataType*)malloc(OldCapacity*sizeof(DataType));
memset(pmylist->Data, 0, (OldCapacity*sizeof(DataType)));
pmylist->size = 0;
}
void PrintList(SqlList_D *pmylist)//打印
{
assert(pmylist);
int i = 0;
for (; i < pmylist->size; i++)
{
printf("%d ", pmylist->Data[i]);
}
}
void PushBack(SqlList_D *pmylist,DataType x)//尾插
{
assert(pmylist);
if (pmylist->Capacity == pmylist->size)
{
IncreaseCapacity(pmylist);
}
else
{
pmylist->Data[pmylist->size] = x;
pmylist->size++;
}
}
void PushFront(SqlList_D *pmylist, DataType x)//头插
{
assert(pmylist);
if (pmylist->Capacity == pmylist->size)
{
IncreaseCapacity(pmylist);
}
else
{
int i = pmylist->size;
for (; i > 0; i--)
{
pmylist->Data[i] = pmylist->Data[i - 1];
}
pmylist->Data[0] = x;
pmylist->size++;
}
}
void PopBack(SqlList_D *pmylist)//尾删
{
assert(pmylist);
pmylist->size--;
}
void PopFront(SqlList_D *pmylist)//头删
{
assert(pmylist);
int i=0;
for (; i < pmylist->size-1; i++)
{
pmylist->Data[i] = pmylist->Data[i + 1];
}
pmylist->size--;
}
void InsertList(SqlList_D *pmylist, int pos, DataType x)//插入
{
assert(pmylist);
if (pmylist->Capacity == pmylist->size)
{
IncreaseCapacity(pmylist);
}
else
{
int i = pmylist->size;
for (; i>pos; i--);
{
pmylist->Data[i] = pmylist->Data[i - 1];
}
pmylist->Data[pos] = x;
pmylist->size ++ ;
}
}
void RemoveList(SqlList_D *pmylist, DataType x)//删除
{
int i = 0;
int ret;
assert(pmylist);
ret = FindList(pmylist, x);
if (ret == -1)
{
return;
}
else
{
int j=ret ;
for (; j < pmylist->size-1; j++)
{
pmylist->Data[j] = pmylist->Data[j + 1];
}
pmylist->size--;
}
}
void RemoveAllList(SqlList_D *pmylist, DataType x)//全部删除
{
int i=0;
assert(pmylist);
while (i < pmylist->size)
{
RemoveList(pmylist, x);
i++;
}
}
void ReverseList(SqlList_D *pmylist)//逆序
{
assert(pmylist);
int left = 0;
int right = pmylist->size - 1;
while (left < right)
{
DataType tmp;
tmp = pmylist->Data[left];
pmylist->Data[left] = pmylist->Data[right];
pmylist->Data[right] = tmp;
left++;
right--;
}
}
void SortList(SqlList_D *pmylist)//排序
{
assert(pmylist);
int i = 0;
int j=0;
for (i = 0; i < pmylist->size - 1; i++)
{
for (j = 0; j < pmylist->size - i - 1; j++)
{
if (pmylist->Data[j]>pmylist->Data[j + 1])
{
DataType tmp;
tmp = pmylist->Data[j];
pmylist->Data[j] = pmylist->Data[j + 1];
pmylist->Data[j + 1] = tmp;
}
}
}
}
int BinaryList(SqlList_D *pmylist, DataType x)//二分查找
{
int left = 0;
int right = pmylist->size - 1;
while (left < right)
{
int mid = (left + right) / 2;
if (pmylist->Data[mid]<x)
{
left = mid + 1;
}
else if (pmylist->Data[mid]>x)
{
right = mid - 1;
}
else
{
return mid;
}
}
}
#endif __Sql_D_H__