带头节点的双向循环链表

//DList.h
#pragma once

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

typedef int LTdataType;

typedef struct ListNode
{
	struct ListNode* prev;
	struct ListNode* next;
	LTdataType data;
}ListNode;

ListNode* Init();
void Distory(ListNode *phead);
void print(ListNode *phead);
ListNode *buy(LTdataType x);

void pushback(ListNode *phead,int x);
void pushfront(ListNode *phead, int x);
void popback(ListNode *phead);
void popfront(ListNode *phead);

ListNode *find(ListNode *phead, LTdataType x);
void insert(ListNode *pos, LTdataType x);
void erase(ListNode *pos);

//DList.c
#include"DList.h"

ListNode* Init()
{
	ListNode* phead = (ListNode*)malloc(sizeof(ListNode));
	phead->next = phead;
	phead->prev = phead;
	return phead;
}

ListNode *buy(LTdataType x)
{
	ListNode *node = (ListNode*)malloc(sizeof(ListNode));
	node->data = x;
	node->next = NULL;
	node->prev = NULL;
	return node;
}

void pushback(ListNode *phead, int x)
{
	assert(phead);
	ListNode *newnode = buy(x);
	ListNode *tail = phead->prev;
	tail->next = newnode;
	newnode->prev = tail;
	newnode->next = phead;
	phead->prev = newnode;
	//insert(phead,x);
}

void pushfront(ListNode *phead, int x)
{
	assert(phead);
	assert(phead->next != phead);
	ListNode *newnode = buy(x);
	ListNode *head = phead->next;
	head->prev = newnode;
	newnode->next = phead->next;
	newnode->prev = phead;
	phead->next = newnode;
	//insert(phead->next,x);
}

void popback(ListNode *phead)
{
	assert(phead);
	assert(phead->next != phead);
	ListNode *tail = phead->prev;
	phead->prev = tail->prev;
	tail->prev->next = phead;
	free(tail);
	//erase(phead->prev);
}

void popfront(ListNode *phead)
{
	assert(phead);
	ListNode *head = phead->next;
	phead->next = head->next;
	head->next->prev = phead;
	free(head);
	//erase(phead->next);
}

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

void insert(ListNode *pos, LTdataType x)
{
	assert(pos);
	ListNode *newnode = buy(x);
	ListNode *posPrev = pos->prev;
	posPrev->next = newnode;
	newnode->prev = posPrev;
	newnode->next = pos;
	pos->prev = newnode;
}

void erase(ListNode *pos)
{
	assert(pos);
	
	ListNode *posPrev = pos->prev;
	ListNode *posNext = pos->next;
	posPrev->next = posNext;
	posNext->prev = posPrev;
	free(pos);
}

void print(ListNode *phead)
{
	assert(phead);
	ListNode* cur = phead->next;

	while (cur!=phead)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

//test.c
#include"DList.h"

void test()
{
	ListNode* phead = Init();
	pushback(phead, 1);
	pushback(phead, 2);
	pushback(phead, 3);
	pushback(phead, 4);
	pushfront(phead, 0);
	print(phead);
	popfront(phead);
	print(phead);
	popback(phead);
	print(phead);
}
int main()
{
	test();
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值