用c语言实现表的物理存储,[数据结构]---C语言实现: 顺序表的增删改查等操作

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。

顺序表一般可以分为:

静态顺序表:使用定长数组存储。(栈上)

动态顺序表:使用动态开辟的数组存储。(堆上)

// 顺序表的静态存储

#define N 100

typedef int SLDataType;

typedef struct SeqList

{

SLDataType array[N]; // 定长数组

size_t size; // 有效数据的个数

}SeqList;

// 顺序表的动态存储

typedef struct SeqList

{

SLDataType* array; // 指向动态开辟的数组

size_t size ; // 有效数据个数

size_t capicity ; // 容量空间的大小

}SeqList;

接下来,我们就要实现动态顺序表的相关操作,主要包括:

初始化顺序表

顺序表尾插、尾删

顺序表头插、头删

顺序表任意位置插入、任意位置删除

顺序表元素查找

删除顺序表中第一个值为x的元素

删除顺序表中所有值为x的元素

修改顺序表中某一元素的数值

顺序表扩容,销毁及打印

对顺序表实现BinaryFind(二分查找)

对顺序表实现InsertSort(插入排序)

对顺序表实现BubbleSort(冒泡排序)

//SeqList.h

#pragma once

#define _CRT_SECURE_NO_WARNINGS 1

#include #include#include //定义顺序表

typedef int SLDataType;

typedef struct SeqList

{

SLDataType* arr; //指向动态开辟的数组

int size; //有效数据个数

int capicity; //容量

}SeqList,*PSeq;

//初始化

void SeqListInit(PSeq ps1, int capicity);

//销毁

void SeqListDestory(PSeq ps1);

//扩容

void CheckCapaticity(PSeq ps1);

//打印输出

void SeqListPrint(PSeq ps1);

//尾插

void SeqListPushBack(PSeq ps1,SLDataType x);

//头插

void SeqListPushFront(PSeq ps1,SLDataType x);

//任意pos位置的插入

void SeqListInsert(PSeq ps1, int pos, SLDataType x);

//尾删

void SeqListPopBack(PSeq ps1);

//头删

void SeqListPopFront(PSeq ps1);

//删除pos位的元素

void SeqListErase(PSeq ps1, int pos);

//查值为x的节点,并返回下标

int SeqListFind(PSeq ps1, SLDataType x);

//删除第一个值为x的节点

void SeqListRemove(PSeq ps1, SLDataType x);

//删除所有值为x的节点

void SeqListRemoveAll(PSeq ps1, SLDataType x);

//修改第POS位的值

void SeqListModify(PSeq ps1, int pos, SLDataType x);

//对顺序表实现BubbleSort

void SeqListBubbleSort(PSeq ps1);

//对顺序表实现BinaryFind(二分查找)

int SeqListBinaryFind(PSeq psl, SLDataType x);

//对顺序表实现插入排序 InsertSort

void InsertSort(PSeq ps1);

//SeqList.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "SeqList.h"

#include //接下来,我们就要实现动态顺序表的相关操作,主要包括:

//初始化顺序表

//顺序表尾插、尾删

//顺序表头插、头删

//顺序表任意位置插入、任意位置删除

//顺序表元素查找

//删除顺序表中第一个值为x的元素

//删除顺序表中所有值为x的元素

//修改顺序表中某一元素的数值

//顺序表扩容,销毁及打印

//对顺序表实现BinaryFind(二分查找)

//对顺序表实现InsertSort(插入排序)

//对顺序表实现BubbleSort(冒泡排序)

//初始化

void SeqListInit(PSeq ps1, int newcapicity)

{

assert(ps1);

assert(newcapicity != 0);

ps1->arr = (SLDataType*)malloc(sizeof(int)*newcapicity);

assert(ps1->arr);

ps1->capicity = newcapicity;

ps1->size = 0;

}

//销毁

void SeqListDestory(PSeq ps1)

{

if (ps1->arr)

{

free(ps1->arr);

ps1->arr = NULL;

ps1->capicity = 0;

ps1->size = 0;

}

}

//数据扩容

void CheckCapaticity(PSeq ps1)

{

assert(ps1);

if (ps1->capicity==ps1->size)

{

ps1->capicity *= 2;

realloc(ps1->arr, sizeof(SLDataType)*ps1->capicity);

}

}

//打印输出

void SeqListPrint(PSeq ps1)

{

assert(ps1);

int i = 0;

for (int i = 0; i< (ps1->size); ++i)

{

printf("%d ", ps1->arr[i]);

}

printf("\n");

}

//尾插

void SeqListPushBack(PSeq ps1, SLDataType x)

{

assert(ps1);

CheckCapaticity(ps1);

ps1->arr[ps1->size] = x;

ps1->size++;

}

//头插

void SeqListPushFront(PSeq ps1, SLDataType x)

{

assert(ps1);

CheckCapaticity(ps1);

int i = 0;

for (i = (ps1->size);i>0; --i)

{

ps1->arr[i] = ps1->arr[i - 1];

}

ps1->arr[0] = x;

ps1->size++;

}

//任意pos位置的插入

void SeqListInsert(PSeq ps1, int pos, SLDataType x)

{

assert(ps1);

assert(pos >= 0 && pos <= ps1->size);

CheckCapaticity(ps1);

int i = 0;

for (i = (ps1->size); i > pos;i--)

{

ps1->arr[i] = ps1->arr[i-1];

}

ps1->arr[pos] = x;

ps1->size++;

}

//尾删

void SeqListPopBack(PSeq ps1)

{

assert(ps1);

assert(ps1->size > 0);

ps1->size--;

}

//头删

void SeqListPopFront(PSeq ps1)

{

assert(ps1);

assert(ps1->size > 0);

int i = 0;

for (i = 1; isize;i++)

{

ps1->arr[i-1] = ps1->arr[i];

}

ps1->size--;

}

//删除pos位的元素

void SeqListErase(PSeq ps1, int pos)

{

assert(ps1);

assert(pos <= ps1->size && pos >= 0);

int i = 0;

for (i = pos+1; i<=ps1->size;i++)

{

ps1->arr[i - 1] = ps1->arr[i];

}

ps1->size--;

}

//查值为x的节点,并返回下标

int SeqListFind(PSeq ps1, SLDataType x)

{

assert(ps1);

int i = 0;

for (i = 0; i size;i++)

{

if (ps1->arr[i]==x)

{

return i;

}

}

return -1;

}

//删除第一个值为x的节点

void SeqListRemove(PSeq ps1, SLDataType x)

{

assert(ps1);

int ret=SeqListFind(ps1, x);

if (ret != -1)

{

SeqListErase(ps1, ret);

}

}

//删除所有值为x的节点

//设置中间变量tem重复利用SeqlistFind与SeqlistErase函数

void SeqListRemoveAll(PSeq ps1, SLDataType x)

{

assert(ps1);

int tem = -1;

while ((tem=SeqListFind(ps1,x))!=-1)

{

SeqListErase(ps1, tem);

}

}

//修改第POS位的值

void SeqListModify(PSeq ps1, int pos, SLDataType x)

{

assert(ps1);

//assert((pos >= 0) && pos < (ps1->size));

int i = 0;

for (int i = 0; i < ps1->size;i++)

{

if (i==pos)

{

ps1->arr[i] = x;

}

}

}

//对顺序表实现BubbleSort

void SeqListBubbleSort(PSeq ps1)

{

assert(ps1);

int i = 0;

int j = 0;

for (i = 0; i < (ps1->size);i++)

{

int flag = 1;

for (j = 0; j size-1-i);j++)

{

if ((ps1->arr[j]) > (ps1->arr[j+1]))

{

int ret = 0;

ret = ps1->arr[j];

ps1->arr[j] = ps1->arr[j+1];

ps1->arr[j+1] = ret;

flag = 0;

}

}

if (flag==1)

{

return;

}

}

}

//对顺序表实现BinaryFind(二分查找) 仅限于排序后的序列

int SeqListBinaryFind(PSeq psl, SLDataType x)

{

assert(psl);

assert(psl->size >= 2);

int left = 0;

int right = psl->size - 1;

while (leftarr[mid]>x)

{

right = mid - 1;

}

else if (psl->arr[mid] < x)

{

left = mid + 1;

}

else

return mid+1;

}

return -1;

}

//对顺序表实现插入排序 InsertSort

//void InsertSort(PSeq ps1)

//{

//assert(ps1);

//int i = 0;

//int j = 0;

//int tmp=0;

//for (i = 1; isize; i++)

//{

//tmp = ps1->arr[i];

//j = i - 1;

与已排序的数逐一比较,大于temp时,该数移后

//while ((j >= 0) && (ps1->arr[j]>tmp))

//{

//ps1->arr[j + 1] = ps1->arr[j];

//j--;

//}

存在大于temp的数

//if (j!=i-1)

//{

//ps1->arr[j + 1] = tmp;

//}

//}

//}

//插入排序代码优化

void InsertSort(PSeq ps1)

{

assert(ps1);

int i = 0;

for (i = 0; i < ps1->size-1; i++)

{

int right = i;

int tmp = ps1->arr[right+1];

while((right>=0) && ((ps1->arr[right])>tmp))

{

ps1->arr[right+1]=ps1->arr[right];

right--;

}

ps1->arr[right+1] = tmp;

}

}

//test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "SeqList.h"

int main()

{

SeqList s;

SeqListInit(&s, 5);

SeqListPushBack(&s, 1);

SeqListPushBack(&s, 2);

SeqListPushBack(&s, 18);

SeqListPushBack(&s, 3);

SeqListPushBack(&s, 1);

SeqListPushBack(&s, 7);

SeqListPushBack(&s, 9);

//SeqListPrint(&s);

SeqListPushFront(&s, 12);

SeqListPushFront(&s, 5);

//SeqListPrint(&s);

//SeqListDestory(&s);

//SeqListInsert(&s, 5, 2);

//SeqListPopBack(&s);

//SeqListPopFront(&s);

//SeqListErase(&s, 4);

//SeqListRemove(&s, 3);

//SeqListModify(&s, 4, 55);

//SeqListRemoveAll(&s, 1);

//SeqListBubbleSort(&s);

//int key = SeqListBinaryFind(&s, 18);

//printf("%d\n", key);

InsertSort(&s);

SeqListPrint(&s);

system("pause");

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值