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

头文件

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


}
相关推荐
历程里程碑11 分钟前
44. TCP -23Linux聊天室实现命令符功能
java·linux·开发语言·数据结构·c++·排序算法·tcp
丶小鱼丶13 分钟前
数据结构和算法之【二叉树】
java·数据结构·算法
Thomas.Sir19 分钟前
精通 MySQL 面试题
数据结构·数据库·mysql
罗湖老棍子26 分钟前
打鼹鼠_二维树状数组(信息学奥赛一本通- P1540)(二维树状数组模版题)
数据结构·算法·树状数组·二维树状数组
_日拱一卒26 分钟前
LeetCode:盛最多水的容器
数据结构·算法·leetcode
计算机安禾28 分钟前
【数据结构与算法】第2篇:C语言核心机制回顾(一):指针、数组与结构体
c语言·开发语言·数据结构·c++·算法·链表·visual studio
愣头不青43 分钟前
78.子集
数据结构·算法
北顾笙9801 小时前
测开准备-day04数据结构力扣
数据结构·算法·leetcode
jing-ya1 小时前
day 60 图论part11
java·数据结构·算法·图论
沉鱼.442 小时前
树的题目集
数据结构·算法