您的位置:首页 > 其它

动态顺序表的一些增、删、查、改、排序

2018-02-01 23:23 337 查看

顺序表呢,是线性表的一种。

它用一段地址连续的存储单元依次存储数据元素的线性结构,地址空间是连续的,一般情况下采用数组,但数组有静态数组和动态数组, 所以顺序表分为:静态顺序表和动态顺序表。

今天写的这个呢是动态的顺序表,就是在静态基础上加了判满,扩容,销毁的函数。

功能如下:

链表开头插入,删除

链表尾部插入,删除

低效普通查找

高效二分查找

替换元素

选择排序

冒泡排序

判断链表是否满

扩大容量

销毁空间

具体代码实现如下:

SequenceList.h(头文件)

#pragma once

#include<stddef.h>
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#include<Windows.h>

typedef int DataType;
#define DefaultCapicity 10
#define EnlargeCapicity 5

typedef struct SeqList
{
DataType* _a;
size_t _size; // 有效数据个数
size_t _capacity; // 容量
}SeqList;

void SeqInit(SeqList* pSeq);//初始化
void SeqPrint(SeqList* pSeq);//打印

void SeqPushBack(SeqList* pSeq, DataType x);//尾插
void SeqPopBack(SeqList* pSeq);//尾删
void SeqPushFront(SeqList* pSeq, DataType x);//头插
void SeqPopFront(SeqList* pSeq);//头删

void SeqInsert(SeqList* pSeq, size_t pos, DataType x);//插入
void SeqErase(SeqList* pSeq, size_t pos);//删除

int SeqFind(SeqList* pSeq, DataType x);//查找
void SeqAt(SeqList* pSeq, size_t pos, DataType x);//替换

void BubbleSort(SeqList* pSeq);//冒泡排序
void Swap(DataType *p1, DataType *p2);//交换
void SelectSort(SeqList* pSeq);//选择排序
int _BinarySearch(DataType* a, DataType begin, DataType end, DataType x);//二分查找函数实现
int BinarySearch(SeqList* pSeq, DataType x);//二分查找

void IsFull(SeqList* pSeq);//判满
void Enlarge(SeqList* pSeq);//扩容
void Destory(SeqList* pSeq);//销毁


SequenceList.c(函数功能实现)

#include"SequenceList.h"

void SeqInit(SeqList* pSeq)//初始化顺序表
{
DataType* tmp;
pSeq->_size = 0;
pSeq->_capacity = DefaultCapicity;

tmp = (DataType*)malloc(sizeof(DataType)*(pSeq->_capacity));
if (tmp == NULL)
{
perror("Malloc fail");
exit(1);
}
pSeq->_a = tmp;
memset(pSeq->_a, 0, sizeof(DataType)*(pSeq->_capacity));

}
void SeqPrint(SeqList* pSeq)//打印顺序表
{
assert(pSeq);
size_t i = 0;

if (pSeq->_size == 0)
{
printf("No element.\n");
return;
}

while (i < pSeq->_size)
{
printf("%d ", pSeq->_a[i]);
i++;
}
printf("\n");

}

void SeqPushBack(SeqList* pSeq, DataType x)//尾插
{
assert(pSeq);
SeqInsert(pSeq, pSeq->_size, x);

}
void SeqPopBack(SeqList* pSeq)//尾删
{
assert(pSeq);
SeqErase(pSeq, pSeq->_size - 1);
}
void SeqPushFront(SeqList* pSeq, DataType x)//头插
{
assert(pSeq);
SeqInsert(pSeq, 0, x);
}
void SeqPopFront(SeqList* pSeq)//头删
{
assert(pSeq);
SeqErase(pSeq, 0);
}

void SeqInsert(SeqList* pSeq, size_t pos, DataType x)//插入
{
assert(pSeq);
size_t i;

IsFull(pSeq);//判满

if ((pos <= pSeq->_size) && (pos >= 0) && (pSeq->_size< pSeq->_capacity))
{
for (i = pSeq->_size; i > pos; i--)
{
pSeq->_a[i] = pSeq->_a[i - 1];
}
pSeq->_a[pos] = x;
pSeq->_size++;
printf("Insert success.\n");
}
else
printf("Input invalid.\n");

}
void SeqErase(SeqList* pSeq, size_t pos)//删除
{
assert(pSeq);
size_t i;

if ((pos < pSeq->_size) && (pos >= 0) && (pSeq->_size > 0))
{
for (i = pos; i < pSeq->_size; i++)
{
pSeq->_a[i] = pSeq->_a[i + 1];
}

pSeq->_size--;
printf("Erase success.\n");
}
else
printf("Input invalid.\n");
}
int SeqFind(SeqList* pSeq, DataType x)//查找
{
assert(pSeq);
size_t i;

for (i = 0; i < pSeq->_size; i++)
{
if (x == (pSeq->_a[i]))
return i;
}
return -1;
}

int _BinarySearch(DataType* a, DataType begin, DataType end, DataType x)//二分查找实现函数
{
DataType mid;

while (begin <= end)
{
mid = begin + (end - begin) / 2;

if (x > a[mid])
begin = mid + 1;
else if (x < a[mid])
end = mid - 1;
else
return mid;
}

return -1;

}

int BinarySearch(SeqList* pSeq, DataType x)//二分查找
{
assert(pSeq);

return _BinarySearch(pSeq->_a, 0, (DataType)(pSeq->_size - 1), x);
}
void SeqAt(SeqList* pSeq, size_t pos, DataType x)//替换
{
assert(pSeq);
assert((pos < pSeq->_size) && (pos >= 0));
pSeq->_
e68f
a[pos] = x;
}
void BubbleSort(SeqList* pSeq)//冒泡排序
{
assert(pSeq);

size_t i, j;
int flag = 0;
int tmp = 0;

for (i = 0; i < pSeq->_size; i++)
{
for (j = 0; j < pSeq->_size - i - 1; j++)
{
if (pSeq->_a[j] > pSeq->_a[j + 1])
{
tmp = pSeq->_a[j];
pSeq->_a[j] = pSeq->_a[j + 1];
pSeq->_a[j + 1] = tmp;
}
flag = 1;
}
if (flag == 0)
{
break;
}

}

}

void Swap(DataType *p1, DataType *p2)//交换
{
DataType tmp;
tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
void SelectSort(SeqList* pSeq)//选择排序,不断选出最大和最小值,将他们放在最后和最开头
{
size_t begin = 0, end = pSeq->_size - 1;
int max = 0, min = 0;
size_t i;

while (begin < end)
{
i = begin;
max = min = i;
while (i <= end)//找出最大数和最小数的下标
{
if (pSeq->_a[i] < pSeq->_a[min])
min = i;
if (pSeq->_a[i] > pSeq->_a[max])
max = i;
i++;
}
Swap(&(pSeq->_a[begin]), &(pSeq->_a[min]));
if (begin == max)//如果max指向begin,则在上一语句已把max指向的值和min指向的互换了,现在改正max指向的值
max = min;
Swap(&(pSeq->_a[end]), &(pSeq->_a[max]));

begin++;
end--;
}
}

void IsFull(SeqList* pSeq)//判满
{
if (pSeq->_size == pSeq->_capacity)
{
printf("This sequence list is full,start enlarge capicity.\n ");
Enlarge(pSeq);
}
return;
}

void Enlarge(SeqList* pSeq)//扩容
{
assert(pSeq);

DataType* tmp = (DataType*)realloc(pSeq->_a,sizeof(DataType)*(pSeq->_capacity + EnlargeCapicity));
if (tmp == NULL)
{
perror("Malloc fail");
exit(1);
}
pSeq->_a = tmp;
pSeq->_capacity = pSeq->_capacity + EnlargeCapicity;
printf("Enlarge successfully.\n");
}

void Destory(SeqList* pSeq)//销毁
{
if (pSeq == NULL)
{
printf("Don't need destory.\n");
}
pSeq->_capacity = 0;
free(pSeq->_a);
printf("Destory successfully.\n");
}


Test.c(测试)

#include"SequenceList.h"

void Test1()//尾插 尾删
{
SeqList s;
SeqInit(&s);
SeqPushBack(&s, 0);
SeqPushBack(&s, 1);
SeqPushBack(&s, 2);
SeqPushBack(&s, 3);

SeqPrint(&s);

SeqPopBack(&s);
SeqPopBack(&s);
SeqPopBack(&s);
SeqPopBack(&s);

SeqPopBack(&s);//无元素可删

SeqPrint(&s);

Destory(&s);//销毁动态申请的空间
}

void Test2()//头插 头删
{
SeqList s;
SeqInit(&s);
SeqPushFront(&s, -1);
SeqPushFront(&s, 1);
SeqPushFront(&s, 2);
SeqPushFront(&s, 3);

SeqPrint(&s);

SeqPopFront(&s);
SeqPopFront(&s);
SeqPopFront(&s);
SeqPopFront(&s);

SeqPopFront(&s);//无元素可删

SeqPrint(&s);

Destory(&s);//销毁动态申请的空间
}

void Test3()//任意位的插入 删除
{
SeqList s;
SeqInit(&s);

SeqPushFront(&s, -1);
SeqPushFront(&s, 1);
SeqPushFront(&s, 2);
SeqPushFront(&s, 3);

SeqPrint(&s);

SeqInsert(&s, 4, 4);
SeqInsert(&s, 0, 0);
SeqInsert(&s, 8, 100);//此处插入位置有误,会提示无效值

SeqPrint(&s);

SeqErase(&s, 0);
SeqErase(&s, 3);
SeqErase(&s, 4);//此处插入位置有误,会提示无效值
SeqPrint(&s);

Destory(&s);//销毁动态申请的空间
}

void Test4()//冒泡排序
{
SeqList s;
SeqInit(&s);

SeqPushFront(&s, -1);
SeqPushFront(&s, 1);
SeqPushFront(&s, 14);
SeqPushFront(&s, 3);
SeqPushBack(&s, 10);

BubbleSort(&s);

SeqPrint(&s);

Destory(&s);//销毁动态申请的空间
}

void Test5()//选择排序
{
SeqList s;
SeqInit(&s);

SeqPushFront(&s, -1);
SeqPushFront(&s, 1);
SeqPushFront(&s, 14);
SeqPushFront(&s, 3);
SeqPushBack(&s, 10);
SeqPushBack(&s, 3);

SelectSort(&s);

SeqPrint(&s);

Destory(&s);//销毁动态申请的空间
}

void Test6()//查找,找到显示下标,找不到显示-1
{
SeqList s;
SeqInit(&s);

SeqPushFront(&s, -1);
SeqPushFront(&s, 1);
SeqPushFront(&s, 14);
SeqPushFront(&s, 3);
SeqPushBack(&s, 10);

SeqPrint(&s);

printf("-1 at %d\n",SeqFind(&s, -1));
printf("-2 at %d\n", SeqFind(&s, -2));
printf("-3 at %d\n", SeqFind(&s, -3));
printf("14 at %d\n", SeqFind(&s, 14));
printf("10 at %d\n", SeqFind(&s, 10));

Destory(&s);//销毁动态申请的空间
}

void Test7()//二分查找
{
SeqList s;
SeqInit(&s);

SeqPushFront(&s, -1);
SeqPushFront(&s, 1);
SeqPushFront(&s, 14);
SeqPushFront(&s, 3);
SeqPushBack(&s, 10);

BubbleSort(&s);

SeqPrint(&s);

printf("-1 at %d\n", BinarySearch(&s, -1));
printf("-2 at %d\n", BinarySearch(&s, -2));//返回-1
printf("-3 at %d\n", BinarySearch(&s, -3));//返回-1
printf("14 at %d\n", BinarySearch(&s, 14));
printf("10 at %d\n", BinarySearch(&s, 10));

Destory(&s);//销毁动态申请的空间
}

void Test8()//替换
{
SeqList s;
SeqInit(&s);

SeqPushFront(&s, 1);
SeqPushFront(&s, 2);
SeqPushFront(&s, 3);
SeqPushFront(&s, 4);
SeqPushBack(&s, 5);

SeqPrint(&s);

SeqAt(&s, 0, 8);
SeqAt(&s, 1, 9);
SeqAt(&s, 2, 10);
SeqAt(&s, 8, 11);//8不在查找范围,断言提示此处不满足条件

SeqPrint(&s);

Destory(&s);//销毁动态申请的空间
}

int main()
{
SeqList s;

Test1(&s);//尾插 尾删
printf("\n");

Test2(&s);//头插 头删
printf("\n");

Test3(&s);//任意位的插入 删除
printf("\n");

Test4(&s);//冒泡排序
printf("\n");

Test5(&s);//选择排序
printf("\n");

Test6(&s);//查找,找到显示下标,找不到返回-1
printf("\n");

Test7(&s);//二分查找,找不到返回-1
printf("\n");

system("pause");

return 0;
}


就到这里了,如果有什么疑问,或者有好的函数实现方法,欢迎评论留言给我,谢谢!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  动态顺序表
相关文章推荐