数据结构——顺序表

三个文件

第一个文件

test.c

#include <stdio.h>
#include "seqList.h"

void testSeqList()
{
	SL sl;
	sepListInit(&sl);//结构体的初始化

	//插入数据
	int n = 0;
	scanf("%d", &n);
	int i = 0;
	for (i = 0; i < n; i++)
	{
		int m = 0;
		scanf("%d", &m);
		sepListPushBack(&sl, m);
	}

	sepListPrint(&sl);

	sepListFree(&sl);//动态内存的释放
}


int main()
{
	testSeqList();

	return 0;
}

第二个文件

seqList.h

#pragma once
#define N 1000
typedef int dataType;

// 动态顺表
typedef struct sepList
{
	dataType* pstr;
	int size;      // 存储的数据大小
	int capacity;  // 数组的空间容量大小
}SL;

// 接口函数

// 顺序表的初始化
void sepListInit(SL* pa);

//顺序表的打印
void sepListPrint(SL* pa);

//顺序表的动态内存的释放
void sepListFree(SL* pa);

// 顺序表的头插
void sepListPushFront(SL* pa, dataType n);

// 顺序表的尾插
void sepListPushBack(SL* pa, dataType n);

// 顺序表的的头删
void sepListPopFront(SL* pa);

//顺序表的尾删
void sepListPopback(SL* pa);


第三个文件

seqList.c

#include "seqList.h"
#include <stdlib.h>
#include <stdio.h>

void sepListInit(SL* pa)
{
	pa->pstr = NULL;
	pa->size = 0;
	pa->capacity = 0;
}

void sepListPushBack(SL* pa, dataType n)
{
	if (pa->size == pa->capacity)
	{
		int newCapacity = pa->capacity == 0 ? 4 : pa->capacity * 2;
		//内存扩容为一般去2倍数
		dataType* ret = (dataType*)realloc(pa->pstr, newCapacity * sizeof(dataType));
		if (NULL != ret)
		{
			pa->pstr = ret;
			pa->capacity = newCapacity;
		}
	}
	pa->pstr[pa->size] = n;
	pa->size++;
}

void sepListPrint(SL* pa)
{
	int i = 0;
	for (i = 0; i < pa->size; i++)
	{
		printf("%d ", pa->pstr[i]);
	}
}

void sepListFree(SL* pa)
{
	free(pa->pstr);
	pa->pstr = NULL;
	pa->size = pa->capacity = 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

甘-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值