数据结构 - 链表实例 (线性结构)

三个文件! 

seqlist.h 、 seqlinklist.c  、test1.c


seqlist.h 


#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__


seqlinklist.c




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

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

int  SeqList_Create01(SeqList **handle, 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;

	*handle = ret;
	return 0;
}

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(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->capacity = 0;

	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)
{
	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;
	}


	for (int 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)
{
	TSeqList *tList = NULL;
	tList = (TSeqList *)list;

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



}

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

	if (list == NULL || pos < 0 || pos >= tList->length)
	{
		return NULL;
	}

	ret = (SeqListNode *)tList->node[pos];

	for (int i = pos + 1; i < tList->length; i++)
	{
		tList->node[i - 1] = tList->node[i];
	}
	tList->length--;
	
	return ret;
}


test01.c



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

#include "seqlist.h"

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


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

//
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_Create01(&list, 10);

	//头插法
	//实现了,业务数据 和 链表算法的分离。。。。
	SeqList_Insert(list, (SeqListNode*)&t1, 0);
	SeqList_Insert(list, (SeqListNode*)&t2, 0);
	SeqList_Insert(list, (SeqListNode*)&t3, 0);

	//循环遍历
	for (i = 0; i < SeqList_Length(list); i++)
	{
		Teacher *tmp = (Teacher *)SeqList_Get(list, i);
		if (tmp != NULL)
		{
			printf("tmp:age:%d ", tmp->age);
		}
	}

	printf("\n");
	//循环删除

	for (i = 0; i < SeqList_Length(list); i++)
	{
		SeqList_Delete(list, 0);
	}

	for (i = 0; i < SeqList_Length(list); i++)
	{
		Teacher *tmp = (Teacher *)SeqList_Get(list, i);
		if (tmp != NULL)
		{
			printf("tmp:age:%d ", tmp->age);
		}
	}

	SeqList_Destroy(list);

	system("pause");
}



原文链接: 数据结构 - 链表实例 (线性结构) 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-18.html )

发表评论

0则评论给“数据结构 - 链表实例 (线性结构)”