数据结构 带头节点的链表

头文件

cpp 复制代码
#pragma once
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
using namespace std;
typedef struct node {
	int data;
	struct node* next;
}node;

typedef struct linklist {
	node* head;
	int cursize;
}linklist;
//1.初始化
void initlist(linklist *a);
//2.获取元素个数
int getsize(linklist* a);
//3.判空
bool isempty(const linklist* a);
//4.找到pos位置的节点并返回它地址
node* findpos(const linklist* a,int pos);
//5.找到pos节点的前驱返回它的地址
node* prevfindpos(const linklist* a, int pos);
//6.尾插
bool push_back(linklist* a, int val);
//7.头插
bool push_front(linklist* a, int val);
//8.在pos位置插入
bool push_pos(linklist* a, int pos, int val);
//9.打印
void print_list(const linklist* a);
//10.在ptr后面插入一个元素
bool push_ptr(linklist* a, node* ptr, int val);
//11.尾删
bool pop_back(linklist* a);
//12.头删
bool pop_front(linklist* a);
//13.按位置删除
bool pop_pos(linklist* a, int pos);
//14.按值查找,返回当前节点,没有返回空
node* findvalue(const linklist* a, int val);
//15.按值查找,返回当前节点前驱,没有的话返回空
node* prevfindvalue(const linklist* a, int val);
//16.按值删除一次
bool removeval(linklist* a, int val);
//17.按值删除多次
bool moreremoveval(linklist* a, int val);
//18.清除链表中的元素
void clearlist(linklist* a);
//19.销毁链表中的元素
void destroylist(linklist* a);

源文件

cpp 复制代码
#include"带头节点的链表.h"
//1.初始化
void initlist(linklist*a) {
	assert(a != nullptr);
	a->cursize = 0;
	a->head = (node*)malloc(sizeof(node));
	if (a->head == nullptr) {
		cout << "init err" << endl;
	}
	a->head->next = nullptr;
}
//2.获取元素个数
int getsize(linklist* a) {
	assert(a != nullptr);
	return a->cursize;
}
//3.判空
bool isempty(const linklist* a) {
	assert(a != nullptr);
	return a->cursize == 0;
}
//4.找到pos位置的节点并返回它地址
node* findpos(const linklist* a,int pos) {
	assert(a != nullptr);
	if (isempty(a) || pos<0 || pos>=a->cursize)return NULL;
	node* p = a->head->next;
	while (pos--) p=p->next;
	return p;
}
//5.找到pos节点的前驱返回它的地址
node* prevfindpos(const linklist* a, int pos) {
	assert(a != nullptr);
	if (isempty(a) || pos<1 || pos>a->cursize)return NULL;
	node* p = a->head;
	while (pos--)p = p->next;
	return p;
}
//6.尾插
bool push_back(linklist* a, int val) {
	assert(a != nullptr);
	node* p = a->head;
	while (p->next) p = p->next;
	node* q = (node*)malloc(sizeof(node));
	if (q == nullptr)return false;
	p->next = q;
	q->data = val;
	a->cursize++;
	return true;
}
//7.头插
bool push_front(linklist* a, int val) {
	assert(a != nullptr);
	node* p = (node*)malloc(sizeof(node));
	if (p == nullptr)return false;
	p->data = val;
	p->next = a->head->next;
	a->head->next = p;
	a->cursize++;
	return true;
}
//8.在pos位置插入
bool push_pos(linklist* a, int pos, int val) {
	assert(a != nullptr);
	if (pos<0 || pos>a->cursize)return false;
	node* p = (node*)malloc(sizeof(node));
	if (p == nullptr)return false;
	p->data = val;
	node* q = a->head;
	while (pos--) q = q->next;
	node* k = q->next;
	q->next = p;
	p->next = k;
	a->cursize++;
	return true;
}
//9.打印
void print_list(const linklist* a) {
	assert(a != nullptr);
	if (isempty(a))return;
	for (node* p = a->head->next; p != nullptr; p = p->next) {
		cout << p->data << " ";
	}cout << endl;
}
//10.在ptr后面插入一个元素
bool push_ptr(linklist* a, node* ptr, int val) {
	assert(a != nullptr&&ptr!=nullptr);
	node* p = (node*)malloc(sizeof(node));
	if (p == nullptr)return false;
	p->data = val;
	p->next = ptr->next;
	ptr->next = p;
	a->cursize++;
	return true;
}
//11.尾删
bool pop_back(linklist* a) {
	assert(a != nullptr);
	if (isempty(a))return false;
	node* p = a->head;
	while (p->next&&p->next->next)p = p->next;
	free(p->next);
	p->next = nullptr;
	a->cursize--;
	return true;
}
//12.头删
bool pop_front(linklist* a) {
	assert(a != nullptr);
	if (isempty(a))return false;
	node* p = a->head->next;
	a->head->next = p->next;
	free(p);
	a->cursize--;
	return true;
}
//13.按位置删除
bool pop_pos(linklist* a, int pos) {
	assert(a != nullptr);
	if (isempty(a)||pos<0||pos>=a->cursize)return false;
	node* p = a->head;
	while (pos--)p = p->next;
	node* q = p->next;
	p->next = q->next;
	free(q);
	a->cursize--;
	return true;
}
//14.按值查找,返回当前节点,没有返回空
node* findvalue(const linklist* a, int val) {
	assert(a != nullptr);
	if (isempty(a))return NULL;
	node* p = a->head->next;
	while (p) {
		if (p->data == val)return p;
		p = p->next;
	}
	return NULL;
}
//15.按值查找,返回当前节点前驱,没有的话返回空
node* prevfindvalue(const linklist* a, int val) {
	assert(a != nullptr);
	if (isempty(a))return NULL;
	node* p = a->head;
	while (p->next) {
		if (p->next->data == val)return p;
		p = p->next;
	}
	return NULL;
}
//16.按值删除一次
bool removeval(linklist* a, int val) {
	assert(a != nullptr);
	if (isempty(a))return false;
	node*p=prevfindvalue(a, val);
	if (p == nullptr)return false;
	node* q = p->next;
	p->next = q->next;
	free(q);
	a->cursize--;
	return true;
}
//17.按值删除多次
bool moreremoveval(linklist* a, int val) {
	assert(a != nullptr);
	if (isempty(a))return false;
	node* p = a->head;
	while (p->next) {
		if (p->next->data == val) {
			node* q = p->next;
			p->next = p->next->next;
			free(q);
			a->cursize--;
			continue;
		}
		p = p->next;
	}
	return true;
}
//18.清除链表中的元素
void clearlist(linklist* a) {
	assert(a != nullptr);
	if (isempty(a))return;
	node* p = a->head->next;
	node* q = p->next;
	while (q) {
		free(p);
		p = q;
		q = q->next;
	}
	free(p);
	a->head->next = nullptr;
	a->cursize = 0;
}
//19.销毁链表中的元素
void destroylist(linklist* a) {
	assert(a != nullptr);
	clearlist(a);
	a->head = nullptr;
}


int main() {


	return 0;
}
相关推荐
Yvonne爱编码12 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
熬夜有啥好13 小时前
数据结构——哈希表
数据结构·散列表
我能坚持多久14 小时前
【初阶数据结构01】——顺序表专题
数据结构
rainbow688915 小时前
深入解析C++STL:map与set底层奥秘
java·数据结构·算法
果果燕15 小时前
今日学习笔记:双向链表、循环链表、栈
笔记·学习·链表
wangjialelele16 小时前
平衡二叉搜索树:AVL树和红黑树
java·c语言·开发语言·数据结构·c++·算法·深度优先
xuxie9916 小时前
day 21 双向链表以及循环链表
数据结构·链表
历程里程碑17 小时前
普通数组----合并区间
java·数据结构·python·算法·leetcode·职场和发展·tornado
梵刹古音17 小时前
【C语言】 指针与数据结构操作
c语言·数据结构·算法
爱敲代码的TOM19 小时前
数据结构总结
数据结构