Data Structures----带头双向循环链表学习总结1

目录

1、在单链表的基础上,进行了双链表的学习;

2、头文件,对结构体进行定义以及函数的声明;

3、对各个功能子函数进行编写;

4、进行各个子程序功能测试 ;

       4.1测试结果1

       4.2测试结果2

       4.3测试结果3

1、在单链表的基础上,进行了双链表的学习;

双链表与单链表最大的区别就是结构具有很大的优势,我们在进行删除、查找、插入等功能的时候,更加的方便快捷;

2、头文件,对结构体进行定义以及函数的声明;

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef int DListDataType;
typedef struct DListNode
{
	DListDataType data;
	struct DList* next;
	struct DList* prev;
}DLNode;

DLNode* DListIint();//双向链表循环初始化
void DListDestroy(DLNode* phead);//双链表摧毁函数
void DListPrint(DLNode* phead);//链表打印函数
DLNode* BuyListNode(DListDataType x);//链表新节点创建函数

void DListPushBack(DLNode* phead, DListDataType x);//链表尾插函数
void DListPopBack(DLNode* phead);//链表尾删函数
void DListPushFront(DLNode* phead, DListDataType x);//链表头插函数
void DListPopFront(DLNode* phead);//链表头删函数

DLNode* DListFind(DLNode* phead, DListDataType x);//链表查找函数
void DListInsert(DLNode* pos, DListDataType x);//链表插入函数
void DListErase(DLNode* pos);//删除pos位置

 3、对各个功能子函数进行编写;

#include "DList.h"

//双向链表初始化函数
DLNode* DListIint()
{
	DLNode* phead = (DLNode*)malloc(sizeof(DLNode));//创建哨兵位头结点
	phead->next = phead;//链表双向循环初始化
	phead->prev = phead;
	return phead;
}
//********************************************

//链表打印函数
void DListPrint(DLNode* phead)
{
	assert(phead);

	DLNode* cur = phead->next;
	while (cur != phead)
	{
		printf("%d\n", cur->data);
		cur = cur->next;
	}
	printf("\n");

}
//********************************************

//链表新节点创建函数
DLNode* BuyListNode(DListDataType x)
{
	DLNode* newnode = (DLNode*)malloc(sizeof(DLNode));
	newnode->next = NULL;
	newnode->prev = NULL;
	newnode->data = x;
	return newnode;
}
//********************************************

//链表尾插函数
void DListPushBack(DLNode* phead, DListDataType x)
{
	assert(phead);

	/*DLNode* tail = phead->prev;
	DLNode* newnode = BuyListNode(x);
	tail->next = newnode;
	newnode->prev = tail;
	phead->prev = newnode;
	newnode->next = phead;*/

	DListInsert(phead, x);//链表插入函数

}
//********************************************

//链表尾删函数
void DListPopBack(DLNode* phead)
{
	assert(phead);
	assert(phead->next != phead);

	/*DLNode* tail = phead->prev;
	DLNode* pervtail = tail->prev;
	pervtail->next = phead;
	phead->prev = pervtail;
	free(tail);*/

	DListErase(phead->prev);
}
//********************************************

//链表头插函数
void DListPushFront(DLNode* phead, DListDataType x)
{
	assert(phead);

	/*DLNode* newnode = BuyListNode(x);
	DLNode* Next = phead->next;
	phead->next = newnode;
	newnode->prev = phead;
	newnode->next = Next;
	Next->prev = newnode;*/

	DListInsert(phead->next,x);
}
//********************************************

//链表头删函数
void DListPopFront(DLNode* phead)
{
	assert(phead);
	assert(phead->next != phead);

	/*DLNode* Next = phead->next;
	DLNode* Nextnext = Next->next;
	phead->next = Nextnext;
	Nextnext->prev = phead;
	free(Next);*/

	DListErase(phead->next);
}
//********************************************

//链表查找函数
DLNode* DListFind(DLNode* phead, DListDataType x)
{
	assert(phead);

	DLNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		else
		{
			cur = cur->next;
		}
	}
	return NULL;
}
//********************************************

//链表插入函数pos位置之前
void DListInsert(DLNode* pos, DListDataType x)
{
	assert(pos);
	DLNode* newnode = BuyListNode(x);
	DLNode* posprev = pos->prev;

	posprev->next = newnode;
	newnode->prev = posprev;
	newnode->next = pos;
	pos->prev = newnode;

}
//********************************************

//删除pos位置
void DListErase(DLNode* pos)
{
	assert(pos);
	DLNode* posnext = pos->next;
	DLNode* posprev = pos->prev;

	posprev->next = posnext;
	posnext->prev = posprev;
	free(pos);
	pos = NULL;
}
//********************************************

//双链表摧毁函数
void DListDestroy(DLNode* phead)
{
	assert(phead);

	DLNode* cur = phead->next;
	while (cur != phead)
	{
		DLNode* NEXT = cur->next;
		free(cur);
		cur = NEXT;

	}
	free(phead);
	phead = NULL;
}
//********************************************

4、进行各个子程序功能测试 ;

4.1测试结果1

void test01()
{
	DLNode* PList = DListIint();
	DListPushBack(PList, 4);
	DListPushBack(PList, 5);
	DListPushBack(PList, 1);
	DListPushBack(PList, 4);
	DListPushBack(PList, 5);// 链表尾插函数
	DListPrint(PList);

	DListPopBack(PList);// 链表尾删函数
	DListPushFront(PList, 77);//链表头插函数
	DListPushFront(PList, 11111);
	
	DListPrint(PList);
}

1)进行链表基础的插入和删除功能验证;首先进行尾插,数据(45145);

2)然后尾删掉一个(5),再头插)(11111、77),成功运行如下图; 

4.2测试结果2

void test02()
{
	DLNode* PList = DListIint();
	DListPushBack(PList, 5);// 链表尾插函数
	DListPushFront(PList, 77);//链表头插函数
	DListPushFront(PList, 11111);
	DListPrint(PList);


	DLNode* pos = DListFind(PList,77);
	if (pos)
	{
		
		DListInsert( pos, 895);//链表插入函数
		DListPrint(PList);
		
		DListErase(pos);//删除pos位置
		DListPrint(PList);

		DListDestroy(PList);//双链表摧毁函数
		printf("链表释放\n");
		DListPrint(PList);
	}
	else
		printf("链表没有该数据\n");

}

 1)进行查找函数和插入函数的验证,先插入数据11111、77、5;

2)然后查找链表中的77,找到了就插入895;

3)然后进行pos位置的删除,把77删除;

4)最后验证链表摧毁函数的验证,把链表给释放了;

 4.3测试结果3

void test03()
{
	DLNode* PList = DListIint();
	DListPushBack(PList,1);// 新链表尾插函数
	DListPushBack(PList,2);// 新链表尾插函数
	DListPushBack(PList,3);// 新链表尾插函数
	DListPushBack(PList,4);// 新链表尾插函数
	DListPushFront(PList, 77);//新链表头插函数
	DListPrint(PList);

	DListPopBack(PList);// 新链表尾删函数
	DListPopFront(PList);//新链表头删函数
	DListPrint(PList);
}

 1)对尾插和头插,插入函数进行了优化,通过调用插入函数Insert函数进行优化,方便快捷;

 2)对尾删和头删,删除函数进行了优化,通过调用删除函数Erase函数进行优化,方便快捷;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值