头文件
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 initlist(linklist *a);
//2.获取元素个数
int getsize(linklist* a);
//3.判空
bool isempty(const linklist* a);
//4.找到pos位置的节点并返回它地址
node* findpos(const linklist* a,int pos);
//5.找到pos节点的前驱返回它的地址
node* prevfindpos(const linklist* a, int pos);
//6.尾插
bool push_back(linklist* a, int val);
//7.头插
bool push_front(linklist* a, int val);
//8.在pos位置插入
bool push_pos(linklist* a, int pos, int val);
//9.打印
void print_list(const linklist* a);
//10.在ptr后面插入一个元素
bool push_ptr(linklist* a, node* ptr, int val);
//11.尾删
bool pop_back(linklist* a);
//12.头删
bool pop_front(linklist* a);
//13.按位置删除
bool pop_pos(linklist* a, int pos);
//14.按值查找,返回当前节点,没有返回空
node* findvalue(const linklist* a, int val);
//15.按值查找,返回当前节点前驱,没有的话返回空
node* prevfindvalue(const linklist* a, int val);
//16.按值删除一次
bool removeval(linklist* a, int val);
//17.按值删除多次
bool moreremoveval(linklist* a, int val);
//18.清除链表中的元素
void clearlist(linklist* a);
//19.销毁链表中的元素
void destroylist(linklist* a);
源文件
cpp
#include"带头节点的链表.h"
//1.初始化
void initlist(linklist*a) {
assert(a != nullptr);
a->cursize = 0;
a->head = (node*)malloc(sizeof(node));
if (a->head == nullptr) {
cout << "init err" << endl;
}
a->head->next = nullptr;
}
//2.获取元素个数
int getsize(linklist* a) {
assert(a != nullptr);
return a->cursize;
}
//3.判空
bool isempty(const linklist* a) {
assert(a != nullptr);
return a->cursize == 0;
}
//4.找到pos位置的节点并返回它地址
node* findpos(const linklist* a,int pos) {
assert(a != nullptr);
if (isempty(a) || pos<0 || pos>=a->cursize)return NULL;
node* p = a->head->next;
while (pos--) p=p->next;
return p;
}
//5.找到pos节点的前驱返回它的地址
node* prevfindpos(const linklist* a, int pos) {
assert(a != nullptr);
if (isempty(a) || pos<1 || pos>a->cursize)return NULL;
node* p = a->head;
while (pos--)p = p->next;
return p;
}
//6.尾插
bool push_back(linklist* a, int val) {
assert(a != nullptr);
node* p = a->head;
while (p->next) p = p->next;
node* q = (node*)malloc(sizeof(node));
if (q == nullptr)return false;
p->next = q;
q->data = val;
a->cursize++;
return true;
}
//7.头插
bool push_front(linklist* a, int val) {
assert(a != nullptr);
node* p = (node*)malloc(sizeof(node));
if (p == nullptr)return false;
p->data = val;
p->next = a->head->next;
a->head->next = p;
a->cursize++;
return true;
}
//8.在pos位置插入
bool push_pos(linklist* a, int pos, int val) {
assert(a != nullptr);
if (pos<0 || pos>a->cursize)return false;
node* p = (node*)malloc(sizeof(node));
if (p == nullptr)return false;
p->data = val;
node* q = a->head;
while (pos--) q = q->next;
node* k = q->next;
q->next = p;
p->next = k;
a->cursize++;
return true;
}
//9.打印
void print_list(const linklist* a) {
assert(a != nullptr);
if (isempty(a))return;
for (node* p = a->head->next; p != nullptr; p = p->next) {
cout << p->data << " ";
}cout << endl;
}
//10.在ptr后面插入一个元素
bool push_ptr(linklist* a, node* ptr, int val) {
assert(a != nullptr&&ptr!=nullptr);
node* p = (node*)malloc(sizeof(node));
if (p == nullptr)return false;
p->data = val;
p->next = ptr->next;
ptr->next = p;
a->cursize++;
return true;
}
//11.尾删
bool pop_back(linklist* a) {
assert(a != nullptr);
if (isempty(a))return false;
node* p = a->head;
while (p->next&&p->next->next)p = p->next;
free(p->next);
p->next = nullptr;
a->cursize--;
return true;
}
//12.头删
bool pop_front(linklist* a) {
assert(a != nullptr);
if (isempty(a))return false;
node* p = a->head->next;
a->head->next = p->next;
free(p);
a->cursize--;
return true;
}
//13.按位置删除
bool pop_pos(linklist* a, int pos) {
assert(a != nullptr);
if (isempty(a)||pos<0||pos>=a->cursize)return false;
node* p = a->head;
while (pos--)p = p->next;
node* q = p->next;
p->next = q->next;
free(q);
a->cursize--;
return true;
}
//14.按值查找,返回当前节点,没有返回空
node* findvalue(const linklist* a, int val) {
assert(a != nullptr);
if (isempty(a))return NULL;
node* p = a->head->next;
while (p) {
if (p->data == val)return p;
p = p->next;
}
return NULL;
}
//15.按值查找,返回当前节点前驱,没有的话返回空
node* prevfindvalue(const linklist* a, int val) {
assert(a != nullptr);
if (isempty(a))return NULL;
node* p = a->head;
while (p->next) {
if (p->next->data == val)return p;
p = p->next;
}
return NULL;
}
//16.按值删除一次
bool removeval(linklist* a, int val) {
assert(a != nullptr);
if (isempty(a))return false;
node*p=prevfindvalue(a, val);
if (p == nullptr)return false;
node* q = p->next;
p->next = q->next;
free(q);
a->cursize--;
return true;
}
//17.按值删除多次
bool moreremoveval(linklist* a, int val) {
assert(a != nullptr);
if (isempty(a))return false;
node* p = a->head;
while (p->next) {
if (p->next->data == val) {
node* q = p->next;
p->next = p->next->next;
free(q);
a->cursize--;
continue;
}
p = p->next;
}
return true;
}
//18.清除链表中的元素
void clearlist(linklist* a) {
assert(a != nullptr);
if (isempty(a))return;
node* p = a->head->next;
node* q = p->next;
while (q) {
free(p);
p = q;
q = q->next;
}
free(p);
a->head->next = nullptr;
a->cursize = 0;
}
//19.销毁链表中的元素
void destroylist(linklist* a) {
assert(a != nullptr);
clearlist(a);
a->head = nullptr;
}
int main() {
return 0;
}