一、顺序表(数组的延伸)
1、静态顺序表(增,删,查,改)
2、动态顺序表 (增,删,查,改)
二、下面来实现顺序表的简单功能(这里只写动态顺序表,静态顺序表是动态的子集)
建立顺序表
typedef int DateType;
typedef struct SeqList
{
DateType *data;
size_t size;
size_t capicity;
}SeqList;
1、顺序表的初始化
void InitList(SeqList* Seq) //初始化顺序表
{
Seq->size = 0;
Seq->capicity=1;
Seq->data=(DateType*)malloc(Seq->capicity *sizeof(DateType));
memset(Seq->data,0,Seq->size *sizeof(DateType));
}
2、顺序表的容量判断和扩容
void ListCheckFull(SeqList *Seq) //检查容量,并扩容
{
DateType *tmp;
assert(Seq);
if (Seq->size==Seq->capicity)
{
tmp= (DateType*)realloc(Seq->data,2*Seq->capicity*sizeof(DateType)+3*sizeof(DateType));
if (NULL==tmp)
{
printf("扩容失败 !\n");
return;
}
else
{
Seq->capicity=Seq->capicity*2+3;
Seq->data=tmp;
}
}
}
3、顺序表的插入(包括头插和尾插)
void ListInsert(SeqList *Seq,size_t pos,DateType x) //插入(包括头插和尾插)
{
DateType end=0;
assert(Seq);
ListCheckFull(Seq);
if (pos>Seq->size)
printf("插入位置无意义\n");
else
{
if (0==Seq->size) //头插情况
{
Seq->data[Seq->size]=x;
Seq->size++;
}
else if(pos==Seq->size) //尾插情况
{
Seq->data[Seq->size]=x;
Seq->size++;
}
else //随机插入
{
end=Seq->size;
while ((DateType)pos<end)
{
Seq->data[end]=Seq->data[end-1];
end--;
}
Seq->data[end]=x;
Seq->size++;
}
}
}
4、顺序表的删除
void SeqListDelete(SeqList *Seq,size_t pos) //删除
{
size_t tmp=pos-1;;
assert(Seq);
if (pos>=Seq->size)
{
printf("删除数据不存在 !");
return ;
}
else if (Seq->size==pos) //尾删
{
Seq->size--;
}
else
{
while (tmp<Seq->size) //任意位置删除
{
Seq->data[tmp]=Seq->data[tmp+1];
tmp++;
}
Seq->size--;
}
}
5、顺序表的查找((1)、遍历查找 (2)、 折半查找)
(1)、遍历查找
DateType SeqListSearch(SeqList *Seq,DateType x) //遍历查找
{
DateType i = 0;
assert(Seq);
while (i<(int)Seq->size)
{
if (Seq->data[i]==x)
return i;
i++;
}
return -1;
}
(2)、折半查找
DateType SeqListBinarySearch(SeqList *Seq,DateType x) //折半查找
{
DateType start = 0;
DateType end =Seq->size-1;
DateType mid = 0;
assert(Seq);
while (start<end)
{
mid = start+((end-start)>>1);
if (x<Seq->data[mid])
end=mid-1;
else if (x>Seq->data[mid])
start=mid+1;
else
return Seq->data[mid];
}
return -1;
}
6、修改顺序表
void SeqListChange(SeqList *Seq,DateType pos, DateType x) //修改
{
size_t tmp;
assert(Seq);
tmp = SeqListSearch(Seq,pos); //找到要修改的位置
if (tmp != -1)
Seq->data[tmp]=x;
else
printf("修改数不存在!");
}
7、排序顺序表((1)、冒泡排序 (2)选择排序)
(1)冒泡排序
void SeqListBubbleSort(SeqList *Seq) //冒泡排序
{
size_t i = 0;
size_t j = 0;
size_t flag =0;
assert(Seq);
for (i=0; i<Seq->size-1; i++)
{
for (j=0; j<Seq->size-1-i; j++)
{
if (Seq->data[j]>Seq->data[j+1])
{
flag=1;
Seq->data[j]^=Seq->data[j+1];
Seq->data[j+1]^=Seq->data[j];
Seq->data[j]^=Seq->data[j+1];
}
}
if (flag==0) //优化冒泡次数
{
break;
}
}
}
(2)、选择排序
void SeqListSelectSort(SeqList *Seq) //选择排序
{
DateType min;
DateType max;
size_t i=0;
size_t j=0;
size_t end;
assert(Seq);
end=Seq->size-1;
for (i=0; i<=end; i++)
{
min=i;
max=i;
for (j=i; j<=end; j++)
{
if (Seq->data[j]<Seq->data[min])
min=j;
if (Seq->data[max]<Seq->data[j])
max=j;
}
if (min!=i)
{
Seq->data[i]^=Seq->data[min];
Seq->data[min]^=Seq->data[i];
Seq->data[i]^=Seq->data[min];
}
if (max==i)
max=min;
if (max!=end)
{
Seq->data[end]^=Seq->data[max];
Seq->data[max]^=Seq->data[end];
Seq->data[end]^=Seq->data[max];
}
end--;
}
}
三、测试用例
1、插入测试
void test1() //插入测试
{
InitList(&list);
PrintList(&list);
printf("\n");
ListInsert(&list,0,4);
ListInsert(&list,1,1);
ListInsert(&list,2,2);
ListInsert(&list,2,5);
ListInsert(&list,2,6);
ListInsert(&list,3,3);
ListInsert(&list,5,10);
ListInsert(&list,2,0);
PrintList(&list);
printf("\n");
}
2、删除测试
void test2() //删除测试
{
SeqListDelete(&list,1);
SeqListDelete(&list,4);
PrintList(&list);
printf("\n");
}
3、查找测试
void test3() //遍历查找测试
{
printf("%d ",SeqListSearch(&list,4));
printf("%d ",SeqListSearch(&list,1));
printf("%d ",SeqListSearch(&list,6));
printf("%d ",SeqListSearch(&list,5));
printf("%d ",SeqListSearch(&list,3));
printf("%d ",SeqListSearch(&list,2));
printf("%d ",SeqListSearch(&list,0));
printf("%d ",SeqListSearch(&list,8));
printf("\n");
}
4、修改测试
void test4() //修改测试
{
SeqListChange(&list,2,16);
SeqListChange(&list,3,46);
SeqListChange(&list,4,16);
PrintList(&list);
printf("\n");
}
5、冒泡测试
void test5() //冒泡测试
{
SeqListBubbleSort(&list);
PrintList(&list);
printf("\n");
}
6、选择排序测试
void test6() //选择排序测试
{
SeqListSelectSort(&list);
PrintList(&list);
printf("\n");
}
四、整体代码
1、SeqList.h
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef int DateType;
typedef struct SeqList
{
DateType *data;
size_t size;
size_t capicity;
}SeqList;
void InitList(SeqList *Seq); //初始化顺序表
void PrintList(SeqList *Seq); //打印顺序表
void ListCheckFull(SeqList *Seq); //检查是否满了,并扩容
void ListInsert(SeqList *Seq,size_t pos,DateType x); //随机插入(包括头插和尾插)
void SeqListDelete(SeqList *Seq,size_t pos); //删除(包括头删和尾删)
DateType SeqListSearch(SeqList *Seq,DateType x); //顺序查找
DateType SeqListBinarySearch(SeqList *Seq,DateType x); //二分查找
void SeqListChange(SeqList *Seq,DateType pos,DateType x); //修改顺序表
void SeqListBubbleSort(SeqList *Seq); //冒泡排序顺序表
void SeqListSelectSort(SeqList *Seq); //选择排序顺序表
#endif
2、
SeqList.c
#include "SeqList.h"
void InitList(SeqList* Seq) //初始化顺序表
{
Seq->size = 0;
Seq->capicity=1;
Seq->data=(DateType*)malloc(Seq->capicity *sizeof(DateType));
memset(Seq->data,0,Seq->size *sizeof(DateType));
}
void PrintList(SeqList *Seq) //打印顺序表
{
size_t i =0;
assert(Seq);
for (i=0; i<Seq->size; i++)
printf("%d ",Seq->data[i]);
printf("\n");
}
void ListCheckFull(SeqList *Seq) //检查容量,并扩容
{
DateType *tmp;
assert(Seq);
if (Seq->size==Seq->capicity)
{
tmp= (DateType*)realloc(Seq->data,2*Seq->capicity*sizeof(DateType)+3*sizeof(DateType));
if (NULL==tmp)
{
printf("扩容失败 !\n");
return;
}
else
{
Seq->capicity=Seq->capicity*2+3;
Seq->data=tmp;
}
}
}
void ListInsert(SeqList *Seq,size_t pos,DateType x) //插入(包括头插和尾插)
{
DateType end=0;
assert(Seq);
ListCheckFull(Seq);
if (pos>Seq->size)
printf("插入位置无意义\n");
else
{
if (0==Seq->size) //头插情况
{
Seq->data[Seq->size]=x;
Seq->size++;
}
else if(pos==Seq->size) //尾插情况
{
Seq->data[Seq->size]=x;
Seq->size++;
}
else //随机插入
{
end=Seq->size;
while ((DateType)pos<end)
{
Seq->data[end]=Seq->data[end-1];
end--;
}
Seq->data[end]=x;
Seq->size++;
}
}
}
void SeqListDelete(SeqList *Seq,size_t pos) //删除
{
size_t tmp=pos-1;;
assert(Seq);
if (pos>=Seq->size)
{
printf("删除数据不存在 !");
return ;
}
else if (Seq->size==pos) //尾删
{
Seq->size--;
}
else
{
while (tmp<Seq->size) //任意位置删除
{
Seq->data[tmp]=Seq->data[tmp+1];
tmp++;
}
Seq->size--;
}
}
DateType SeqListSearch(SeqList *Seq,DateType x) //遍历查找
{
DateType i = 0;
assert(Seq);
while (i<(int)Seq->size)
{
if (Seq->data[i]==x)
return i;
i++;
}
return -1;
}
DateType SeqListBinarySearch(SeqList *Seq,DateType x) //折半查找
{
DateType start = 0;
DateType end =Seq->size-1;
DateType mid = 0;
assert(Seq);
while (start<end)
{
mid = start+((end-start)>>1);
if (x<Seq->data[mid])
end=mid-1;
else if (x>Seq->data[mid])
start=mid+1;
else
return Seq->data[mid];
}
return -1;
}
void SeqListChange(SeqList *Seq,DateType pos, DateType x) //修改
{
size_t tmp;
assert(Seq);
tmp = SeqListSearch(Seq,pos); //找到要修改的位置
if (tmp != -1)
Seq->data[tmp]=x;
else
printf("修改数不存在!");
}
void SeqListBubbleSort(SeqList *Seq) //冒泡排序
{
size_t i = 0;
size_t j = 0;
size_t flag =0;
assert(Seq);
for (i=0; i<Seq->size-1; i++)
{
for (j=0; j<Seq->size-1-i; j++)
{
if (Seq->data[j]>Seq->data[j+1])
{
flag=1;
Seq->data[j]^=Seq->data[j+1];
Seq->data[j+1]^=Seq->data[j];
Seq->data[j]^=Seq->data[j+1];
}
}
if (flag==0) //优化冒泡次数
{
break;
}
}
}
void SeqListSelectSort(SeqList *Seq) //选择排序
{
DateType min;
DateType max;
size_t i=0;
size_t j=0;
size_t end;
assert(Seq);
end=Seq->size-1;
for (i=0; i<=end; i++)
{
min=i;
max=i;
for (j=i; j<=end; j++)
{
if (Seq->data[j]<Seq->data[min])
min=j;
if (Seq->data[max]<Seq->data[j])
max=j;
}
if (min!=i)
{
Seq->data[i]^=Seq->data[min];
Seq->data[min]^=Seq->data[i];
Seq->data[i]^=Seq->data[min];
}
if (max==i)
max=min;
if (max!=end)
{
Seq->data[end]^=Seq->data[max];
Seq->data[max]^=Seq->data[end];
Seq->data[end]^=Seq->data[max];
}
end--;
}
}
3、test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
SeqList list;
void test1() //插入测试
{
InitList(&list);
PrintList(&list);
printf("\n");
ListInsert(&list,0,4);
ListInsert(&list,1,1);
ListInsert(&list,2,2);
ListInsert(&list,2,5);
ListInsert(&list,2,6);
ListInsert(&list,3,3);
ListInsert(&list,5,10);
ListInsert(&list,2,0);
PrintList(&list);
printf("\n");
}
void test2() //删除测试
{
SeqListDelete(&list,1);
SeqListDelete(&list,4);
PrintList(&list);
printf("\n");
}
void test3() //遍历查找测试
{
printf("%d ",SeqListSearch(&list,4));
printf("%d ",SeqListSearch(&list,1));
printf("%d ",SeqListSearch(&list,6));
printf("%d ",SeqListSearch(&list,5));
printf("%d ",SeqListSearch(&list,3));
printf("%d ",SeqListSearch(&list,2));
printf("%d ",SeqListSearch(&list,0));
printf("%d ",SeqListSearch(&list,8));
printf("\n");
}
void test4() //修改测试
{
SeqListChange(&list,2,16);
SeqListChange(&list,6,46);
SeqListChange(&list,5,16);
PrintList(&list);
printf("\n");
}
void test5() //冒泡测试
{
SeqListBubbleSort(&list);
PrintList(&list);
printf("\n");
}
void test6() //选择排序测试
{
SeqListSelectSort(&list);
PrintList(&list);
printf("\n");
}
void test7() //折半查找测试
{
printf("%d ",SeqListBinarySearch(&list,4));
printf("%d ",SeqListBinarySearch(&list,1));
printf("%d ",SeqListBinarySearch(&list,6));
printf("%d ",SeqListBinarySearch(&list,5));
printf("%d ",SeqListBinarySearch(&list,3));
printf("%d ",SeqListBinarySearch(&list,2));
printf("%d ",SeqListBinarySearch(&list,0));
printf("%d ",SeqListBinarySearch(&list,8));
printf("\n");
}
int main ()
{
test1();
test2();
test3();
test4();
test5();
test6();
test7();
return 0;
}