链表学习笔记--链式链表

接下来介绍链式链表

链式链表则有点麻烦, 需要在用户自己的数据结构中加入节点数据类型。

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

其设计的思想便是通过数据结构头部的 LinkListNode 字段来连接成链表,而无需在乎数据结构LinkListNode之后数据是如何组成的

#ifndef _MYLINKLIST_H_
#define _MYLINKLIST_H_
#include<stdio.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

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

typedef struct _tag_LinkList
{
	//保存头节点
	LinkListNode header;
	int lenght;
}TLinkList;


LinkList* LinkList_Create()
{
	TLinkList *ret = (TLinkList *)malloc(sizeof(TLinkList));
	if (ret == NULL)
	{
		return NULL;
	}

	memset(ret, 0, sizeof(TLinkList));

	ret->header.next = NULL;
	ret->lenght = 0;
	return ret;
}

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

void LinkList_Clear(LinkList* list)
{
	TLinkList *tList = NULL;
	tList = (TLinkList*)list;
	if (tList == NULL)
	{
		return;
	}
	tList->lenght = 0;
}

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

int LinkList_Insert(LinkList* list, LinkListNode* node, int pos)
{
	TLinkList *tList = NULL;
	LinkListNode *current = NULL;//辅助指针
	int i = 0;

	tList = (TLinkList*)list;
	if (tList == NULL || node == NULL || pos < 0)
	{
		return -1;
	}

	if (pos > tList->lenght)
	{
		pos = tList->lenght;
	}

	//移动辅助指针变量到要插入节点的位置
	current = &(tList->header);
	for (i = 0; i < pos && current != NULL ; i++)
	{
		current = current->next;
	}

	//将节点插入
	node->next = current->next;
	current->next = node;

	tList->lenght++;

	return 0;
}

LinkListNode* LinkList_Get(LinkList* list, int pos)
{
	TLinkList *tList = NULL;
	LinkListNode *current = NULL;//辅助指针
	int i = 0;

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

	current = &(tList->header);
	for (i = 0; i < pos+1 && current != NULL; i++)
	{
		current = current->next;
	}

	return current;
}

LinkListNode* LinkList_Delete(LinkList* list, int pos)
{
	TLinkList *tList = NULL;
	LinkListNode *current = NULL;//辅助指针
	LinkListNode *theNode = NULL; //要删除的节点的指针
	int i = 0;

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


	current = &(tList->header);
	for (i = 0; i < pos  && current != NULL; i++)
	{
		current = current->next;
	}
	
	//先保存节点
	theNode = current->next;
	//将直接点删除
	current->next = theNode->next;
	theNode->next = NULL;
	tList->lenght--;

	return theNode;
}


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

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

int main()
{
	LinkList	*list = NULL;
	Teacher		t1, t2, t3;
	int			length;

	t1.age = 1;
	t2.age = 2;
	t3.age = 3;


	list = LinkList_Create();
	LinkList_Insert(list, (LinkListNode*)&t1 , LinkList_Length(list));
	LinkList_Insert(list, (LinkListNode*)&t2, LinkList_Length(list));
	LinkList_Insert(list, (LinkListNode*)&t3, LinkList_Length(list));

	length = LinkList_Length(list);

	for (int i = 0; i < length; i++)
	{
		Teacher *temp = (Teacher*)LinkList_Get(list, i);
		if (temp != NULL)
		{
			printf("the teacher age = %d \n", temp->age);
		}
	}

	while (LinkList_Length(list) > 0)
	{

		LinkList_Delete(list, 0);
	}

	LinkList_Destroy(list);

	system("pause");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值