链表学习笔记 --- 双向链表

这次代码写的是循环链表,用的是节点连接。

双向链表相比与一般链表要注意的地方就是 头部插入和尾部插入 不要进行空指针操作。


#ifndef DLINK_H_
#define DLINK_H_

typedef void DLinkList;
typedef struct _tag_DLinkListNode
{
	struct _tag_DLinkListNode *next;
	struct _tag_DLinkListNode *pre;
}DLinkListNode;


DLinkList *DLinkList_Create();

int DLinkList_Destroy(DLinkList *list);

int DLinkList_Lenght(DLinkList *list);

DLinkListNode* DLinkList_Get(DLinkList *list, int pos);

int DLinkList_Insert(DLinkList* list, DLinkListNode* node, int pos);

DLinkListNode* DLinkList_Delete(DLinkList* list, int pos);

DLinkListNode* DLinkList_Postion(DLinkList *list);

DLinkListNode* DLinkList_Next(DLinkList *list);

DLinkListNode* DLinkList_Pre(DLinkList *list);

#endif


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

#include"dlink.h"

typedef struct _tagTDLinkList
{
	DLinkListNode header;
	DLinkListNode *slider;
	int lenght;

}TDLinkList;


DLinkList *DLinkList_Create()
{
	TDLinkList *ret = NULL;
	ret = (TDLinkList*)malloc(sizeof(TDLinkList));
	if (ret == NULL)
	{
		return ret;
	}
	memset(ret, 0, sizeof(TDLinkList));
	ret->lenght = 0;
	ret->slider = NULL;
	ret->header.next = NULL;
	ret->header.pre = NULL;
	return ret;
}

int DLinkList_Destroy(DLinkList *list)
{
	if (list == NULL)
	{
		return -1;
	}
	free(list);
	return 0;
}


int DLinkList_Lenght(DLinkList *list)
{
	TDLinkList *tList = NULL;
	if (list == NULL)
	{
		return -1;
	}

	tList = (TDLinkList*)list;
	return tList->lenght;
}

DLinkListNode* DLinkList_Get(DLinkList *list, int pos)
{
	TDLinkList				*tList = NULL;
	DLinkListNode			*current = NULL;
	int						i = 0;
	tList = (TDLinkList*)list;
	if (list == NULL || pos < 0 || pos > tList->lenght)
	{
		return NULL;
	}


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

	return current;
}

int DLinkList_Insert(DLinkList* list, DLinkListNode* node, int pos)
{
	TDLinkList				*tList = NULL;
	DLinkListNode			*current = NULL;
	DLinkListNode			*next = NULL;
	int						i = 0;
	int						ret = 0;
	tList = (TDLinkList*)list;
	if (list == NULL || pos < 0 || node == NULL)
	{
		ret = -1;
		return ret;
	}

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

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

	next = current->next;
	

	node->next = next;
	current->next = node;

	//非链表尾部的处理
	if (next != NULL)
	{
		next->pre = node;
	}

	node->pre = current;

	//如果是在头部插如 则没有 前节点
	if (i == 0)
	{
		node->pre = NULL;
		tList->slider = node;
	}

	tList->lenght++;
	
	return 0;
}

DLinkListNode* DLinkList_Delete(DLinkList* list, int pos)
{
	TDLinkList				*tList = NULL;
	DLinkListNode			*current = NULL;
	DLinkListNode			*next = NULL;
	int						i = 0;
	DLinkListNode			*ret = 0;
	
	tList = (TDLinkList*)list;
	if (list == NULL || pos < 0 )
	{
		ret = NULL;
		return ret;
	}

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

	current = (DLinkListNode*)&(tList->header);
	for (i = 0; i < pos && current->next != NULL; i++)
	{
		current = current->next;
	}
	ret = current->next;
	if (ret == tList->slider)
	{
		if (current != (DLinkListNode*)&(tList->header))
			tList->slider = current;
		else
			tList->slider = next;

	}
	next = current->next->next;
	current->next = next;

	ret->next = NULL;
	ret->pre = NULL;


	if (next != NULL)
	{
		next->pre = current;
	}

	if (i == 0)
	{
		if (next != NULL)
				next->pre = NULL;
	}
	
	tList->lenght--;

	return ret;
}

DLinkListNode* DLinkList_Postion(DLinkList *list)
{
	TDLinkList				*tList = NULL;
	DLinkListNode			*current = NULL;
	int						i = 0;
	tList = (TDLinkList*)list;
	if (list == NULL )
	{
		return NULL;
	}

	return tList->slider;
}

DLinkListNode* DLinkList_Next(DLinkList *list)
{
	TDLinkList				*tList = NULL;
	DLinkListNode			*current = NULL;
	int						i = 0;
	tList = (TDLinkList*)list;
	if (list == NULL)
	{
		return NULL;
	}

	tList->slider = tList->slider->next;
	return tList->slider;
}

DLinkListNode* DLinkList_Pre(DLinkList *list)
{
	TDLinkList				*tList = NULL;
	DLinkListNode			*current = NULL;
	int						i = 0;
	tList = (TDLinkList*)list;
	if (list == NULL)
	{
		return NULL;
	}

	tList->slider = tList->slider->pre;
	return tList->slider;

}


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

typedef struct _tag_Teacher
{
	DLinkListNode node;
	char name[64];
	int age;
}Teacher;


int main()
{
	DLinkList		*list = NULL;
	int				i = 0;
	Teacher			t1, t2, t3;
	t1.age = 1;
	t2.age = 2;
	t3.age = 3;

	list = DLinkList_Create();

	DLinkList_Insert(list, (DLinkListNode*)&t1, 0);
	DLinkList_Insert(list, (DLinkListNode*)&t2, 0);
	DLinkList_Insert(list, (DLinkListNode*)&t3, 0);

	int size = DLinkList_Lenght(list);
	if (size == -1)
		return -1;
	for (i = 0; i < size; i++)
	{
		Teacher *temp = NULL;
		temp = (Teacher*)DLinkList_Get(list, i);
		printf("the age of teacher is %d\n", temp->age);
	}

	//for (i = 0; i < size; i++)
	//{
	//	Teacher *temp = NULL;
	//	temp = (Teacher*)DLinkList_Delete(list, 0);
	//	printf("the age of teacher is %d\n", temp->age);
	//}
	/*Teacher *temp = NULL;
	temp = (Teacher*)DLinkList_Delete(list, 2);
	printf("the age of teacher is %d\n", temp->age);

	
	temp = (Teacher*)DLinkList_Delete(list, 0);
	printf("the age of teacher is %d\n", temp->age);

	
	temp = (Teacher*)DLinkList_Delete(list, 0);
	printf("the age of teacher is %d\n", temp->age);*/

	Teacher *temp = NULL;
	temp = (Teacher*)DLinkList_Postion(list);
	printf("the age of teacher is %d\n", temp->age); 

	temp = (Teacher*)DLinkList_Next(list);
	printf("the age of teacher is %d\n", temp->age);

	temp = (Teacher*)DLinkList_Pre(list);
	printf("the age of teacher is %d\n", temp->age);


	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值