双向循环 链表

首先,我们明确需要实现的功能:

#pragma once

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

typedef int LTDataType;

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

// 创建 结点.
ListNode* ListCreate(LTDataType x);

//初始化
ListNode* ListInit();

// 双向链表销毁
void ListDestory(ListNode* phead);

//  打印
void ListPrint(ListNode* phead);

//  尾插
void ListPushBack(ListNode* phead, LTDataType x);

//  尾删
void ListPopBack(ListNode* phead);

//  头插
void ListPushFront(ListNode* phead, LTDataType x);

//  头删
void ListPopFront(ListNode* phead);

//  查找
ListNode* ListFind(ListNode* phead, LTDataType x);

// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);

// 双向链表删除pos位置的结点
void ListErase(ListNode* pos);

//判断
bool ListEmpty(ListNode* phead);

//链表数
int ListSize(ListNode* phead);

实现功能的代码如下:

#include"ListNode.h"

//判断是否为空
bool ListEmpty(ListNode* phead) {

	assert(phead);

	return phead->next == phead;
}


//打印
void ListPrint(ListNode* phead) {
	assert(phead);

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

//创建
ListNode* ListCreate(LTDataType x) {
	ListNode* node = (ListNode*)malloc(sizeof(ListNode));
	if (node == NULL) {
		perror("malloc fail");
		exit(-1);
	  }
	node->data = x;
	node->next = NULL;
	node->prev = NULL;
	return node;

}

//头节点
ListNode* ListInit() {
	ListNode* phead = ListCreate(-1);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}

//尾插
void ListPushBack(ListNode* phead, LTDataType x) {
	assert(phead);

	//ListNode* newnode = ListCreate(x);
	//ListNode* tail = phead->prev;
	//tail->next = newnode;
	//newnode->prev = tail;
	//newnode->next = phead;
	//phead->prev = newnode;
	ListInsert(phead,x);

} 

//头插
void ListPushFront(ListNode* phead, LTDataType x) {

	assert(phead);

	//ListNode* newnode = ListCreate(x);
	//ListNode* head = phead->next;
	//phead->next = newnode;
	//newnode->prev = phead;
	//newnode->next = head;
	//head->prev = newnode;
	ListInsert(phead->next, x);

}

//尾删
void ListPopBack(ListNode* phead) {

	assert(phead);
	assert(!ListEmpty(phead));

	ListNode* tail = phead->prev;
	ListNode* tailprev = tail->prev;

	free(tail);

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


}

//头删
void ListPopFront(ListNode* phead) {
	
	assert(phead);
	assert(!ListEmpty(phead));

	ListNode* head = phead->next;
	ListNode* headnext = head->next;

	free(head);

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


}
//指定位置前一位插入
void ListInsert(ListNode* pos, LTDataType x) {
	
	assert(pos);
	ListNode* prev = pos->prev;
	ListNode* newnode = ListCreate(x); 

	prev->next = newnode;
	newnode ->prev = prev;
	newnode->next = pos;
	pos->prev = newnode;
}



//指定位置删除
void ListErase(ListNode* pos) {
	
	assert(pos);

	ListNode* prev = pos->prev;
	ListNode* next = pos->next;
	
	prev->next = next;
	next->prev = prev;

	free(pos);

	 

}
//计数
int ListSize(ListNode* phead) {

	assert(phead);
	ListNode* cur = phead->next;
	int size = 0;
	while (cur) {
		cur = cur->next;
		size++;
	}

	return size;

}

//销毁
void ListDestory(ListNode* phead) {
	assert(phead);
	ListNode* cur = phead->next;
	int size = 0;
	while (cur != phead) {
		ListNode* next = cur->next;
		ListErase(cur);
		cur = next;
	}
	free(phead);
}

这里面有一个细节是,删除操作与插入操作是可以使用其他功能的代码来实现的。这里可以减少面试过程中,实现代码的实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值