数据结构 不带头结点的链表

头文件

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 InitLinkList(linklist* plist);
//2.判空
bool IsEmpty(const linklist* plist);
//3.找到pos位置的节点并返回他的地址
node* FindPos(const linklist* plist, int pos);
//4.尾插
bool Push_Back(linklist* plist, int val);
//5.打印
void Print_List(const linklist* plist);
//6.头插
bool Push_Front(linklist* plist, int val);
//7.在pos位置插入
bool Push_Pos(linklist* plist, int pos, int val);
//8.尾删
bool Pop_Back(linklist* plist);
//9.头删
bool Pop_Front(linklist* plist);
//10.按位置删除
bool Pop_Index(linklist* plist, int Index);
//11.按值查找,返回当前节点,没有的话返回空
node* FindValue(const linklist* plist, int val);
//12.按值删除一次
bool Removeval(linklist* plist, int val);
//13.按值删除多次
bool MemoreRemoveval(linklist* plist, int val);
//14.清除链表中的元素
void ClearList(linklist* plist);
//15.销毁链表中的元素
void DestroyList(linklist* plist);
//16 反转链表 
node* Reverselist(linklist*plist);

源文件

cpp 复制代码
#include"不带头结点的链表.h"
node* buynode(int val);
//1.初始化
void InitLinkList(linklist* plist) {
	assert(plist != NULL);
	plist->head = NULL;
	plist->cursize = 0;
}
//2.判空
bool IsEmpty(const linklist* plist) {
	assert(plist != NULL);
	return plist->cursize == 0;
}
//3.找到pos位置的节点并返回他的地址
node* FindPos(const linklist* plist, int pos) {
	assert(plist != NULL);
	if (IsEmpty(plist) || pos<1|| pos>plist->cursize)return NULL;
	node* p = plist->head;
	while (--pos)p = p->next;
	return p;
}
//4.尾插
node* buynode(int val) {
	node* p = (node*)malloc(sizeof(node));
	if (p == NULL)return NULL;
	p->data = val;
	p->next = NULL;
	return p;
}
bool Push_Back(linklist* plist, int val) {
	assert(plist != NULL);
	node* p = plist->head; node* q = buynode(val);
	if (p == NULL) {
		plist->head = q;
		plist->cursize++;
		return true;
	}
	while (p->next != NULL)p = p->next;
	p->next = q;
	plist->cursize++;
	return true;
}
//5.打印
void Print_List(const linklist* plist) {
	assert(plist != NULL);
	printf("链表:");
	for (node* p = plist->head; p != NULL; p = p->next)printf("%d ", p->data);
	printf("\n");
}
//6.头插
bool Push_Front(linklist* plist, int val) {
	assert(plist != NULL);
	node* q = buynode(val);
	if (q == NULL)return false;
	q->next = plist->head;
	plist->head = q;
	plist->cursize++;
	return true;
}
//7.在pos位置插入
bool Push_Pos(linklist* plist, int pos, int val) {
	assert(plist != NULL);
	if (pos < 1|| pos > plist->cursize+1)return false;
	node* p = plist->head; node* q = buynode(val);
	if (pos == 1) {
		q->next = plist->head;
		plist->head = q;
		plist->cursize++;
		return true;
	}pos = pos - 1;
	while (--pos)p = p->next;
	q->next = p->next;
	p->next = q;
	plist->cursize++;
	return true;
}
//8.尾删
bool Pop_Back(linklist* plist) {
	assert(plist != NULL);
	if (IsEmpty(plist))return false;
	node* p = plist->head;
	if (p->next == NULL) {
		free(p);
		plist->head = NULL;
	}
	else {
		while (p->next->next != NULL)p = p->next;
		free(p->next); p->next = NULL;
	}
	plist->cursize--;
	return true;
}
//9.头删
bool Pop_Front(linklist* plist) {
	assert(plist != NULL);
	if (IsEmpty(plist))return false;
	node* p = plist->head;
	plist->head = p->next;
	free(p); p = NULL;
	plist->cursize--;
	return true;
}
//10.按位置删除
bool Pop_Index(linklist* plist, int Index) {
	assert(plist != NULL);
	if (IsEmpty(plist) || Index<1 || Index>plist->cursize)return false;
	node* p = plist->head;
	if (Index == 1) {
		Pop_Front(plist); return true;
	}
	Index--;
	while (--Index)p = p->next;
	node* q = p->next;
	p->next = q->next;
	free(q); q = NULL;
	plist->cursize--;
	return true;
}
//11.按值查找,返回当前节点,没有的话返回空
node* FindValue(const linklist* plist, int val) {
	assert(plist != NULL);
	if (IsEmpty(plist))return NULL;
	for (node* p = plist->head; p != NULL; p = p->next)
		if (p->data == val)return p;
	return NULL;
}
//12.按值删除一次
bool Removeval(linklist* plist, int val) {
	assert(plist != NULL);
	if (IsEmpty(plist))return false;
	if (plist->head->data == val)return Pop_Front(plist);
	for (node* p = plist->head; p->next != NULL; p = p->next)
		if (p->next->data == val) {
			node* q = p->next;
			p->next = q->next;
			free(q); q = NULL;
			plist->cursize--;
			return true;
		}
	return false;
}
//13.按值删除多次
bool MemoreRemoveval(linklist* plist, int val) {
	assert(plist != NULL);
	int i = plist->cursize;
	if (IsEmpty(plist))return false;
	while (plist->head->data == val) Pop_Front(plist);
	for (node* p = plist->head; p->next != NULL; p = p->next)
		if (p->next->data == val) {
			node* q = p->next;
			p->next = q->next;
			free(q); q = NULL;
			plist->cursize--;
		}
	return plist->cursize != i;
}
//14.清除链表中的元素
void ClearList(linklist* plist) {
	assert(plist != NULL);
	while (plist->head != NULL) {
		node* q = plist->head;
		plist->head = plist->head->next;
		free(q);
		q = NULL;
	}
	plist->cursize = 0;
}
//15.销毁链表中的元素
void DestroyList(linklist* plist) {
	assert(plist != NULL);
	while (plist->head != NULL) {
		node* q = plist->head;
		plist->head = plist->head->next;
		free(q);
		q = NULL;
	}
	plist->cursize = 0;
}
//16 反转链表 
node* Reverselist(linklist*plist) {
	assert(plist!= nullptr);
	if (IsEmpty(plist))return NULL;
	node* curr = plist->head;
	node* prev = nullptr;
	while (curr) {
		node* next = curr->next;
		curr->next = prev;
		prev = curr;
		curr = next;
	}
	return prev;
}

int main() {


}
相关推荐
希望有朝一日能如愿以偿1 小时前
力扣每日一题:使数组和能被p整除
数据结构·算法·leetcode
zz0723201 小时前
数据结构 —— 图
数据结构
代码游侠1 小时前
数据结构——线性表
linux·c语言·数据结构·学习·算法
潼心1412o1 小时前
数据结构(长期更新)第10讲:堆
数据结构
吃着火锅x唱着歌1 小时前
LeetCode 3371.识别数组中的最大异常值
数据结构·算法·leetcode
dringlestry1 小时前
B树的最大/最小高度
数据结构·b树
无敌最俊朗@1 小时前
链表-力扣hot100-LRU缓存-146
链表
黎梨梨梨_1 小时前
双向链表的实现
数据结构·链表
量子炒饭大师1 小时前
【一天一个计算机知识】—— 【编程百度】悬空指针
c语言·数据结构·c++·git·安全·github·dubbo