C语言实现动态顺序表

/***********************************************************************************
xxx.h:
    Copyright (c) , Inc.(2016), All rights reserved.

Purpose: C语言实现动态顺序表

Author: MaJing

Reviser: yyy

Created Time: 2016_9_6
************************************************************************************/

// 动态顺序表
#pragma once
#define __SEQ_LIST__
#ifdef __SEQ_LIST__

typedef int DataType;

#define DEFAULT_CAPICITY 3

typedef struct SeqList
{
	DataType* array;  // 数据块指针
	size_t size;	  // 当前的有效数据个数 
	size_t capicity;  // 容量
}SeqList;

// 初始化/销毁/打印/检查扩容
void InitSeqList(SeqList* pSeq);
void DestorySeqList(SeqList* pSeq);
void PrintSeqList(SeqList* pSeq);
void CheckCapicity(SeqList* pSeq);

// 头插/头删/尾插/尾删 (主要体现插入数据的时候进行自动扩容处理)
void PushBack(SeqList* pSeq, DataType x);
void PopBack(SeqList* pSeq);
void PushFront(SeqList* pSeq, DataType x);
void PopFront(SeqList* pSeq);

// 插入/修改/删除/查找(ps:逻辑均与静态顺序表大致相同,所以未实现)
void Insert(SeqList* pSeq, size_t index, DataType x);
void Modified (SeqList* pSeq, size_t index, DataType x);
void Erase(SeqList* pSeq, size_t index);
int Find(SeqList* pSeq, DataType x, size_t index);
bool Remove(SeqList* pSeq, DataType x);
void RemoveAll(SeqList* pSeq, DataType x);
//...

//
// 实现

void InitSeqList(SeqList* pSeq)
{
	assert(pSeq);
	pSeq->array = (DataType*)malloc(sizeof(DataType)*DEFAULT_CAPICITY);
	pSeq->size = 0;
	pSeq->capicity = DEFAULT_CAPICITY;
}

void PrintSeqList(SeqList* pSeq)
{
	int i = 0;
	for (; i < pSeq->size; ++i)
	{
		printf("%d ", pSeq->array[i]);
	}

	printf("\n", pSeq->array[i]);
}

void CheckCapicity(SeqList* pSeq)
{
	if (pSeq->size == pSeq->capicity)
	{
		// ps:在这里用realloc也可以,且更简洁

		// 创建新空间,拷贝数据
		DataType* tmp = (DataType*)malloc(pSeq->capicity * 2 *sizeof(DataType));
		memcpy(tmp, pSeq->array, sizeof(DataType) * pSeq->size);

		// 释放原空间,更新指针
		free(pSeq->array);
		pSeq->array = tmp;
		pSeq->capicity = pSeq->capicity * 2;
	}
}

void DestorySeqList(SeqList* pSeq)
{
	if (pSeq)
	{
		free(pSeq->array);
		pSeq->size = 0;
		pSeq->capicity = 0;
	}
}

void PushBack(SeqList* pSeq, DataType x)
{
	assert(pSeq);
	CheckCapicity(pSeq);

	pSeq->array[pSeq->size++] = x;
}

void PopBack(SeqList* pSeq)
{
	assert(pSeq);
	if (pSeq->size == 0)
	{
		printf("SeqList Is Empty\n");
	}

	--pSeq->size;
}

void PushFront(SeqList* pSeq, DataType x)
{
	int i = pSeq->size;
	assert(pSeq);
	CheckCapicity(pSeq);

	for (; i > 0; --i)
	{
		pSeq->array[i] = pSeq->array[i - 1];
	}

	pSeq->array[0] = x;
	pSeq->size++;
}

#endif // __SEQ_LIST__

void Test1()
{
	SeqList s;
	InitSeqList(&s);

	PushBack(&s, 1);
	PushBack(&s, 2);
	PushBack(&s, 3);
	PushBack(&s, 4);

	PrintSeqList(&s);

	PopBack(&s);
	PopBack(&s);
	PopBack(&s);
	PopBack(&s);
	PopBack(&s);

	DestorySeqList(&s);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值