动态顺序表的建立

#pragma once
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>

typedef int DataType;
#define INIT_SIZE 10
typedef struct SeqList
{
DataType* _array;
size_t _size;
size_t _capacity;
}
SeqList;

typedef SeqList* pSeqList;

//

//动态顺序表的初始化
void SeqListInit(pSeqList ps);
//尾插
void SeqListPushBack(pSeqList ps, DataType data);
//尾删
void SeqListPopBack(pSeqList ps);
//头插
void SeqListPushFront(pSeqList ps, DataType data);
//头删
void SeqListPopFront(pSeqList ps);

//顺序表判空
int SeqListEmpty(pSeqList ps);
//获取顺序表中有效元素的个数
int SeqListSize(pSeqList ps);
//获取顺序表的容量
int SeqListCapacity(pSeqList PS);
//清空顺序表中的所有元素
void SeqListClear(pSeqList ps);
//动态增容
void CheckCapacity(pSeqList ps);

//销毁顺序表
void SeqListDestory(pSeqList ps);
//顺序表任意位置插入元素
void SeqListInsert(pSeqList ps);
//顺序表任意位置删除元素
void SeqListErase(pSeqList ps);

//打印顺序表
void SeqListPrint(pSeqList ps);
//在动态顺序表中查找元素data 找到返回下标 找不到返回-1
int Find(pSeqList ps, DataType data);

//

void SeqListInit(pSeqList ps)
{
assert(ps);
ps->_array = (DataType*)malloc(sizeof(DataType)*INIT_SIZE);
if (ps->_array == 0)
{
printf(“申请失败\n”);
return ;
}
ps->_size = 0;
ps->_capacity = INIT_SIZE;
}

//检查当前动态顺序表容量 不够则申请空间
void CheckCapacity(pSeqList ps)
{
assert(ps);
if (ps->_size >= ps->_capacity)
{
DataTypep = (DataType)realloc(ps->_array, 2 * (ps->_capacity)*sizeof(DataType));
if (p == NULL)
{
printf(“内存申请失败\n”);
return ;
}
else
{
ps->_array = p;
ps->_capacity = 2 * ps->_capacity;
}
}
}

//打印动态顺序表
void SeqListPrint(pSeqList ps)
{
assert(ps);
for (size_t i = 0; i < ps->_size; i++)
{
printf("%d", ps->_array[i]);
}
printf(“插入成功\n”);
}

//尾删
void SeqListPopBack(pSeqList ps)
{
assert(ps);
if (ps->_size == 0)
{
printf(“顺序表为空,无法删除\n”);
return ;
}
ps->_size–;
}

//头插
void SeqListPushBack(pSeqList ps, DataType data)
{
assert(ps);
CheckCapacity(ps);
for (int i = ps->_size; i > 0; i++)
{
ps->_array[i] = ps->_array[i - 1];
}
ps->_array[0] = data;
ps->_size++;
}

//头删
void SeqListPopFront(pSeqList ps)
{
assert(ps);
if (ps->_size == 0)
{
printf(“顺序表为空,无法删除\n”);
return ;
}
for (int i = 0; i < (ps->_size); i++)
{
ps->_array[i] = ps->_array[i + 1];
}
ps->_size–;
}

//顺序表判空 空时返回0 否则为1
int SeqListEmpty(pSeqList ps)
{
assert(ps);
if (ps->_size == 0)
{
printf(“顺序表为空\n”);
}
printf(“顺序表里有%d个元素\n”, ps->_size);
return ps->_size;
}

//任意位置插入元素
void SeqListInsert(pSeqList ps, DataType pos, DataType data)
{
assert(ps);
CheckCapacity(ps);
for (int i = ps->_size; i > pos; i–)
{
ps->_array[i] = ps->_array[i - 1];
}
ps->_array[pos] = data;
ps->_size++;
}

//任意位置删除元素
void SeqListErase(pSeqList ps, DataType pos)
{
assert(ps);
if (pos <= 0 || pos >= ps->_size)
{
printf(“输入不正确\n”);
return;
}
for (int i = pos; i < ps->_size - 1; i++)
{
ps->_array[i] = ps->_array[i + 1];
}
ps->_size–;
}

//获取顺序表中有效元素的个数
int SeqListSize(pSeqList ps)
{
assert(ps);
return ps->_size;
}
//获取顺序表的容量
int SeqListCapacity(pSeqList ps)
{
assert(ps);
return ps->_capacity;
}

//清空顺序表中的所有元素
void SeqListClear(pSeqList ps)
{
assert(ps);
ps->_size = 0;;
}

//销毁顺序表
void SeqListDestory(pSeqList ps)
{
assert(ps);
free(ps->_array);
ps->_array = NULL;
ps->_capacity = 0;
ps->_size = 0;
}

//查找元素 找到返回下标 无返回-1
int Find(pSeqList ps, DataType data)
{
assert(ps);
if (ps->_size == 0)
{
printf(“顺序表为空\n”);
}
for (int i = 0; i < ps->_size; i++)
{
if (ps->_array[i] == data)
return i;
}
return -1;
}
//
void SeqListTest()
{
SeqList p;
SeqListInit(&p);
SeqListPushBack(&p, 1);
SeqListPushBack(&p, 2);
SeqListPushBack(&p, 3);
SeqListPushBack(&p, 4);
SeqListPrint(&p);

SeqListPopBack(&p);
SeqListPrint(&p);

SeqListPushFront(&p, 4);
SeqListPushFront(&p, 5);
SeqListPrint(&p);

SeqListPopFront(&p);
SeqListPrint(&p);
SeqListEmpty(&p);

SeqListInsert(&p, 2,6);
SeqListPrint(&p);

SeqListErase(&p, 2);
SeqListPrint(&p);

BinarySearch(&p, 3);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值