前言
大家好我是東木,在CSDN上持续更新更多原创文章,唯一指定传送门:東木的博客
本文将分享一下自己用C++实现my_forward_list类,本质上是一个带空头节点的单链表。其private部分包括一个记录长度的变量m_len,还有一个ListNode类型的指针。在此Header file中,默认链表第一个为1而非0,忽略第一个空的头节点,并且,为了简化程序,在本文我不采取C++的异常机制。程序有不足的地方还请大家多多指正!
my_forward_list代码块
//
// Created by dongMu on 2020/9/24.
//
#ifndef my_forward_list_H
#define my_forward_list_H
#include "bits/stdc++.h"
#include <ciso646>
using namespace std;
template<class T>
struct ListNode {
T data;//数据域
struct ListNode *next;//指针域
};
template<class T>
class my_forward_list {
protected:
typedef struct ListNode<T> node;
typedef struct ListNode<T> *ptr_node;
private:
ptr_node m_head;//头指针
int m_len;
public:
//构造函数
my_forward_list(const T &val = 0);
//拷贝构造函数
my_forward_list(const my_forward_list<T> &list);
//拷贝赋值函数
my_forward_list<T> &operator=(const my_forward_list<T> &list);
//析构函数
~my_forward_list();
//尾插法
bool addLast(const T &val);
//头插法
bool addFirst(const T &val);
//求单链表的长度1
inline int length() const {
cout << m_len << endl;
return m_len;
}
//求单链表的长度2
inline int size() const {
cout << m_len << endl;
return m_len;
}
//按位置取元素
T getVal(const int &index);
//按位置插入元素
bool insert(const T &val, const int &index);
//按位置删除结点
bool del(const int &index);
//输出链表
inline bool printList();
//删除有序表中重复的元素
bool listDelDuplicates();
//销毁链表
inline bool destroy() {
delete this;
return true;
}
};
template<class T>
inline my_forward_list<T>::my_forward_list(const T &val):m_len(0), m_head(nullptr) {
m_head = new node;
m_head->next = nullptr;
m_head->data = 0;
if (val != 0) {
addLast(val);
}
}
template<class T>
my_forward_list<T>::my_forward_list(const my_forward_list<T> &list) {
m_head = new node;
m_head->next = nullptr;
m_head->data = 0;
m_len = 0;
for (ptr_node q = list.m_head->next; q != nullptr; q = q->next) {
addLast(q->data);
}
}
template<class T>
my_forward_list<T>::~my_forward_list() {
//创建两个node指针
ptr_node p, tmp;
//p指向头
p = m_head;
while (p) {
tmp = p->next;
delete p;
p = tmp;
}
m_head = nullptr;
}
template<class T>
my_forward_list<T> &my_forward_list<T>::operator=(const my_forward_list<T> &list) {
//清空原来的数据
//创建两个node指针
ptr_node p, tmp;
//p指向头
p = m_head;
while (p) {
tmp = p->next;
delete p;
p = tmp;
}
m_head = nullptr;
for (ptr_node q = list.m_head->next; q != nullptr; q = q->next) {
this->addLast(q->data);
}
return *this;
}
template<class T>
bool my_forward_list<T>::addLast(const T &val) {
//创建一个新节点,将val,赋值进去
ptr_node p = new node;
p->data = val;
p->next = nullptr;
//创建一个可移动node结构指针去找到最后一个结点
ptr_node last = m_head->next;
if (last) {
while (last->next) {
//一直找到末尾
last = last->next;
}
//连接
last->next = p;
} else {
m_head->next = p;
}
//长度加1
m_len++;
return true;
}
template<class T>
bool my_forward_list<T>::addFirst(const T &val) {
//创建一个新的结点
ptr_node p = new node;
//attach过程
p->data = val;
p->next = m_head->next;
m_head->next = p;
m_len++;
return true;
}
template<class T>
T my_forward_list<T>::getVal(const int &index) {
if (index <= 0) {
cout << "位置 index不可以小于或者等于零!" << endl;
return -1;//错误码
}
if (index > m_len) {
cout << "位置index大于my_forward_list长度!" << endl;
return -2;//错误码
}
ptr_node p = m_head->next;
int j = 1;
while (j != index) {
p = p->next;
j++;
}
return p->data;
}
template<class T>
bool my_forward_list<T>::insert(const T &val, const int &index) {
if (index <= 0) {
cout << "位置index不可以小于或者等于零!" << endl;
return -1;//错误码
}
if (index > m_len) {
cout << "位置index大于my_forward_list长度!" << endl;
return -2;//错误码
}
ptr_node p = m_head;
int j = 1;
while (j != index) //搜索i-1结点指针p
{
p = p->next;//p指向下一个节点
j++;
} //k=i-1或p=NULL退出
node *s = new node;// 创建一个新节点
s->data = val;//装入数据
//插入新节点
s->next = p->next;
p->next = s;
m_len++;
return true;
}
template<class T>
inline bool my_forward_list<T>::printList() {
for (ptr_node p = m_head->next; p != nullptr; p = p->next) {
cout << p->data << " ";
}
cout << endl;
return true;
}
template<class T>
bool my_forward_list<T>::del(const int &index) {
//i应该从1开始如果i<=0则不执行
if (index <= 0) {
cout << "index不可以小于或者等于零!" << endl;
return -1;
}
ptr_node p = m_head;
int k = 1;
while ((k != index) and (p != nullptr)) { //搜索ai-1节点指针
p = p->next;
k++;
}
if (p == nullptr or p->next == nullptr) {
cout << "删除位置的 index 超出范围" << endl;
} else {
ptr_node u = p->next;
p->next = u->next;
delete u; //释放删除节点占据的内存空间,此句必须,
//否则这个节点的内存将成为垃圾内存
}
m_len--;
return true;
}
template<class T>
bool my_forward_list<T>::listDelDuplicates() {
ptr_node p = m_head->next;//P移动寻找相同值
ptr_node flag = m_head;//flag作为标杆值
while (p) {
if (flag->data == p->data)//若flag的值和p的值相同时
{
ptr_node q = p;//定义一个q去指向p,目的是为了释放内存
flag->next = p->next;//flag的next指向p的next
p = p->next;//p指向下一个结点
delete q;//释放q所指的结点
} else//若两者的值不同时,flag和p都向下一个移动
{
flag = p;
p = p->next;
}
}
m_len--;
return true;
}
#endif //my_forward_list_H