数据结构 | C语言动态顺序表的实现

1、Data.h

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDatetype;
typedef struct SeqList
{
	SLDatetype* a;
	int size;
	int capacity;
}SL;

//动态顺序表的创建和销毁
void SLInit(SL* s1);
void SLDestory(SL* s1);

//扩容
void SLCheckCapacity(SL* s1);

//顺序表数据的增删查改插入删除
void SLPushBack(SL* s1, SLDatetype x);
void SLPushBFront(SL* s1, SLDatetype x);
void SLPopBack(SL* s1);
void SLPopFront(SL* s1);
void SLInsert(SL* s1, int pos, SLDatetype x);
void SLErase(SL* s1, int pos);
void SLModify(SL* s1, int pos, SLDatetype x);

int SLFind(SL* s1, SLDatetype x);


void print1(SL* s1);

2、main.cpp

#include"Data.h"
int main()
{
	SL s;
	s.a = NULL;
	SLInit(&s);
	SLPushBack(&s, 1);
	SLPushBack(&s, 2);
	SLPushBack(&s, 3);
	SLPushBack(&s, 4);
	SLPushBack(&s, 5);
	SLPushBack(&s, 5);
	SLPushBFront(&s, 234);
	SLPushBack(&s, 5);
	SLPushBack(&s, 5);
	SLPushBack(&s, 5);
	print1(&s);
	SLPopFront(&s);
	print1(&s);
	SLPopBack(&s);
	SLPopBack(&s);
	SLPopBack(&s);
	print1(&s);
	SLPopBack(&s);
	SLPopBack(&s);
	SLPopBack(&s);
	print1(&s);
	SLPopBack(&s);
	SLPopBack(&s);
	SLPopBack(&s);
	print1(&s);
	SLPopBack(&s);
	SLPopBack(&s);
	SLPopBack(&s);
	print1(&s);
	SLPushBack(&s, 1);
	SLPushBack(&s, 2);
	SLPushBack(&s, 3);
	SLPushBack(&s, 5);
	print1(&s);
	SLInsert(&s, 2, 456);
	print1(&s);
	SLInsert(&s, 2, 46);
	print1(&s);
	SLErase(&s, 4);
	print1(&s);
	printf("%d ",SLFind(&s, 4));
	printf("%d \n",SLFind(&s, 2));
	print1(&s);
	SLModify(&s, 3, 21345);
	print1(&s);

	return 0;
}

3、Data.cpp

#include"Data.h"

void SLInit(SL* s1)
{
	s1->a = (SLDatetype*)malloc(sizeof(SLDatetype) * 4);
	if (s1->a == NULL)
	{
		perror("malloc fail");
	}
	s1->capacity = 4;
	s1->size = 0;
}
void SLDestory(SL* s1)
{
	free(s1->a);
	s1->a = NULL;
	s1->size = 0;
	s1->capacity = 0;
}



void SLCheckCapacity(SL* s1)
{
	if (s1->size == s1->capacity)
	{
		SLDatetype* temp = (SLDatetype*)realloc(s1->a, sizeof(SLDatetype) * s1->capacity * 2);
		if (temp == NULL)
		{
			perror("realloc fail");
		}
		s1->a = temp;
		s1->capacity *= 2;
	}

}



void SLPushBack(SL* s1, SLDatetype x)
{
	SLCheckCapacity(s1);
	s1->a[s1->size++] = x;
}
void SLPushBFront(SL* s1, SLDatetype x)
{
	SLInsert(s1, 0, x);
	/*
	SLCheckCapacity(s1);

	int end = s1->size-1;
	while (end >= 0)
	{
		s1->a[end + 1] = s1->a[end];
		end--;
	}
	s1->a[0] = x;
	s1->size++;
	*/
}
void SLPopBack(SL* s1)
{
	//暴力检查
	//assert(s1->size > 0);
	//温柔的检查
	if(s1->size>0)
		s1->size--;
}
void SLPopFront(SL* s1)
{
	assert(s1->size > 0);
	SLErase(s1, 0);
	/*int start = 0;
	while (start < s1->size - 1)
	{
		s1->a[start] = s1->a[start + 1];
		start++;
	}
	s1->size--;*/
}




void SLInsert(SL* s1, int pos, SLDatetype x)
{
	assert(pos >= 0 && pos <= s1->size);
	SLCheckCapacity(s1);
	int end = s1->size-1;
	while (pos<=end)
	{
		s1->a[end + 1] = s1->a[end];
		end--;
	}
	s1->a[pos] = x;
	s1->size++;
}
void SLErase(SL* s1, int pos)
{
	assert(pos >= 0 && pos < s1->size);
	int start = pos + 1;
	while (start < s1->size)
	{
		s1->a[start - 1] = s1->a[start];
		start++;
	}
	s1->size--;
}

int SLFind(SL* s1, SLDatetype x)
{
	for (int i = 0; i < s1->size; i++)
	{
		if (s1->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}

void SLModify(SL* s1, int pos, SLDatetype x)
{
	assert(pos >= 0 && pos < s1->size);
	s1->a[pos] = x;
}




void print1(SL* s1)
{
	for (int i = 0; i < s1->size; i++)
	{
		printf("%d ", s1->a[i]);
	}
	printf("\n");
}

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值