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

头文件

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


}
相关推荐
qeen8715 小时前
【数据结构】建堆的时间复杂度讨论与TOP-K问题
c语言·数据结构·c++·学习·
图码15 小时前
如何用多种方法判断字符串是否为回文?
开发语言·数据结构·c++·算法·阿里云·线性回归·数字雕刻
我星期八休息16 小时前
IT疑难杂症诊疗室:AI时代工程师Superpowers进化论
linux·开发语言·数据结构·人工智能·python·散列表
漂流瓶jz16 小时前
UVA-1152 和为0的4个值 题解答案代码 算法竞赛入门经典第二版
数据结构·算法·二分查找·题解·aoapc·算法竞赛入门经典·uva
你撅嘴真丑16 小时前
map 与 set容器的应用--话题焦点人物
数据结构
生成论实验室16 小时前
《事件关系阴阳博弈动力学:识势应势之道》第二篇:阴阳博弈——认知的动力学基础
数据结构·人工智能·科技·神经网络·算法
li16709027017 小时前
第二十七章:智能指针
c语言·数据结构·c++·visual studio
风筝在晴天搁浅17 小时前
LeetCode 92.反转链表Ⅱ
算法·leetcode·链表
WL_Aurora18 小时前
Python 算法基础篇之链表
python·算法·链表
代码中介商19 小时前
数据结构开篇:从问题到解决方案
数据结构