文件结构 :
文件名字 | 用途 |
CmakeList.txt | cmake文件 |
how.md | 简述思路以及其他说明 |
main.cpp | 主测试程序 |
SingLinkedList.cpp | 核心实现文件 |
SingLinkedList.h | 核心头文件 |
SingleLinkedList.cpp
//
// Created by A Luck Boy on 2023/1/13.
//
#include "SingleLinkedList.h"
LinkedList::LinkedList() {
head = new Node(0);
length = 0;
cout << "创建成功!"<<endl;
}
LinkedList:: ~LinkedList(){
Node *curr = head;
while (curr != nullptr) {
Node *next = curr->next;
delete curr;
curr = next;
}
cout <<"已经销毁"<<endl;
}
void LinkedList::append(int data) {
Node* newNode = new Node(data);
Node* curr = head;
while (curr->next != nullptr) {
curr = curr->next;
}
curr->next = newNode;
length++;
}
bool LinkedList::isEmpty()const {
return length == 0;
}
int LinkedList::getLength() const{
return length;
}
bool LinkedList::insert(int data, int location) {
// attention : location <= len-1 and location should be no less than 0
if (location < 0 || location > length-1) {
cout << "插入索引不在范围,索引应该不小于0并且不大于" << length-1 << endl;
return false;
}
Node * newNode = new Node(data);
Node * curr = head;
for (int i = 0; i < location; i++) {
curr = curr->next;
}
newNode->next = curr->next;
curr->next = newNode;
length++;
return true;
};
bool LinkedList::remove(int location) {
// be similar to the insert method
if (location < 0 ||location > length-1) {
cout << "删除索引数据不在范围,索引应该不小于0并且不大于" << length-1 << endl;
return false;
}
Node* curr = head;
for (int i = 0; i <location; i++) {
curr = curr->next;
}
Node* toDelete = curr->next;
curr->next = toDelete->next;
delete toDelete;
length--;
return true;
}
Node * LinkedList::find(int location) const{
if (location < 0 || location > length-1) {
cout << "删除索引数据不在范围,索引应该不小于0并且不大于" << length-1 << endl;
return nullptr;
}
Node* curr = head->next;
for (int i = 0; i < location; i++) {
curr = curr->next;
}
return curr;
}
SingleLinkedList.h
//
// Created by A Luck Boy on 2023/1/13.
//
// 结点类
#include <iostream>
using namespace std;
#define Waring cout << "这里代码有问题!" << endl;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
// 链表
class LinkedList {
public:
Node *head;
int length;
// 创建
LinkedList();
// 销毁
~LinkedList();
// 尾部添加
void append(int data);
// 判空
bool isEmpty() const;
// 表长
int getLength() const;
// 插入
bool insert(int data, int location);
// 删除
bool remove(int location) ;
// 查找
Node* find(int location) const;
};
main.cpp
//
// Created by A Luck Boy on 2023/1/13.
//
#include "SingleLinkedList.h"
int main(){
LinkedList list;
// 判空试试
if (list.isEmpty()){cout << "空的" << endl;}
else {Waring}
// 尾部添加数据
for (int i=0;i<10;i++){
list.append((1+i)*(i+2));
}
// 添加了十次,那么长度肯定是十,来确认一下
cout << "Length is " << list.getLength() <<endl;
// 遍历试试
Node *head = list.head;
for (int i=0;i<list.getLength();i++){
head = head->next;
cout << "Index " << i << " is number " << head->data <<endl;
}
delete head;
// 插入操作 : 我会操作三次,分别是 : 前两次检测溢出索引时插入的情况和索引范围内插入,当前最大索引是9
// 另外,position参数是正常的顺序,即 索引+1 的数字
if (list.insert(1000, -1) || list.insert(1000, 10))
{
Waring
} else{
cout << "检测出索引不在范围,程序正确!" << endl;
}
if (list.insert(1000, 5)){
cout << "已经在索引5插入新数据,长度变成了11,下面直接打印所有证明\n";
// 遍历试试
Node *another_head = list.head;
for (int i=0;i<list.getLength();i++){
another_head = another_head->next;
cout << "Index " << i << " is number " << another_head->data <<endl;
}
delete another_head;
} else{
Waring
}
// 操作三次,思路同上
if (list.remove(-1) || list.remove(list.getLength())){
Waring
} else{
cout << "检测出索引不在范围,程序正确!" << endl;
}
// 把索引四的30删掉,然后验证
if (list.remove(4))
{
cout << "验证正确性 " <<endl;
Node *another_head2 = list.head;
for (int i=0;i<list.getLength();i++){
another_head2 = another_head2->next;
cout << "Index " << i << " is number " << another_head2->data <<endl;
}
delete another_head2;
} else{Waring}
// 查找操作,操作三次
// 注意,我这里是查找的结点,函数返回值固然是一个结点类型
// 前两次仍然测试溢出检查
Node *check_mistake_Node = list.find(-1);
Node *check_mistake_Node_2 = list.find(100);
if ((check_mistake_Node == nullptr) or (check_mistake_Node_2 == nullptr)){
cout << "验证了程序正确!"<<endl;
} else{Waring}
delete check_mistake_Node, check_mistake_Node_2;
// 第三次,试图找索引5上面的结点,数据是42
Node * myFindNode = list.find(5);
cout << "Index 5 number is " << myFindNode->data << endl;
return 0;
}
终端运行结果
创建成功!
空的
Length is 10
Index 0 is number 2
Index 1 is number 6
Index 2 is number 12
Index 3 is number 20
Index 4 is number 30
Index 5 is number 42
Index 6 is number 56
Index 7 is number 72
Index 8 is number 90
Index 9 is number 110
插入索引不在范围,索引应该不小于0并且不大于9
插入索引不在范围,索引应该不小于0并且不大于9
检测出索引不在范围,程序正确!
已经在索引5插入新数据,长度变成了11,下面直接打印所有证明
Index 0 is number 2
Index 1 is number 6
Index 2 is number 12
Index 3 is number 20
Index 4 is number 30
Index 5 is number 1000
Index 6 is number 42
Index 7 is number 56
Index 8 is number 72
Index 9 is number 90
Index 10 is number 110
删除索引数据不在范围,索引应该不小于0并且不大于10
删除索引数据不在范围,索引应该不小于0并且不大于10
检测出索引不在范围,程序正确!
验证正确性
Index 0 is number 2
Index 1 is number 6
Index 2 is number 12
Index 3 is number 20
Index 4 is number 1000
Index 5 is number 42
Index 6 is number 56
Index 7 is number 72
Index 8 is number 90
Index 9 is number 110
删除索引数据不在范围,索引应该不小于0并且不大于9
删除索引数据不在范围,索引应该不小于0并且不大于9
验证了程序正确!
Index 5 number is 42
已经销毁
源代码链接 :
无聊的备注 :
此刻是2023.1.13,夜里22:32,夜大雨 ,天气预报写着6°,还有,王菲的《归途有风》很好听,循环播放着……