双向循环链表C语言

#include <stdio.h>
#include <stdlib.h>
struct doubleList {
	int data;
	struct doubleList* front;
	struct doubleList* rear;
};
struct doubleList* createList()
{
	struct doubleList* headNode = (struct doubleList *)malloc(sizeof(struct doubleList));
	headNode->front = headNode->rear = headNode;
	return headNode;
}
struct doubleList* createNode(int data)
{
	struct doubleList* newNode = (struct doubleList *)malloc(sizeof(struct doubleList));
	newNode->data = data;
	newNode->front = newNode->rear = NULL;
	return newNode;
}
void insertNode_head(struct doubleList* headNode, int data)
{
	struct doubleList* newNode = createNode(data);
	newNode->front = newNode;
	newNode->rear = headNode->rear;
	headNode->rear->front = newNode;
	headNode->rear = newNode;
}
void insertNode_rear(struct doubleList* headNode, int data)
{
	struct doubleList* newNode = createNode(data);
	struct doubleList* lastNode = headNode;
	while(lastNode->rear != headNode)
	{
		lastNode = lastNode->rear;
	}
	headNode->front = newNode;
	newNode->rear = headNode;
	lastNode->rear = newNode;
	newNode->front = lastNode;
}
void deleteNode_point(struct doubleList* headNode, int posdata)
{
	struct doubleList* posNode = headNode->rear;
	struct doubleList* posNodeFront = headNode;
	while(posNode->data != posdata)
	{
		posNodeFront = posNode;
		posNode = posNodeFront->rear;
		if(posNode->rear == headNode)
		{
			return;
		}
	}
	posNodeFront->rear = posNode->rear;
	posNode->rear->front = posNodeFront;
	free(posNode);
}
void printList_rear(struct doubleList* headNode)
{
	struct doubleList* t = headNode->rear;
	while(t != headNode)
	{
		printf("%d", t->data);
		t = t->rear;
	}
	printf("\n");
}
void printList_front(struct doubleList* headNode)
{
	struct doubleList* t = headNode->front;
	while(t != headNode)
	{
		printf("%d", t->data);
		t = t->front;
	}
	printf("\n");
}
int main()
{
	struct doubleList* list = createList();
	insertNode_head(list, 1);
	insertNode_head(list, 2);
	insertNode_head(list, 3);
	printList_rear(list);
	insertNode_rear(list, 0);
	printList_rear(list);
	deleteNode_point(list, 3);
	printList_rear(list);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值