欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

双向链表

程序员文章站 2022-05-06 21:40:59
...

doubleLinkList.h:

#pragma once
typedef int Type;
struct Node
{
	Type data;
	Node* prev;
	Node* next;
};

struct LinkList
{
	Node* head;
	Node* tail;
	int size;
};

//双向链表的初始化
void LinkList_new(LinkList* ll);

//添加头节点
//value 要插入的元素
void lpush_front(LinkList* ll, Type value);

//添加尾结点
//value 要插入的元素
void lpush_back(LinkList* ll, Type value);

//删除头部节点,.并将被删除的数据返回
//return 返回删除的元素
Type lpop_front(LinkList* ll);

//删除尾部节点,取出被删除的数据
Type lpop_back(LinkList* ll);

//销毁所有的节点
void ldestroy(LinkList* ll);

//输出链表中所有元素
void lout(LinkList* ll);

//逆向输出
void lrout(LinkList* ll);

//连接两个链表
void lconnect(LinkList* ll1, LinkList* ll2);

//push  插入元素
void linsert(LinkList* ll, int index, Type val);
//pop   删除元素
void ldelete(LinkList* ll, int index);
//back	最后面的元素
Type lback(LinkList* ll);
//front 最前面的元素
Type lfront(LinkList* ll);
//复制链表
LinkList* lcopy(LinkList* ll);

doubleLinkList.cpp:

#include "doubleLinkList.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h> 
//初始化节点
Node* node_create(Type val)
{
	Node* temp = (Node*)malloc(sizeof(Node));
	temp->data = val;
	temp->next = NULL;
	temp->prev = NULL;
	return temp;
}
//连接2个节点
void node_link(Node* n1, Node* n2)
{
	n1->next = n2;
	n2->prev = n1;
}


//双向链表的初始化
void LinkList_new(LinkList* ll) {
	ll->head = NULL;
	ll->tail = NULL;
	ll->size = 0;
}

//添加头节点
//value 要插入的元素
void lpush_front(LinkList* ll, Type value)
{
	if (ll->head == NULL)
	{
		ll->head = node_create(value);
		ll->tail = ll->head;
		ll->size = 1;
	}
	else
	{
		Node* temp = node_create(value);
		node_link(temp, ll->head);
		ll->size++;
		ll->head = temp;
	}
}

//添加尾结点
//value 要插入的元素
void lpush_back(LinkList* ll, Type value)
{
	if (ll->tail == NULL)
	{
		ll->tail = node_create(value);
		ll->head = ll->tail;
		ll->size++;
	}
	else
	{
		Node* temp = node_create(value);
		node_link(ll->tail, temp);
		ll->tail = temp;
		ll->size++;
	}
}

//删除头部节点,.并将被删除的数据返回
//return 返回删除的元素
Type lpop_front(LinkList* ll)
{
	assert(ll->head);
	Type temp = ll->head->data;
	if (ll->size == 1)
	{
		free(ll->head);
		ll->head= NULL;
		ll->tail = NULL;
		
	}
	else
	{
		ll->head = ll->head->next;
		free(ll->head->prev);
		ll->head->prev= NULL;
	}
	ll->size--;
	return temp;

}

//删除尾部节点,取出被删除的数据
Type lpop_back(LinkList* ll)
{
	assert(ll->tail);
	Type temp = ll->tail->data;
	if (ll->size == 1)
	{
		free(ll->tail);
		ll->head = NULL;
		ll->tail = NULL;

	}
	else
	{
		ll->tail = ll->tail->prev;
		free(ll->tail->next);
		ll->tail->next = NULL;
	}
	ll->size--;
	return temp;
}

//销毁所有的节点
void ldestroy(LinkList* ll)
{
	for (Node* temp = ll->head; temp != NULL;)
	{
		Node* t = temp;
		temp = temp->next;
		free(t);
	}
	LinkList_new(ll);
}

//输出链表中所有元素
void lout(LinkList* ll)
{
	if (ll->head == NULL)
	{
		printf("输出头为空\n");
		return;
	}
	for (Node* temp = ll->head; temp != NULL;temp=temp->next)
	{
		printf("%d  ", temp->data);
	}
	printf("\n");
}

//逆向输出
void lrout(LinkList* ll)
{
	for (Node* temp = ll->tail; temp != NULL; temp = temp->prev)
	{
		printf("%d  ", temp->data);
	}
	printf("\n");
}

//连接两个链表
void lconnect(LinkList* ll1, LinkList* ll2)
{
	if (ll1->head == NULL || ll2->head == NULL)
	{
		return;
	}
	node_link(ll1->tail, ll2->head);
	ll1->size += ll2->size;
}

//push  插入元素
void linsert(LinkList* ll, int index, Type val)
{
	if (ll->head == NULL)
	{
		return;
	}
	if (index<0 || index>ll->size)//越界
	{
		printf("index越界\n");
		return;
	}
	else if (index == 0)//头插
	{
		Node* temp = node_create(val);
		node_link(temp, ll->head);
		ll->head = temp;
		ll->size++;
	}
	else if (index == ll->size)//尾插
	{
		Node* temp = node_create(val);
		node_link(ll->tail,temp);
		ll->tail = temp;
		ll->size++;
	}
	else//中间插
	{
		Node* temp = node_create(val);
		Node* t1 = ll->head;
		for (int i = 1; i < index; i++)
		{
			assert(t1);
			t1 = t1->next;
		}
		temp->prev = t1;
		temp->next = t1->next;
		t1->next = temp;
		temp->next->prev = temp;
		ll->size++;
	}

}
//pop   删除元素
void ldelete(LinkList* ll, int index)
{
	if (index<0 || index> ll->size)
	{
		return;
	}
	if (index == 0)
	{
		lpop_front(ll);
	}
	else if (index == ll->size)
	{
		lpop_back(ll);
	}
	else
	{
		Node* t1 = ll->head;
		for (int i = 1; i < index; i++)
		{
			assert(t1);
			t1 = t1->next;
		}
		t1->next = t1->next->next;
		free(t1->next->prev);
		t1->next->prev = t1;
		ll->size--;
	}
}
//back	最后面的元素
Type lback(LinkList* ll)
{
	return ll->tail->data;
}
//front 最前面的元素
Type lfront(LinkList* ll)
{
	return ll->head->data;
}

LinkList* lcopy(LinkList* ll)
{
	LinkList* temp = (LinkList*)malloc(sizeof(LinkList));
	LinkList_new(temp);
	Node* val = ll->tail;
	while (val != NULL)
	{
		lpush_front(temp, val->data);
		val = val->prev;
	}
	return temp;
}

main.cpp:

#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
#include "doubleLinkList.h"
void test()
{
	LinkList* list = (LinkList*)malloc(sizeof(LinkList));
	LinkList* list2 = (LinkList*)malloc(sizeof(LinkList));
	int val=0;
	LinkList_new(list);
	LinkList_new(list2);
	printf("输入头插的2个数据\n");
	for (int i = 0; i < 2; i++)
	{
		scanf("%d", &val);
		lpush_front(list, val);
	}
	printf("顺序输出\n");
	lout(list);
	printf("逆序输出\n");
	lrout(list);

	printf("\n");

	printf("输入尾插的2个数据\n");
	for (int i = 0; i < 2; i++)
	{
		scanf("%d", &val);
		lpush_back(list, val);
	}
	printf("顺序输出\n");
	lout(list);
	printf("逆序输出\n");
	lrout(list);

	printf("\n");

	val = lpop_front(list);
	printf("头pop出%d\n", val);
	printf("顺序输出\n");
	lout(list);

	printf("\n");

	val = lpop_back(list);
	printf("尾pop出%d\n", val);
	printf("顺序输出\n");
	lout(list);

	printf("\n创建list2\n");
	printf("输入头插的2个数据\n");
	for (int i = 0; i < 2; i++)
	{
		scanf("%d", &val);
		lpush_front(list2, val);
	}
	printf("顺序输出list2\n");
	lout(list2);

	lconnect(list, list2);
	printf("合并2个链表顺序输出list\n");
	lout(list);

	printf("输入要插入的位置和值\n");
	int index = 0;
	scanf("%d%d", &index, &val);
	linsert(list, index, val);
	printf("顺序输出list\n");
	lout(list);

	printf("输入要删除的位置\n");
	scanf("%d", &index);
	ldelete(list, index);
	printf("顺序输出list\n");
	lout(list);
	
	ldestroy(list);
	free(list);
	free(list2);
}
void test1()
{
	LinkList* list = (LinkList*)malloc(sizeof(LinkList));
	LinkList_new(list);
	lpush_front(list, 10);
	lpush_back(list, 20);
	lout(list);
	LinkList* list2 = lcopy(list);
	lout(list2);

	ldestroy(list);
	ldestroy(list2);
	free(list);
	free(list2);
	
}

int main()
{
	//test();
	test1();
	return 0;
}

结果:

双向链表
双向链表

相关标签: 双向链表