双向链表的接口设计

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


//指的是双向链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int  DataType_t;

//构造双向链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct DoubleLinkedList
{
	DataType_t  		     data; //结点的数据域
	struct DoubleLinkedList	*prev; //直接前驱的指针域
	struct DoubleLinkedList	*next; //直接后继的指针域

}DoubleLList_t;


//创建一个空双向链表,空链表应该有一个头结点,对链表进行初始化
DoubleLList_t * DoubleLList_Create(void)
{
	//1.创建一个头结点并对头结点申请内存
	DoubleLList_t *Head = (DoubleLList_t *)calloc(1,sizeof(DoubleLList_t));
	if (NULL == Head)
	{
		perror("Calloc memory for Head is Failed");
		exit(-1);
	}

	//2.对头结点进行初始化,头结点是不存储数据域,指针域指向NULL
	Head->prev = NULL;
	Head->next = NULL;

	//3.把头结点的地址返回即可
	return Head;
}

//创建新的结点,并对新结点进行初始化(数据域 + 指针域)


DoubleLList_t * DoubleLList_NewNode(DataType_t data)
{
	//1.创建一个新结点并对新结点申请内存
	DoubleLList_t *New = (DoubleLList_t *)calloc(1,sizeof(DoubleLList_t));
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}

	//2.对新结点的数据域和指针域的两个指针进行初始化
	New->data = data;
	New->prev = NULL;
	New->next = NULL;

	return New;
}
//头插
bool DoubleLList_HeadInsert(DoubleLList_t *Head,DataType_t data)
{
	
	DoubleLList_t *Phead=Head;
	DoubleLList_t *New = DoubleLList_NewNode(data);
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}
	
	if(Phead->next==NULL)
	{
		Head->next=New;
		return true;
	}
	New->next=Phead->next;
	Phead->next->prev=New;
	Head->next=New;


	return true;
}

//尾插
bool DoubleLList_TailInsert(DoubleLList_t *Head,DataType_t data)
{
	DoubleLList_t *Phead = Head;
	DoubleLList_t *New = DoubleLList_NewNode(data);
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}

	if(Phead->next==NULL)
	{
		Head->next=New;
		return true;
	}

	while(Phead->next!=NULL)
	{
		Phead = Phead->next;
	}
	Phead->next = New;
	New->prev = Phead;

	return true;
}
//指定位置插入
bool DoubleLList_DestInsert(DoubleLList_t *Head,DataType_t destval,DataType_t data)
{

	DoubleLList_t *Phead = Head;
	DoubleLList_t *New = DoubleLList_NewNode(data);
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}

	while(Phead->data != destval)
	{
		if(Phead->next==NULL)
		{
			printf("dest node is not found\n");
			return false;
		}
		Phead = Phead->next;
	}

	if(Phead->next ==NULL)
	{
		New->prev = Phead;
		Phead->next = New;
	}
	else
	{
		New->next = Phead->next;
		New->prev = Phead;
		Phead->next = New;
		New->next->prev = New;
	}


	return true;
}
//头删
bool DoubleLList_HeadDel(DoubleLList_t *Head)
{
	//对单向循环链表的头结点的地址进行备份
	DoubleLList_t *Phead=Head->next;
	
	if(Head->next =NULL)
	{
		printf("双链表为空\n");
		return false;
	}
	
	Head->next = Phead->next;
	Phead->next=NULL;
	Phead->prev = NULL;
	free(Phead);

	return true;
}
//尾删
bool DoubleLList_TailDel(DoubleLList_t *Head)
{
	//对单向循环链表的头结点的地址进行备份
	DoubleLList_t *Phead=Head;

	if(Head->next==NULL)
	{
		printf("双链表为空\n");
		return false;
	}
	while(Phead->next!=NULL)
	{
		Phead=Phead->next;
	}

	if(Head->next==Phead)//单元素
	{
		Head->next = NULL;
	}
	else
	{
		Phead->prev->next=NULL;
		Phead->prev = NULL;
	}
	free(Phead);

	return true;
}

bool DoubleLList_Del(DoubleLList_t *Head, DataType_t dest) 
{
    // 对双链表的头结点的地址进行备份
    DoubleLList_t *Phead = Head;
    
    if (Phead == NULL) 
	{
        printf("双链表为空\n");
        return false;
    }
    
    while (Phead->data != dest) 
	{
        if (Phead->next == NULL) 
		{
            printf("未找到目标节点\n");
            return false;
        }
        Phead = Phead->next;
    }
    
    // 找到目标节点
    if (Phead == Head->next) 
	{
        Head->next=Phead->next;
		if(Phead->next!=NULL)
		{
			Phead->next->prev=NULL;
			Phead->next = NULL;
		}
    }
	else if(Phead->next == NULL)
	{
		Phead->prev->next = NULL;
		Phead->prev = NULL;
	}
	else
	{
		Phead->next->prev=Phead->prev;
		Phead->prev->next=Phead->next;
		Phead->prev = NULL;
		Phead->next = NULL;
	}
    // 释放节点内存
    free(Phead);

    
    return true;
}

bool DoubleLList_Print(DoubleLList_t *Head)
{
	//对单向循环链表的头结点的地址进行备份
	DoubleLList_t *Phead = Head;
	
	//判断当前链表是否为空,为空则直接退出
	if (Head->next == NULL)
	{
		printf("current linkeflist is empty!\n");
		return false;
	}

	//从首结点开始遍历
	while(Phead->next)
	{
		//把头结点的直接后继作为新的头结点
		Phead = Phead->next;

		//输出头结点的直接后继的数据域
		printf("data = %d\n",Phead->data);

		//判断是否到达尾结点,尾结点的next指针是指向首结点的地址
		if (Phead->next == NULL)
		{
			break;
		}	
	}

	return true;
}

int main(int argc, char const *argv[])
{

	//1.创建顺序表
	DoubleLList_t * Manager = DoubleLList_Create();
	
	//2.向顺序表中的尾部插入新元素
	printf("*********************************尾插********************************\n");
	DoubleLList_TailInsert(Manager,5);
	DoubleLList_TailInsert(Manager,2);
	DoubleLList_TailInsert(Manager,1);
	DoubleLList_TailInsert(Manager,4);
	DoubleLList_TailInsert(Manager,6);  


	// //3.遍历顺序表
	DoubleLList_Print(Manager); // -- 5 2 1 4 6
	printf("\n");
	//4.向顺序表中的头部插入新元素
	printf("*********************************头插********************************\n");
	DoubleLList_HeadInsert(Manager,9);
	DoubleLList_HeadInsert(Manager,7);
	DoubleLList_HeadInsert(Manager,8);
	DoubleLList_HeadInsert(Manager,0);
	DoubleLList_HeadInsert(Manager,10);  

	// //5.遍历顺序表
	DoubleLList_Print(Manager); // --10 0 8 7 9 5 2 1 4 6
	printf("\n");
	//6.删除顺序表的元素
	printf("*********************************头删********************************\n");
	DoubleLList_HeadDel(Manager);
	DoubleLList_HeadDel(Manager);
	DoubleLList_HeadDel(Manager);
	DoubleLList_HeadDel(Manager);
	DoubleLList_HeadDel(Manager);


	DoubleLList_Print(Manager);// --5  2  1  4  6 


	//7.指定插入
	printf("*********************************指定插入********************************\n");
	DoubleLList_DestInsert(Manager,5,1);
	DoubleLList_DestInsert(Manager,2,3);
	DoubleLList_DestInsert(Manager,6,7);

	//8.遍历顺序表
	DoubleLList_Print(Manager); // --5 1 2 3 1 4 6 7
	printf("\n");

	//9.指定删除
	printf("*********************************指定删除********************************\n");
	DoubleLList_Del(Manager,5);
	DoubleLList_Del(Manager,7);
	DoubleLList_Del(Manager,3);

	//10.遍历顺序表
	DoubleLList_Print(Manager); // -- 1 2 1 4 6 
	printf("\n");

	//11.尾部删除
	printf("*********************************尾删********************************\n");
	DoubleLList_TailDel(Manager);
	DoubleLList_TailDel(Manager);
	DoubleLList_TailDel(Manager);
	DoubleLList_TailDel(Manager);

	//12.遍历顺序表
	DoubleLList_Print(Manager); // --1  
	printf("\n");
}
posted @   藍桉未遇釋槐鳥  阅读(3)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示