线性表的实现(一)顺序存储结构

1、基本概念


线性表的实现(一)顺序存储结构_第1张图片


2、设计与实现

接口文件:

#ifndef  __MY_SEQLIST_H__ 
#define __MY_SEQLIST_H__

typedef void SeqList;
typedef void SeqListNode;


SeqList* SeqList_Create(int capacity);

int  SeqList_Create01(SeqList **handle, int capacity);

void SeqList_Destroy(SeqList* list);

void SeqList_Clear(SeqList* list);

int SeqList_Length(SeqList* list);

int SeqList_Capacity(SeqList* list);

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);

SeqListNode* SeqList_Get(SeqList* list, int pos);

SeqListNode* SeqList_Delete(SeqList* list, int pos);


#endif  //__MY_SEQLIST_H__

实现文件:

typedef struct _tag_SeqList
{
	int capacity;
	int length;
	unsigned int *node; //unsigned int array[capacity]
}TSeqList;


SeqList* SeqList_Create_01(int capacity)
{
	TSeqList *ret = NULL;
	if (capacity < 0)
	{
		return NULL;
	}
	ret = (TSeqList *)malloc(sizeof(TSeqList));
	if (ret == NULL)
	{
		return NULL;
	}
	memset(ret, 0, sizeof(sizeof(TSeqList)));
	ret->node = (unsigned int *)malloc(sizeof(unsigned int )*capacity);
	if (ret ->node == NULL)
	{
		return NULL;
	}
	memset(ret->node, 0, sizeof(unsigned int )*capacity);
	ret->capacity = capacity;
	ret->length = 0;
	return ret;
}

SeqList* SeqList_Create(int capacity)
{
	TSeqList *ret = NULL;
	if (capacity < 0)
	{
		return NULL;
	}
	ret = (TSeqList *)malloc(sizeof(TSeqList) + sizeof(unsigned int )*capacity );
	if (ret == NULL)
	{
		return NULL;
	}
	memset(ret, 0, sizeof(sizeof(TSeqList)) + sizeof(unsigned int )*capacity );
	ret->node = (unsigned int *)(ret +1); //ret向后跳sizeof(TSeqList)
	ret->capacity = capacity;
	ret->length = 0;
	return ret;
}

void SeqList_Destroy(SeqList* list)
{
	if (list == NULL)
	{
		return ;
	}
	free(list);
	return ;
}

//链表清零 。。。长度为零
void SeqList_Clear(SeqList* list)
{
	TSeqList *tList = NULL; 

	if (list == NULL)
	{
		return ;
	}
	tList  = (TSeqList *)list;
	tList->length = 0;
	return ;
}

int SeqList_Length(SeqList* list)
{
	TSeqList *tList = NULL; 
	tList = (TSeqList *)list;
	if (list == NULL)
	{
		return -1;
	}
	
	return tList->length;
}

//线性表的容量和线性表长度是不一样的
int SeqList_Capacity(SeqList* list)
{
	TSeqList *tList = NULL; 
	tList = (TSeqList *)list;
	if (list == NULL)
	{
		return -1;
	}

	return tList->capacity;
}

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
	int i = 0;
	TSeqList *tList = NULL; 
	tList  = (TSeqList *)list;

	if (list == NULL || node == NULL) 
	{
		return -1;
	}

	//查看是不是满了
	if (tList->length >= tList->capacity)
	{
		return -2;
	}

	//位置错误判断
	if (pos<0 || pos>=tList->capacity)
	{
		return -3;
	}

	//优化的容错。。。
	if (pos >=tList->length)
	{
		pos = tList->length;
	}

	//插入算法
	//从pos位置处开始,把数组后面的元素依此后移
	for(i=tList->length; i>pos; i--)
	{
		//把前的元素往后移
		tList->node[i] = tList->node[i-1];
	}
	//循环跳出以后,pos正好是,要出入的位置
	tList->node[pos] = (unsigned int)node;
	tList->length ++;
	return 0;
}

SeqListNode* SeqList_Get(SeqList* list, int pos)
{

	SeqListNode *ret = NULL;
	TSeqList *tList = NULL;
	tList = (TSeqList *)list;
	if (list == NULL || pos<0 || pos>=tList->length)
	{
		return NULL;
	}
	ret = (SeqListNode*)tList->node[pos];
	return ret;
}

SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
	int					i;
	TSeqList				*tList = NULL;
	SeqListNode			*ret = NULL; 
	tList = (TSeqList *)list;

	if (list==NULL || pos<0 || pos>=tList->length)
	{
		return NULL;
	}
	
	//赋给a3之前,要把a3元素缓存下来
	ret = (SeqListNode *)tList->node[pos];
	//删除算法
	for (i=pos+1; ilength; i++)
	{
		tList->node[i-1] = tList->node[i];
	}
	tList->length --;

	return ret;
}

测试文件:

typedef struct _Teacher
{
	char name[64];
	int age;
}Teacher;

int main()
{
	int i = 0;
	SeqList *list = NULL;

	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;

	list =  SeqList_Create(10);

	SeqList_Insert(list, (SeqListNode*)&t1, 0);
	SeqList_Insert(list, (SeqListNode*)&t2, 0);
	SeqList_Insert(list, (SeqListNode*)&t3, 0);

	//循环遍历
	for (i=0; iage);
		}
	}

	//循环删除

	for (i=0; i

3、优点和缺点

优点:

无需为线性表中的逻辑关系增加额外的空间

可以快速的获取表中合法位置的元素

缺点:

插入和删除操作需要移动大量元素

当线性表长度变化较大时难以确定存储空间的容量



你可能感兴趣的:(数据结构,线性表,指针)