数据结构 带头节点的双向循环链表

头文件

cpp 复制代码
#pragma once
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
using namespace std;

typedef struct node {
	int data;
	node* next;
	node* prev;
}node;

typedef struct linklist {
	int cursize;
	node* head;
}linklist;

//1.初始化
node* buynode(int val);
void initlist(linklist* ps);
//2.在一个节点前插入元素
bool insertelem(linklist* ps, node* ptr, int val);
//3.打印
void print_(linklist* ps);
//4.删除节点
bool erasenode(linklist* ps,node* ptr);
//5.头删
bool erasefront(linklist* ps);
//6.尾删
bool eraselast(linklist* ps);
//7.头插
bool insertfront(linklist* ps, int val);
//8.尾插
bool insertback(linklist* ps, int val);

源文件

cpp 复制代码
#include"带头节点双向循环链表.h"


//1.初始化
node* buynode(int val) {
	node* p = (node*)malloc(sizeof(node));
	if (p == nullptr) {
		cout << "申请内存失败" << endl;
		return NULL;
	}
	p->data = val;
	p->next = p;
	p->prev = p;
	return p;
}
void initlist(linklist* ps) {
	assert(ps != nullptr);
	ps->head = buynode(0);
	ps->cursize = 0;
}
//2.在一个节点前插入元素
bool insertelem(linklist* ps, node* ptr, int val) {
	assert(ps != nullptr && ptr != nullptr);
	node* p = buynode(val);
	p->next = ptr;
	p->prev = ptr->prev;
	ptr->prev->next = p;
	ptr->prev = p;
	ps->cursize++;
	return true;
}
//3.打印
void print_(linklist* ps) {
	assert(ps != nullptr);
	for (node* p = ps->head->next; p != ps->head; p = p->next) {
		cout << p->data << " ";
	}cout << endl;
}
//4.删除节点
bool erasenode(linklist* ps, node* ptr) {
	assert(ps != nullptr&&ptr!=nullptr);
	if (ptr == ps->head)return false;
	ptr->prev->next = ptr->next;
	ptr->next->prev = ptr->prev;
	free(ptr);
	ps->cursize--;
	return true;
}
//5.头删
bool erasefront(linklist* ps) {
	assert(ps != nullptr);
	if (ps->cursize == 0) return false;
	node* p = ps->head->next;
	ps->head->next = p->next;
	p->next->prev = ps->head;
	free(p);
	ps->cursize--;
	return true;
}
//6.尾删
bool eraselast(linklist* ps) {
	assert(ps != nullptr);
	if (ps->cursize == 0) return false;
	node* p = ps->head->prev;
	ps->head->prev = ps->head->prev->prev;
	ps->head->prev->next = ps->head;
	free(p);
	ps->cursize--;
	return true;
}
//7.头插
bool insertfront(linklist* ps, int val) {
	assert(ps != nullptr);
	node* p = buynode(val);
	ps->head->next->prev = p;
	p->next = ps->head->next;
	ps->head->next = p;
	p->prev = ps->head;
	ps->cursize++;
	return true;
}
//8.尾插
bool insertback(linklist* ps, int val) {
	assert(ps != nullptr);
	node* p = buynode(val);
	ps->head->prev->next = p;
	p->prev = ps->head->prev;
	p->next = ps->head;
	ps->head->prev = p;
	ps->cursize++;
	return true;
}
int main() {

	return 0;
}
相关推荐
皓月斯语1 小时前
B3842 [GESP202306 三级] 春游 题解
数据结构·c++·算法·题解
atunet1 小时前
树状结构在查询优化中的作用与实现细节7
算法
徐凤年_1 小时前
rog_map参数理解
算法
春日见1 小时前
算法与数据结构----哈希表
数据结构·人工智能·算法·机器学习·自动驾驶·哈希算法·散列表
萌动的小火苗2 小时前
嵌入式开发中的栈与队列:任务调度为什么依赖数据结构
数据结构·c++·单片机·嵌入式硬件
叩码以求索2 小时前
统计按位或能得到最大值的子集数目(一)
数据结构·算法
tachibana22 小时前
hot100 数组中的第K个最大元素(215)
java·数据结构·算法·leetcode
星恒随风2 小时前
C++ STL 栈详解:stack 的使用、经典题目与简单模拟实现
开发语言·数据结构·c++·笔记·学习
txzrxz3 小时前
单调队列讲解
数据结构·c++·算法·单调队列
不会就选b3 小时前
算法日常・每日刷题--<快排>4
算法