带头节点的双向循环链表

数据结构 专栏收录该内容
5 篇文章 0 订阅
//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
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值