带头循环链表

定义

# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
typedef int  LTDataType;
typedef struct ListNode
{
	struct ListNode* next;
	struct ListNode* prev;
	LTDataType data;
}ListNode;

ListNode* ListInit(void);
void ListPrint(ListNode* phead);
void ListDestory(ListNode* phead);
void ListPushback(ListNode* phead, LTDataType x);
void ListPushFront(ListNode* phead, LTDataType x);
ListNode* ListFind(ListNode* phead, LTDataType x);
void ListErase(ListNode* pos);
void ListNodeInset(ListNode* pos, LTDataType x);
void ListPopBack(ListNode* phead);
void ListPopFront(ListNode* phead);

创造节点

ListNode* BuyNode(LTDataType x)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	if (newnode == NULL)
	{
		perror("buynode");
		exit(-1);
	}
	else
	{
		newnode->data = x;
		newnode->next = NULL;
		newnode->prev = NULL;
		return newnode;
	}
}

 初始化

ListNode* ListInit(void)
{
	ListNode* phead = BuyNode(0);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}

尾插

void ListPushback(ListNode* phead, LTDataType x)
{
	assert(phead);
	ListNode* tail = phead->prev;
	ListNode* newnode = BuyNode(x);

	tail->next = newnode;
	newnode->next = phead;
	phead->prev = newnode;
	
	
}

头插

void ListPushFront(ListNode* phead, LTDataType x)
{
	assert(phead);
	ListNode* first = phead->next;
	ListNode* newnode = BuyNode(x);

	//phead newnode first
	phead->next = newnode;
	newnode->prev = phead;
	newnode->next = first;
	first->prev = newnode;
}

尾删

void ListPopBack(ListNode* phead)
{
	assert(phead);
	assert(phead->next != phead);
	ListNode* tail = phead->prev;
	ListNode* prev = tail->prev;

	prev->next = phead;
	phead->prev = prev;

	free(tail);
	tail = NULL;


}

头删

void ListPopFront(ListNode* phead)
{
	assert(phead);
	assert(phead->next != phead);
	ListNode* first = phead->next;
	ListNode* second = first->next;

	phead->next = second;
	second->prev = phead;
	free(first);
	first = NULL;
}

查找

ListNode* ListFind(ListNode* phead, LTDataType x)
{
	assert(phead); 
	ListNode* cur = phead->next;
	while (cur!=phead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		else
		{
			cur = cur->next;
		}
		return NULL;
	}
}

 插入

void ListNodeInset(ListNode* pos, LTDataType x)
{
	assert(pos);
	ListNode* prev = pos->prev;
	ListNode* newndoe = BuyNode(x);
	
	prev->next= newndoe;
	newndoe->prev = prev;
	newndoe->next = pos;
	pos->prev = newndoe;

}

删除

void ListErase(ListNode* pos)
{
	assert(pos);
	ListNode* prev = pos->prev;
	ListNode* next = pos->next;

	prev->next = next;
	next->prev = prev;
	free(pos);
	pos=NULL;
}

打印 

void ListPrint(ListNode* phead)
{
	assert(phead); 
	ListNode* cur = phead->next;
	while(cur!=phead)
	{
		printf("%d ", cur->data);
		cur = cur->next;

	}
	printf("\n");
}

 销毁

void ListDestory(ListNode* phead)
{
	assert(phead);
	ListNode* cur = phead->next;
	while (cur != phead)
	{
		ListNode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(phead);
	phead = NULL;

}

复用 头删尾尾删

void ListPopFront(ListNode* phead)
{
assert(phead);
ListErase(phead->next);
}
void ListPopBack(ListNode* phead)
{
     assert(phead);
	ListErase(phead->prev);
}

复用头插尾插

void ListPushFront(ListNode* phead, LTDataType x)
{
    assert(phead);
    ListNodeInset(phead->next,x);
}

尾插

void ListPushback(ListNode* phead, LTDataType x)
{
    assert(phead);
   ListNodeInset(phead->prev,x);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值