顺序表的实现

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

typedef int elemtype;
typedef struct SQlist
{
	elemtype data[10];  
	int length;
	int capacity;
}SQlist,*PSQlist;

void Init_SQlist(PSQlist ps);
bool Insert(PSQlist ps, int pos, int val);
bool Del_pos(PSQlist ps, int pos);
bool Del_pos(PSQlist ps, int val);
bool IsEmpty(PSQlist ps);
bool IsFull(PSQlist ps);
int Get_Length(PSQlist ps);
void Clear(PSQlist ps);
void Destory(PSQlist ps);
void expland(PSQlist ps, int n);

#include<stdio.h>
#include<assert.h>
#include"sqlist.h"

void Init_SQlist(PSQlist ps)
{
	assert(ps!= NULL);
	ps->length = 0;
}

bool Insert(PSQlist ps, int pos, int val)
{
	assert(ps != NULL&&pos<=0&&pos>=ps->length);
	if(ps==NULL||pos<0||pos>ps->length)
	{
	    return false;
	}
	if (IsFull(ps))
	{
		return false;
	}
	for (int i = ps->length - 1; i >= pos; i--)
	{
		ps->data[i+1] = ps->data[i];
	}
	ps->data[pos] = val;
	ps->length++;
	return true;
}
bool Del_pos(PSQlist ps, int pos)
{
	assert(ps != NULL&&pos>=0&&pos<ps->length);
	if (IsEmpty(ps))
	{
		return false;
	}
	for (int i = pos + 1; i < ps->length; i++)
	{
		ps->data[i - 1] = ps->data[i];
	}
	ps->length--;
	return true;
}
bool Del_val(PSQlist ps, int val)
{
	assert(ps != NULL);
	if (IsEmpty(ps))
	{
		return false;
	}
	int tmp = -1;
	for (int i = 0; i < ps->length; i++)
	{
		if (ps->data[i] == val)
		{
			tmp = i;
			break;
		}
	}
	if (tmp == -1)
	{
		return false;
	}
	Del_pos(ps, tmp);
	return true;
}
bool IsEmpty(PSQlist ps)
{
	assert(ps != NULL); 
	return ps->length == 0;
}
bool IsFull(PSQlist ps)
{
	assert(ps != NULL);
	return ps->length == 10;
}
int Get_Length(PSQlist ps)
{
	return ps->length;
}
void Clear(PSQlist ps)
{
	ps->length = 0;
}
void Destory(PSQlist ps)
{
	Clear(ps);
}
void Show(PSQlist ps)
{
	for (int i = 0; i < ps->length; i++)
	{
		printf("%d ", ps->data[i]);
	}
	printf("\n");
}
//扩容1.5和扩容两倍的区别
//1.5倍可以把之前申请的空间再进行利用,而2倍不能
//vs的扩容规则  1.5
//gcc的扩容规则 2
void expland(PSQlist ps, int n)
{
	assert(ps != NULL);
	if (ps->length == ps->capacity)
	{
		ps->capacity *= n;
		elemtype* newdata = (elemtype*)malloc(sizeof(elemtype) * ps->capacity);
		for (int i = 0; i < ps->length; i++)
		{
			newdata[i]= ps->data[i];
		}
	}
}

#include<stdio.h>
#include"sqlist.h"
int main()
{
	SQlist sq;
	Init_SQlist(&sq);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值