表(用数组实现)原码展示(C语言)

表(用数组实现)原码分享(C语言)

1、头文件

(ArrayList.h)

#pragma once
/*
* 1 实现一个表的数据结构,可以进行插入、查询、删除、判空、获取大小等操作
* 2 底层使用数组存储
*/

#include <stdio.h>
#include <stdlib.h>		//malloc
#include <stdbool.h>	//bool类型

#define MAX_SIZE 100


//函数原型声明
struct List* CreateList();

void InsertFirst(struct List* p, int x);
void InsertLast(struct List* p, int x);

int Find(struct List* p, int x);

int FindKth(struct List* p, int position);

void Delete(struct List* p, int x);

bool IsEmpty(struct List* p);

int Size(struct List* p);

void PrintList(struct List* p);

struct List
{
	int element[MAX_SIZE];	//表的元素都存放在数组中
	int length;				//表中元素的长度(即个数),实际上是存储的有效个数
};

2、函数实现

###2.1 CreateList.c

#include "ArrayList.h"

struct List* CreateList()
{
	struct List* p = malloc(sizeof(struct List));
	if (p == NULL) {
		printf("内存空间不足,创建表头失败。\n");
		exit(1);
	}
	p->length = 0;
	return p;
}

2.2 InsertFirst.c

#include "ArrayList.h"

void InsertFirst(struct List* p, int x)
{
	if (p->length-1 >= MAX_SIZE) {
		printf("数组空间不足\n");
		return;
	}
	for (int i = p->length; i > 0; i--) {
		p->element[i] = p->element[i - 1];
	}
	p->element[0] = x;
	p->length++;
}

2.3 InsertLast.c

#include "ArrayList.h"

void InsertLast(struct List* p, int x)
{
	if (p->length - 1 >= MAX_SIZE) {
		printf("数组空间不足");
		return;
	}
	p->element[p->length] = x;
	p->length++;
}

2.4 Find.c

#include "ArrayList.h"

int Find(struct List* p, int x)
{
	for (int i = 0; i < p->length; i++) {
		if (x == p->element[i]) {
			return i;
		}
	}
	return -1;
}

2.5 FindKth.c

#include "ArrayList.h"

int FindKth(struct List* p, int position)
{
	if (position > p->length || position < 0) {
		printf("超出表达范围\n");
		return 0;
	}
	return p->element[position - 1];
}

2.6 Delete.c

#include "ArrayList.h"

void Delete(struct List* p, int x)
{
	for (int i = 0; i < p->length; i++) {
		if (p->element[i] == x) {
			for (int j = i; j < p->length - 1; j++) {
				p->element[i] = p->element[i + 1];
			}
			p->length--;
			return;
		}
	}
	printf("没有找到你想删除的元素");
}

2.7 IsEmpty.c

#include "ArrayList.h"

bool IsEmpty(struct List* p)
{
	return p->length == 0;
}

2.8 Size.c

#include "ArrayList.h"

int Size(struct List* p)
{
	return p->length;
}

2.9 PrintList.c

#include "ArrayList.h"

void PrintList(struct List* p)
{
	printf("该表的元素分别为:");
	for (int i = 0; i < p->length; i++) {
		printf("%d ", p->element[i]);
	}
	printf("\n");
}

3、主函数

(main.c)

#include "ArrayList.h"

int main()
{
	int index;
	int element;

	//创建一个空表
	struct List* list = CreateList();

	printf("List isEmpty:%d\n", IsEmpty(list));
	printf("--------------------------\n");

	//头插
	InsertFirst(list, 1);
	InsertFirst(list, 2);

	PrintList(list);
	printf("--------------------------\n");

	//尾插
	InsertLast(list, 3);
	InsertLast(list, 4);
	
	PrintList(list);
	printf("--------------------------\n");

	printf("List isEmpty:%d\n", IsEmpty(list));
	printf("--------------------------\n");

	//查找值为3的元素,如果找到了返回数组角标,找不到返回-1
	index = Find(list, 3);
	if (index == -1) {
		printf("not find...\n");
	}
	else {
		printf("find element index: %d\n", index);
	}

	//查找第2个元素,返回元素的值
	element = FindKth(list, 2);
	printf("find element:%d\n", element);
	printf("--------------------------\n");

	//删除值为3的元素
	Delete(list, 3);
	PrintList(list);
	printf("--------------------------\n");

	//获取表的大小
	printf("List szie is:%d\n", Size(list));
	printf("--------------------------\n");

	return 0;
}

4、写在后面

以上展示了表中常见的几种操作,如有纰漏、补充,欢迎私信、评论区友好指正、讨论。

为方便大家学习,原码已放在GitHub,欢迎下载link(文件名:表(用数组来实现))。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值