C语言实现一般线性表的链式存储

C语言实现一般线性表的顺序存储,以下是在VS2013编译通过的,欢迎讨论。有关数据结构的概念可看我的另一篇文章http://blog.csdn.net/linuxlsl/article/details/45649655

一、头文件linklist.h

#ifndef _LINKLIST_H_
#define _LINKLIST_H_

typedef void LinkList;

typedef struct _tag_LinkListNode
{
	struct _tag_LinkListNode* next;
}LinkListNode;

LinkList* LinkList_Create();

void LinkList_Destroy(LinkList* list);

void LinkList_Clear(LinkList* list);

int LinkList_Length(LinkList* list);

int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);

LinkListNode* LinkList_Get(LinkList* list, int pos);

LinkListNode* LinkList_Delete(LinkList* list, int pos);

#endif

二、实现文件linklist.c

#include
#include "stdlib.h"
#include "string.h"

#include "linklist.h"

typedef struct _tag_LinkList
{
	//表头,保存必要的信息
	LinkListNode header;
	int length;
}TLinkList;

LinkList* LinkList_Create()
{
	TLinkList *ret = (TLinkList *)malloc(sizeof(TLinkList));
	if (ret == NULL)	
		return NULL;
	ret->header.next = NULL;
	ret->length = 0;
	return ret;
}

void LinkList_Destroy(LinkList* list)
{
	if (list == NULL)	
		return ;	
	free(list);
	return ;
}

void LinkList_Clear(LinkList* list)
{
	TLinkList *tList =NULL;
	
	if (list == NULL)	
		return ;

	tList = (TLinkList *)list;
	tList->length = 0;
	tList->header.next = NULL;
	return ;
}

int LinkList_Length(LinkList* list)
{
	TLinkList *tList = (TLinkList *)list;
	if (tList == NULL)	
		return -1;
	
	return tList->length;
}

int LinkList_Insert(LinkList* list, LinkListNode* node, int pos)
{
	int i = 0;

	TLinkList *tList  = NULL;
	LinkListNode *current = NULL;

	tList = (TLinkList *)list;
	//准备环境让辅助指针变量 指向链表头节点
	current = &tList->header;
	for (i=0; inext!=NULL); i++)
	{
		current = current->next;
	}

	//让node节点链接后续链表
	node->next = current->next ;
	//让前边的链表。链接node
	current->next = node;
	tList->length ++;	
	return 0;
}

LinkListNode* LinkList_Get(LinkList* list, int pos)
{
	int i = 0;

	TLinkList *tList  = NULL;
	LinkListNode *current = NULL;
	LinkListNode *ret = NULL;
	tList = (TLinkList *)list;

	if (list == NULL || pos <0 ||pos>=tList->length)	
		return NULL;
	
	//准备环境让辅助指针变量 指向链表头节点
	current = &tList->header;
	for (i=0; inext!=NULL); i++)
	{
		current = current->next;
	}
	ret = current->next;

	return ret;
}

LinkListNode* LinkList_Delete(LinkList* list, int pos)
{
	int i = 0;

	TLinkList *tList  = NULL;
	LinkListNode *current = NULL;
	LinkListNode *ret = NULL;
	tList = (TLinkList *)list;

	if (list == NULL || pos <0 ||pos>=tList->length)
	{
		return NULL;
	}
	//准备环境让辅助指针变量 指向链表头节点
	current = &tList->header;
	for (i=0; inext!=NULL); i++)
	{
		current = current->next;
	}
	ret = current->next;

	//删除算法
	current->next =ret->next;
	tList->length--;

	return ret;
}

三、测试文件main.c

#include
#include "stdlib.h"
#include "string.h"
#include "linklist.h"

typedef struct 
{
	LinkListNode node;
	int x;
	int y;	
}Point;

int main()
{
	Point		p1,p2, p3;
	int			length, i = 0;

	LinkList		*list = NULL;
	p1.x = 0;
	p1.y = 0;
	p2.x = 1;
	p2.y = 1;
	p3.x = -1;
	p3.y = -1;


	list = LinkList_Create();

	length = LinkList_Length(list);

	//业务节点是Point,和算法分开
	LinkList_Insert(list, (LinkListNode *)&p1, LinkList_Length(list));
	LinkList_Insert(list, (LinkListNode *)&p2, LinkList_Length(list));
	LinkList_Insert(list, (LinkListNode *)&p3, LinkList_Length(list));

	//遍历链表 
	for (i=0; ix, tmp->y);
		}
	}
	printf("\n");
	while(LinkList_Length(list) > 0)
	{
		Point *tmp = (Point *)LinkList_Delete(list, 0);
		if (tmp != NULL)
		{
			printf("P(%d,%d)\n", tmp->x, tmp->y);
		}
	}
	LinkList_Destroy(list);
	getchar();
	return 0;
}

四、运行结果
C语言实现一般线性表的链式存储_第1张图片



你可能感兴趣的:(C/C++数据结构与算法)