带头双向循环链表实现

  • Dlist.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int Datatype;
typedef struct ListNode
{
	Datatype _val;
	struct ListNode* _prev;
	struct ListNode* _next;
}ListNode;
typedef struct List
{
	ListNode* _head;
}List;

void ListInt(List* pcb);//初始化
void ListDestroy(List* pcb);//销毁
void List_print(List* pcb);//打印

void ListHead_insert(List* pcb, Datatype x);//头插
void ListHead_delete(List* pcb);//头删

void ListTail_insert(List* pcb, Datatype x);//尾插
void ListTail_delete(List* pcb);//尾删

ListNode* BuyListNode(int val);//创建新结点
ListNode* List_find(List* pcb, Datatype x);//查找

void ListFind_insert(ListNode* pos,Datatype x);//随机找值插入
void ListFind_delete(ListNode* pos1);//随机找值删除

  • Dlist.c
#include"Dlist.h"
ListNode* BuyListNode(Datatype val)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	newnode->_next = NULL;
	newnode->_prev = NULL;
	newnode->_val = val;
	return newnode;
}
void ListInt(List* pcb)
{
	assert(pcb);
	ListNode* head = (ListNode*)malloc(sizeof(ListNode));
	head->_next = head;
	head->_prev = head;
	pcb->_head = head;
}

void ListTail_insert(List* pcb, Datatype x)
{
	assert(pcb);
	ListNode* head = pcb->_head;
	//ListNode* tail = head->_prev; 
	//ListNode* newnode = BuyListNode(x);
	//newnode->_prev = tail;
	//tail->_next = newnode;
	//head->_prev = newnode;
	//newnode->_next = head;

	//或者
	ListFind_insert(head, x);
}
void ListTail_delete(List* pcb)
{
	assert(pcb);
	ListNode* head = pcb->_head;
	//ListNode* tail = head->_prev;
	//ListNode* pre_tail = tail->_prev;
	//head->_prev = pre_tail;
	//pre_tail->_next = head;
	//free(tail);

	//或者
	ListFind_delete(head->_prev);
}
void ListHead_insert(List* pcb, Datatype x)
{
	assert(pcb);
	ListNode* head = pcb->_head;
	//ListNode* phead = head->_next;
	//ListNode* newnode = BuyListNode(x);
	//head->_next = newnode;
	//newnode->_prev = head;
	//newnode->_next = phead;
	//phead->_prev = newnode;

	//或者
	ListFind_insert(head->_next, x);
}
void ListHead_delete(List* pcb)
{
	assert(pcb);
	ListNode* head = pcb->_head;
	//ListNode* first_head = head->_next;
	//ListNode* second_head = first_head->_next;
	//head->_next= second_head;
	//second_head->_prev = head;
	//free(first_head);

	//或者
	ListFind_delete(head->_next);
}
ListNode* List_find(List* pcb, Datatype x)
{
	assert(pcb);
	ListNode* head = pcb->_head;
	ListNode* cur = head->_next;
	while (cur != head)
	{
		if (cur->_val == x)
		{
			return cur;
		}
		cur = cur->_next;
	}
	return NULL;
}

void ListFind_insert(ListNode* pos,Datatype x)
{
	assert(pos);
	ListNode* prev_pos = pos->_prev;
	ListNode* newnode = BuyListNode(x);
	prev_pos->_next = newnode;
	newnode->_prev = prev_pos;
	newnode->_next = pos;
	pos->_prev = newnode;
}

void ListFind_delete(ListNode* pos1)
{
	assert(pos1);
	ListNode* pos1_next = pos1->_next;
	ListNode* pre_pos1 = pos1->_prev;
	pre_pos1->_next = pos1_next;
	pos1_next->_prev = pre_pos1;
	free(pos1);
}
void List_print(List* pcb)
{
	assert(pcb);
	ListNode* head = pcb->_head;
	ListNode* cur = head->_next;
	printf("head<=>");
	while (cur != head)
	{
		printf("%d<=>", cur->_val);
		cur = cur->_next;
	}
	printf("\n");
}
void ListDestroy(List* pcb)
{
	assert(pcb);
	ListNode* head = pcb->_head;
	ListNode* cur = head->_next;
	ListNode* next = NULL;
	while (cur != head)
	{
		next = cur->_next;
		free(cur);
		cur = next;
	}
}
  • test.c
#include"Dlist.h"
void List_test1()
{
	List cb;
	ListInt(&cb);
	ListHead_insert(&cb, 1);
	ListHead_insert(&cb, 2);
	ListHead_insert(&cb, 3);
	ListHead_insert(&cb, 4);
	ListHead_insert(&cb, 5);

	ListHead_delete(&cb);
	ListHead_delete(&cb);
	ListHead_delete(&cb);
	ListHead_delete(&cb);

	List_print(&cb);
}
void List_test2()
{
	List cb;
	ListInt(&cb);
	ListTail_insert(&cb, 1);
	ListTail_insert(&cb, 2);
	ListTail_insert(&cb, 3);
	ListTail_insert(&cb, 4);
	ListTail_insert(&cb, 5);


	ListTail_delete(&cb);
	ListTail_delete(&cb);
	ListTail_delete(&cb);
	ListTail_delete(&cb);

	List_print(&cb);

    ListNode* pos = List_find(&cb,1);
	ListFind_insert( pos, 5);
	List_print(&cb);

	ListNode* pos1 = List_find(&cb, 3);
	ListFind_delete(pos1);
	List_print(&cb);

	ListDestroy(&cb);
}
int main()
{
    //List_test1();
	List_test2();
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值