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

头文件

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() {


}
相关推荐
yangminlei3 小时前
MySQL玩转数据可视化
数据结构·sql·oracle
CodeByV3 小时前
【算法题】链表
数据结构·算法
wen__xvn4 小时前
代码随想录算法训练营DAY15第六章 二叉树part03
数据结构·算法·leetcode
有一个好名字4 小时前
力扣-链表最大孪生和
算法·leetcode·链表
无限进步_4 小时前
【C语言&数据结构】二叉树链式结构完全指南:从基础到进阶
c语言·开发语言·数据结构·c++·git·算法·visual studio
好奇龙猫4 小时前
大学院-筆記試験練習:线性代数和数据结构(8)
数据结构·线性代数
Dylan的码园5 小时前
深入浅出Java排序:从基础算法到实战优化(上)
java·数据结构·算法
学嵌入式的小杨同学5 小时前
循环队列(顺序存储)完整解析与实现(数据结构专栏版)
c语言·开发语言·数据结构·c++·算法
jimy15 小时前
消息队列Message Queue(MQ),队列链表(queue),消费者,生产者
数据结构·链表