数据结构顺序线性表


数据结构顺序线性表表实现



/*****************************************************
  > File Name   : test.c
  > Author      : xboss
  > Mail        : 2366006417@qq.com 
  > Created Time: 2020年06月13日 星期六 15时14分59秒
 *****************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>


typedef void SeqList;
typedef void SeqListNode;
typedef unsigned int ElemType;  //地址(指针指向数据类型)

typedef struct _tag_SeqList
{
	int length;     	//顺序线性表的当前长度
	int capacity;   	//当前分配的存储容量(以sizeof(ElemType)为单位)
	ElemType **node;     //存储空间基地址(数据元素的首地址)(二级指针相当于指针数组int *a[])

}TSeqList;          	//顺序线性表的类型定义


/***************************************
function :顺序线性表创建
parameter:int capacity(分配的容量)
return   :顺序线性表数据对象的首地址
 ****************************************/
SeqList* SeqList_Create(int capacity)
{
	int ret = 0;
	TSeqList *temp = (TSeqList *)malloc(sizeof(TSeqList));
	if(temp == NULL)
	{	
		ret = -1;
		printf("SeqList_Create error: %d\r\n",ret);
		return NULL;
	}
	memset(temp,0,sizeof(TSeqList));   //动态分配后先赋值,置0

	//根据capacity值去动态分配节点内存空间
	temp->node = (ElemType **)malloc(sizeof(ElemType *)*capacity);
	if(temp->node == NULL)
	{	
		ret = -2;
		printf("SeqList_Create error: %d\r\n",ret);
		return NULL;
	}
	temp->capacity = capacity;
	temp->length   = 0;
	return temp;
}

/***************************************
function :顺序线性表删除
parameter:顺序线性表数据对象的首地址
return   :无
 ****************************************/
void SeqList_Destory(SeqList *list)
{
	TSeqList *tlist = NULL;
	if(list == NULL)
	{
		return ;	
	}
	tlist = (TSeqList *)list;
	if(tlist->node != NULL)
	{
		free(tlist->node);
	}
	free(tlist);
}

/***************************************
function :顺序线性表清空(清空链表,回到初始状态)(长度清空)
parameter:顺序线性表数据对象的首地址
return   :无
 ****************************************/
void SeqList_Clear(SeqList *list)
{
	TSeqList *tlist = NULL;
	if(list == NULL)
	{
		return ;	
	}
	tlist = (TSeqList *)list;
	tlist->length = 0;
}


/***************************************
function :顺序线性表分配的存储容量(以sizeof(ElemType)为单位)查询
parameter:顺序线性表数据对象的首地址
return   :返回顺序线性表的分配的存储容量,失败返回-1
 ****************************************/
int SeqList_Capacity(SeqList *list)
{
	TSeqList *tlist = NULL;
	if(list == NULL)
	{
		return -1;	
	}
	tlist = (TSeqList *)list;
	return tlist->capacity;
}


/***************************************
function :顺序线性表的长度计算
parameter:顺序线性表数据对象的首地址
return   :返回顺序线性表的长度,失败返回-1
 ****************************************/
int SeqList_Length(SeqList *list)
{
	TSeqList *tlist = NULL;
	if(list == NULL)
	{
		return -1;	
	}
	tlist = (TSeqList *)list;
	return tlist->length;
}


/***************************************
function :向顺序线性表指定位置插入元素
parameter:顺序线性表数据对象的首地址(SeqList *list),元素数据(SeqListNode *node),位置(pos)
return   :成功返回0,失败返回-1
 ****************************************/
int SeqList_Insert(SeqList *list,SeqListNode *node,int pos)
{
	int ret = 0;
	int i = 0;
	TSeqList *tlist = NULL;
	if(list == NULL || node == NULL || pos < 0 )
	{
		ret = -1;
		printf("SeqList_Insert err:%d\r\n",ret);
		return ret;	
	}
	tlist = (TSeqList *)list;
	//判断顺序线性表是不是满了
	if(tlist->length >= tlist->capacity)
	{
		ret = -2;
		printf("SeqList_Insert err:%d\r\n",ret);
		return ret;		
	}
	//容错处理(例如:当前长度为6,容量为10,用户在8处插入元素需要做容错处理)
	if(pos >= tlist->length)
	{
		pos = tlist->length;
	}
	//***元素后移
	for(i = tlist->length;i > pos;i--)
	{
		tlist->node[i] = tlist->node[i-1]; 
	}
	//在pos位置插入元素
	tlist->node[i] = node;
	//长度加1
	tlist->length++;
	return 0;
}


/***************************************
function :顺序线性表每个位置的值查询
parameter:顺序线性表数据对象的首地址,查询的位置pos
return   :返回顺序线性表对应位置的地址,失败返回NULL
 ****************************************/
SeqListNode* SeqList_Get(SeqList *list,int pos)
{
	int ret = 0;
	TSeqList *tlist = NULL;
	if(list == NULL || pos < 0 )
	{
		ret = -1;
		printf("SeqList_Get err:%d\r\n",ret);
		return NULL;	
	}
	tlist = (TSeqList *)list;
	return (SeqListNode*)tlist->node[pos];	
}


/***************************************
function :删除顺序线性表指定位置的值
parameter:顺序线性表数据对象的首地址,删除的位置pos
return   :返回顺序线性表对应位置的地址,失败返回NULL
 ****************************************/
SeqListNode* SeqList_Delete(SeqList *list,int pos)
{
	int ret = 0;
	int i = 0;
	TSeqList *tlist = NULL;
	SeqListNode *temp = NULL;
	if(list == NULL || pos < 0 )
	{
		ret = -1;
		printf("SeqList_Delete err:%d\r\n",ret);
		return NULL;	
	}
	tlist = (TSeqList *)list;
	//将删除位置的地址保留
	temp = (SeqListNode *)tlist->node[pos];
	//元素前移
	for(i = pos+1;i<tlist->length;i++)
	{
		tlist->node[i-1] = tlist->node[i]; 
	}
	//长度减1
	tlist->length--;
	return temp;	
}


typedef struct teacher
{
	int age;
	int high;

}Teacher;

int main(void)
{
	Teacher t1,t2,t3,t4;
	int ret = 0;
	int i = 0;
	t1.age = 22;
	t1.high = 110;
	t2.age = 13;
	t2.high = 130;
	t3.age = 14;
	t3.high = 140;
	t4.age = 16;
	printf("t1=%p\r\n",&t1);
	printf("t2=%p\r\n",&t2);
	printf("t3=%p\r\n",&t3);

	SeqList *list = NULL;
//创建顺序线性表
	list = SeqList_Create(5); 
	if(list == NULL)
	{
		printf("SeqList_Create\r\n");
		exit(1);

	}
//向顺序线性表插入数据
	ret = SeqList_Insert(list,&t1,0);
	ret = SeqList_Insert(list,&t2,0);
	ret = SeqList_Insert(list,&t3,0);
	ret = SeqList_Insert(list,&t4,2);
//遍历顺序线性表中的元素
	for(i = 0;i<SeqList_Length(list);i++)
	{
		Teacher *temp = (Teacher *)SeqList_Get(list,i);
		if(temp == NULL)
		{
			printf("error");
			exit(1);

		}
		printf("temp->age= %d\r\n",temp->age);
//		printf("temp->high= %d\r\n",temp->high);

	}
//删除顺序线性表中的元素
	SeqList_Delete(list,0);
	Teacher *temp = (Teacher *)SeqList_Get(list,0);
	printf("temp->age= %d\r\n",temp->age);
	return 0;
}

代码运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值