顺序表的代码实现

文章目录


一、顺序表简介

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般可以分为:
1. 静态顺序表:使用定长数组存储元素。

#define N 100
typedef int ElemType;
typedef struct SeqList{
	ElemType data[N];
	int size;
	int capacity;
}SL;

2. 动态顺序表:使用动态开辟的数组存储。

typedef int ElemType;
typedef struct SeqList{
	ElemType *data;
	int size;
	int capacity;
}SL;

 静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间大小,所以下面我们实现动态顺序表

2.代码实现

.h文件

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int ElemType;
typedef struct SeqList{
	ElemType *data;
	int size;
	int capacity;
}SL;
void SLInit(SL* p); //顺序表初始化
void SLPushBack(SL *p, ElemType x); //尾插
void SLPushFront(SL *p, ElemType x); //头插
void SLPopBack(SL *p);  //尾删
void SLPopFront(SL *p); //头删 
int SLFind(SL *p,ElemType x);   //查找并返回下标
void SLInsert(SL *p, int pos, ElemType x);    //在指定位置插入
void SLErase(SL *p, int pos);   //删除pos位置数据
void SLPrintf(SL *p);  //打印
int SLSize(SL *p);  //返回元素个数
bool SLEmpty(SL *p); //判空
void SLDestroy(SL *p);  //销毁

各个函数实现.c文件

#include"SeqList.h"
void SLInit(SL* p){      //顺序表初始化
	p->data = (ElemType *)malloc(sizeof(ElemType)* 10);
	if (p->data == NULL){
		printf("malloc fail\n");
		exit(-1);
	}
	p->size = 0;
	p->capacity = 10;
}
void SLCheckCapacity(SL *p){   //判断是否满了
	if (p->size == p->capacity){         
		ElemType *new = realloc(p->data, sizeof(ElemType)*(p->capacity + 5));
		if (new == NULL){
			printf("realloc fail");
			exit(-1);
		}
		p->data = new;
		p->capacity += 5;
	}
}
void SLPushBack(SL *p, ElemType x){      //尾插
	SLCheckCapacity(p);
	p->data[p->size] = x;
	p->size++;
}
void SLPushFront(SL *p, ElemType x){      //头插
	SLCheckCapacity(p);
	for (int i = p->size; i > 0; i--){   //数据后移
		p->data[i] = p->data[i - 1];
	}
	p->data[0] = x;
	p->size++;
}
void SLPopBack(SL *p){   //尾删
	if (p->size == 0){
		printf("该表暂无内容,无法删除\n");
		return;
	}
	p->size--;
}
void SLPopFront(SL *p){   //头删
	if (p->size == 0){
		printf("该表暂无内容,无法删除\n");
		return;
	}
	for (int i = 0; i < p->size-1; i++){     //数据前移
		p->data[i] = p->data[i + 1];
	}
	p->size--;
}
int SLFind(SL *p,ElemType x){  //查找并返回下标
	int i;
	for ( i = 0; i < p->size; i++){
		if (x == p->data[i]){
			return i;
		}
	}
	printf("没找到该元素\n");
}
void SLInsert(SL *p, int pos, ElemType x){   //在指定位置插入
	SLCheckCapacity(p);    //先判断是否满了
	if (pos<0 || pos>p->size+1){   //判断pos的值是否合法
		printf("请在合理的范围插入\n");
		return;
	}
	for (int i =p->size; i >pos-1; i--){  //数据后移
		p->data[i] = p->data[i-1];
	}
	p->data[pos-1] = x;
	p->size++;
}
void SLErase(SL *p, int pos){   //删除pos位置数据
	if (p->size == 0){
		printf("该表暂无内容,无法删除\n");
		return;
	}
	if (pos<0 || pos>p->size){   //判断pos的值是否合法
		printf("请在合理的范围插入\n");
		return;
	}
	for (int i =pos-1; i<p->size-1; i++){   //数据前移
		p->data[i] = p->data[i+1];
	}
	p->size--;
}
int SLSize(SL *p){   //返回表中元素个数
	return p->size;
}
bool SLEmpty(SL *p){ //判空
	return p->size == 0;
}
void SLDestroy(SL *p){  //销毁
	free(p->data);
	p->data = NULL;
	p->size = p->capacity = 0;
}
void SLPrintf(SL *p){    //打印
	for (int i = 0; i < p->size; i++){
		printf("%d ", p->data[i]);
	}
	printf("\n");
}

总结

以上就是今天要讲的内容,本文简单介绍了单链表的代码实现

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值