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

头文件

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


}
相关推荐
琢磨先生David5 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
qq_454245035 天前
基于组件与行为的树状节点系统
数据结构·c#
超级大福宝5 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
岛雨QA5 天前
常用十种算法「Java数据结构与算法学习笔记13」
数据结构·算法
weiabc5 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
wefg15 天前
【算法】单调栈和单调队列
数据结构·算法
岛雨QA5 天前
图「Java数据结构与算法学习笔记12」
数据结构·算法
czxyvX5 天前
020-C++之unordered容器
数据结构·c++
岛雨QA5 天前
多路查找树「Java数据结构与算法学习笔记11」
数据结构·算法
AKA__Zas5 天前
初识基本排序
java·数据结构·学习方法·排序