顺序表的实现

1.seqlist.h

#ifndef _SEQLIST_H_
#define _SEQLIST_H_
#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct
{
	int data[N];
	int last;//last代表的是数组中最后一个有效元素的下标
}seqlist_t;
//1.创建一个空的顺序表
seqlist_t* CreateEpSeqlist();//返回的是申请空间的首地址
//2.向顺序表的指定位置插入数据
int InsertIntoSeqlist(seqlist_t* p, int post, int data);//post第几个位置,data插入的数据
//3.遍历顺序表sequence 顺序 list 表
void ShowSeqlist(seqlist_t* p);
//4.判断顺序表是否为满,满返回1 未满返回0
int IsFullSeqlist(seqlist_t* p);
//5.判断顺序表是否为空
int IsEpSeqlist(seqlist_t* p);
//6.删除顺序表中指定位置的数据post删除位置
int DeletePostSeqlist(seqlist_t* p, int post);
//7.清空顺序表
void ClearSeqList(seqlist_t* p);
//8.修改指定位置的数据
int ChangePostSeqList(seqlist_t* p, int post, int data);//post被修改的位置,data修改成的数据
//9.查找指定数据出现的位置
int SearchDataSeqList(seqlist_t* p, int data);//data代表被查找的数据
void CombineAB(seqlist_t* pa, seqlist_t* pb);

#endif

seqlist.c

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

//1.创建一个空的顺序表
seqlist_t* CreateEpSeqlist()//返回的是申请空间的首地址
{
	//动态申请一块空间
	seqlist_t* p = (seqlist_t*)malloc(sizeof(seqlist_t));
	if (p == NULL) {
		printf("error\n");
		return NULL;
	}
	p->last = -1;//空
	return p;
}
//4.判断顺序表是否为满,满返回1 未满返回0
int IsFullSeqlist(seqlist_t* p)
{
	return p->last + 1 == N;
}
//2.向顺序表的指定位置插入数据
int InsertIntoSeqlist(seqlist_t* p, int post, int data)//post第几个位置,data插入的数据
{
	//1、容错判断:小于最小,大于最大,满啦都不能插入
	if (post<0 || post>p->last + 1 || IsFullSeqlist(p)) {
		printf("error\n");
		return -1;//错误返回
	}
	//2、last~post从后往前整体往后移动一位
	int i;
	for (i = p->last; i >= post; i--) {
		p->data[i + 1] = p->data[i];
	}
	//3、插入数据
	p->data[post] = data;
	//4、p->last+1
	p->last++;
	return 0;
}
//3.遍历顺序表sequence 顺序 list 表
void ShowSeqlist(seqlist_t* p)
{
	int i;
	for (i = 0; i <= p->last; i++) {
		printf("%d ", p->data[i]);
	}
	printf("\n");
}

//5.判断顺序表是否为空
int IsEpSeqlist(seqlist_t* p)
{
	return p->last == -1;
}
//6.删除顺序表中指定位置的数据post删除位置
int DeletePostSeqlist(seqlist_t* p, int post)
{
	//1、容错判断
	if (post > p->last || post < 0 || IsEpSeqlist(p)) {
		printf("error\n");
		return -1;
	}
	//2、post+1~last往前移动一个位置,覆盖删除
	int i;
	for (i = post + 1; i <= p->last; i++) {
		p->data[i - 1] = p->data[i];
	}
	//有效元素-1
	p->last--;
	return 0;
}
//7.清空顺序表(清空:访问不到,内存当中还有  销毁:内存清空)
void ClearSeqList(seqlist_t* p)
{
	p->last = -1;

}
//8.修改指定位置的数据
int ChangePostSeqList(seqlist_t* p, int post, int data)//post被修改的位置,data修改成的数据
{
	//1、容错判断
	if (post<0 || post>p->last) {
		printf("error\n");
		return -1;
	}
	//2.修改指定位置数据
	p->data[post] = data;
	return 0;
}
//9.查找指定数据出现的位置
int SearchDataSeqList(seqlist_t* p, int data)//data代表被查找的数据
{
	int i;
	for (i = 0; i <= p->last; i++) {
		if (p->data[i] == data) {
			return i;
		}
	}
	return -1;
}
//合并表
void CombineAB(seqlist_t* pa, seqlist_t* pb) {
	int i;
	for (i = 0; i <= pb->last; i++) {//遍历pb表
		if (SearchDataSeqList(pa, pb->data[i]) == -1) {//没找到
			InsertIntoSeqlist(pa, pa->last + 1, pb->data[i]);//将不重复的数据插入pa
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值