Link_List file 改名为 List
List 更改成模板类 添加了 转换函数(重载) 添加了 单链 单体 单键 设计模式
This commit is contained in:
parent
d797c2f62e
commit
2afe10c1f7
@ -3,4 +3,4 @@ project(MSTL)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
|
||||
add_executable(MSTL main.cpp h/String.h h/LinkList.h)
|
||||
add_executable(MSTL main.cpp h/String.h h/List.h h/Singleton.h h/Faction.h)
|
||||
|
@ -1,60 +0,0 @@
|
||||
{
|
||||
"configurations" :
|
||||
[
|
||||
{
|
||||
"directories" :
|
||||
[
|
||||
{
|
||||
"build" : ".",
|
||||
"jsonFile" : "directory-.-Debug-f5ebdc15457944623624.json",
|
||||
"minimumCMakeVersion" :
|
||||
{
|
||||
"string" : "3.23"
|
||||
},
|
||||
"projectIndex" : 0,
|
||||
"source" : ".",
|
||||
"targetIndexes" :
|
||||
[
|
||||
0
|
||||
]
|
||||
}
|
||||
],
|
||||
"name" : "Debug",
|
||||
"projects" :
|
||||
[
|
||||
{
|
||||
"directoryIndexes" :
|
||||
[
|
||||
0
|
||||
],
|
||||
"name" : "MSTL",
|
||||
"targetIndexes" :
|
||||
[
|
||||
0
|
||||
]
|
||||
}
|
||||
],
|
||||
"targets" :
|
||||
[
|
||||
{
|
||||
"directoryIndex" : 0,
|
||||
"id" : "MSTL::@6890427a1f51a3e7e1df",
|
||||
"jsonFile" : "target-MSTL-Debug-7d2d0f05e169997e8a2f.json",
|
||||
"name" : "MSTL",
|
||||
"projectIndex" : 0
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"kind" : "codemodel",
|
||||
"paths" :
|
||||
{
|
||||
"build" : "/home/dongl/code/MSTL/cmake-build-debug",
|
||||
"source" : "/home/dongl/code/MSTL"
|
||||
},
|
||||
"version" :
|
||||
{
|
||||
"major" : 2,
|
||||
"minor" : 4
|
||||
}
|
||||
}
|
@ -1,108 +0,0 @@
|
||||
{
|
||||
"cmake" :
|
||||
{
|
||||
"generator" :
|
||||
{
|
||||
"multiConfig" : false,
|
||||
"name" : "Ninja"
|
||||
},
|
||||
"paths" :
|
||||
{
|
||||
"cmake" : "/home/dongl/app/ide/clion/clion-2022.2.1/bin/cmake/linux/bin/cmake",
|
||||
"cpack" : "/home/dongl/app/ide/clion/clion-2022.2.1/bin/cmake/linux/bin/cpack",
|
||||
"ctest" : "/home/dongl/app/ide/clion/clion-2022.2.1/bin/cmake/linux/bin/ctest",
|
||||
"root" : "/home/dongl/app/ide/clion/clion-2022.2.1/bin/cmake/linux/share/cmake-3.23"
|
||||
},
|
||||
"version" :
|
||||
{
|
||||
"isDirty" : false,
|
||||
"major" : 3,
|
||||
"minor" : 23,
|
||||
"patch" : 2,
|
||||
"string" : "3.23.2",
|
||||
"suffix" : ""
|
||||
}
|
||||
},
|
||||
"objects" :
|
||||
[
|
||||
{
|
||||
"jsonFile" : "codemodel-v2-05830bec4f0034487f3b.json",
|
||||
"kind" : "codemodel",
|
||||
"version" :
|
||||
{
|
||||
"major" : 2,
|
||||
"minor" : 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"jsonFile" : "cache-v2-471ef0fba02a766483a6.json",
|
||||
"kind" : "cache",
|
||||
"version" :
|
||||
{
|
||||
"major" : 2,
|
||||
"minor" : 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"jsonFile" : "cmakeFiles-v1-7c70437388afc6a20846.json",
|
||||
"kind" : "cmakeFiles",
|
||||
"version" :
|
||||
{
|
||||
"major" : 1,
|
||||
"minor" : 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"jsonFile" : "toolchains-v1-d06480eadcc461d85546.json",
|
||||
"kind" : "toolchains",
|
||||
"version" :
|
||||
{
|
||||
"major" : 1,
|
||||
"minor" : 0
|
||||
}
|
||||
}
|
||||
],
|
||||
"reply" :
|
||||
{
|
||||
"cache-v2" :
|
||||
{
|
||||
"jsonFile" : "cache-v2-471ef0fba02a766483a6.json",
|
||||
"kind" : "cache",
|
||||
"version" :
|
||||
{
|
||||
"major" : 2,
|
||||
"minor" : 0
|
||||
}
|
||||
},
|
||||
"cmakeFiles-v1" :
|
||||
{
|
||||
"jsonFile" : "cmakeFiles-v1-7c70437388afc6a20846.json",
|
||||
"kind" : "cmakeFiles",
|
||||
"version" :
|
||||
{
|
||||
"major" : 1,
|
||||
"minor" : 0
|
||||
}
|
||||
},
|
||||
"codemodel-v2" :
|
||||
{
|
||||
"jsonFile" : "codemodel-v2-05830bec4f0034487f3b.json",
|
||||
"kind" : "codemodel",
|
||||
"version" :
|
||||
{
|
||||
"major" : 2,
|
||||
"minor" : 4
|
||||
}
|
||||
},
|
||||
"toolchains-v1" :
|
||||
{
|
||||
"jsonFile" : "toolchains-v1-d06480eadcc461d85546.json",
|
||||
"kind" : "toolchains",
|
||||
"version" :
|
||||
{
|
||||
"major" : 1,
|
||||
"minor" : 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,120 +0,0 @@
|
||||
{
|
||||
"artifacts" :
|
||||
[
|
||||
{
|
||||
"path" : "MSTL"
|
||||
}
|
||||
],
|
||||
"backtrace" : 1,
|
||||
"backtraceGraph" :
|
||||
{
|
||||
"commands" :
|
||||
[
|
||||
"add_executable"
|
||||
],
|
||||
"files" :
|
||||
[
|
||||
"CMakeLists.txt"
|
||||
],
|
||||
"nodes" :
|
||||
[
|
||||
{
|
||||
"file" : 0
|
||||
},
|
||||
{
|
||||
"command" : 0,
|
||||
"file" : 0,
|
||||
"line" : 6,
|
||||
"parent" : 0
|
||||
}
|
||||
]
|
||||
},
|
||||
"compileGroups" :
|
||||
[
|
||||
{
|
||||
"compileCommandFragments" :
|
||||
[
|
||||
{
|
||||
"fragment" : "-g"
|
||||
},
|
||||
{
|
||||
"fragment" : "-std=gnu++14"
|
||||
}
|
||||
],
|
||||
"language" : "CXX",
|
||||
"languageStandard" :
|
||||
{
|
||||
"backtraces" :
|
||||
[
|
||||
1
|
||||
],
|
||||
"standard" : "14"
|
||||
},
|
||||
"sourceIndexes" :
|
||||
[
|
||||
0
|
||||
]
|
||||
}
|
||||
],
|
||||
"id" : "MSTL::@6890427a1f51a3e7e1df",
|
||||
"link" :
|
||||
{
|
||||
"commandFragments" :
|
||||
[
|
||||
{
|
||||
"fragment" : "-g",
|
||||
"role" : "flags"
|
||||
},
|
||||
{
|
||||
"fragment" : "",
|
||||
"role" : "flags"
|
||||
}
|
||||
],
|
||||
"language" : "CXX"
|
||||
},
|
||||
"name" : "MSTL",
|
||||
"nameOnDisk" : "MSTL",
|
||||
"paths" :
|
||||
{
|
||||
"build" : ".",
|
||||
"source" : "."
|
||||
},
|
||||
"sourceGroups" :
|
||||
[
|
||||
{
|
||||
"name" : "Source Files",
|
||||
"sourceIndexes" :
|
||||
[
|
||||
0
|
||||
]
|
||||
},
|
||||
{
|
||||
"name" : "Header Files",
|
||||
"sourceIndexes" :
|
||||
[
|
||||
1,
|
||||
2
|
||||
]
|
||||
}
|
||||
],
|
||||
"sources" :
|
||||
[
|
||||
{
|
||||
"backtrace" : 1,
|
||||
"compileGroupIndex" : 0,
|
||||
"path" : "main.cpp",
|
||||
"sourceGroupIndex" : 0
|
||||
},
|
||||
{
|
||||
"backtrace" : 1,
|
||||
"path" : "h/String.h",
|
||||
"sourceGroupIndex" : 1
|
||||
},
|
||||
{
|
||||
"backtrace" : 1,
|
||||
"path" : "h/LinkList.h",
|
||||
"sourceGroupIndex" : 1
|
||||
}
|
||||
],
|
||||
"type" : "EXECUTABLE"
|
||||
}
|
Binary file not shown.
@ -1,23 +1,3 @@
|
||||
# ninja log v5
|
||||
0 176 1664114584453149321 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
||||
176 227 1664114584501149051 MSTL 5fce564c316cd3aa
|
||||
0 196 1664115258161179799 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
||||
196 250 1664115258213179483 MSTL 5fce564c316cd3aa
|
||||
1 206 1664115415616221673 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
||||
206 253 1664115415664221380 MSTL 5fce564c316cd3aa
|
||||
0 195 1664115434932103707 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
||||
195 247 1664115434984103389 MSTL 5fce564c316cd3aa
|
||||
0 186 1664115539947460993 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
||||
186 238 1664115539995460699 MSTL 5fce564c316cd3aa
|
||||
0 185 1664115559843338991 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
||||
185 234 1664115559891338697 MSTL 5fce564c316cd3aa
|
||||
0 187 1664115711810405059 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
||||
187 236 1664115711858404763 MSTL 5fce564c316cd3aa
|
||||
0 183 1664115718762362256 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
||||
183 233 1664115718810361961 MSTL 5fce564c316cd3aa
|
||||
0 180 1664115743494209937 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
||||
180 229 1664115743542209642 MSTL 5fce564c316cd3aa
|
||||
0 181 1664115749406173515 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
||||
181 230 1664115749454173219 MSTL 5fce564c316cd3aa
|
||||
1 192 1664116226535223733 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
||||
192 243 1664116226587223410 MSTL 5fce564c316cd3aa
|
||||
0 188 1664284334515694444 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
||||
188 243 1664284334567693897 MSTL 5fce564c316cd3aa
|
||||
|
Binary file not shown.
Binary file not shown.
@ -1,3 +1,3 @@
|
||||
Start testing: Sep 25 22:30 CST
|
||||
Start testing: Sep 27 21:14 CST
|
||||
----------------------------------------------------------
|
||||
End testing: Sep 25 22:30 CST
|
||||
End testing: Sep 27 21:14 CST
|
||||
|
21
h/Faction.h
Normal file
21
h/Faction.h
Normal file
@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by dongl on 22-9-27.
|
||||
//
|
||||
|
||||
#ifndef MSTL_FACTION_H
|
||||
#define MSTL_FACTION_H
|
||||
|
||||
#endif //MSTL_FACTION_H
|
||||
|
||||
// 转换函数 重载
|
||||
class Faction{
|
||||
public:
|
||||
Faction(int num, int den = 1):m_numerator(num),m_denominator(den){}
|
||||
|
||||
operator double() const{
|
||||
return (double) m_numerator / m_denominator;
|
||||
}
|
||||
private:
|
||||
int m_numerator; // 分子
|
||||
int m_denominator; // 分母
|
||||
};
|
@ -8,38 +8,40 @@
|
||||
#include <iostream>
|
||||
#endif //CPP_LINKLIST_H
|
||||
|
||||
|
||||
template<typename U>
|
||||
class LNode{
|
||||
|
||||
friend class Link;
|
||||
|
||||
public:
|
||||
int data = 0x0;
|
||||
private:
|
||||
LNode *next = nullptr;
|
||||
void update(int el){
|
||||
U data;
|
||||
|
||||
LNode<U> *next = nullptr;
|
||||
|
||||
LNode(): next(nullptr){
|
||||
|
||||
}
|
||||
void update(U el){
|
||||
data = el;
|
||||
}
|
||||
};
|
||||
|
||||
class Link{
|
||||
LNode *lNode = (LNode*) new LNode;
|
||||
template<typename T>
|
||||
class List{
|
||||
LNode<T> *lNode = new LNode<T>;
|
||||
unsigned int _length = 0;
|
||||
LNode* _begin = lNode;
|
||||
LNode<T>* _begin = lNode;
|
||||
public:
|
||||
Link(){
|
||||
List(){
|
||||
|
||||
}
|
||||
void add(int el){
|
||||
lNode = lNode->next = new LNode;
|
||||
void add(T el){
|
||||
lNode = lNode->next = new LNode<T>;
|
||||
lNode->data = el;
|
||||
_length++;
|
||||
lNode->next = new LNode;
|
||||
lNode->next = new LNode<T>;
|
||||
};
|
||||
|
||||
int get(int index){
|
||||
int get(T index){
|
||||
int el;
|
||||
LNode* node = _begin;
|
||||
LNode<T>* node = _begin;
|
||||
for (int i = 0; i < index; ++i) {
|
||||
node = node->next;
|
||||
el = node->data;
|
||||
@ -49,21 +51,22 @@ public:
|
||||
|
||||
void remove(unsigned int index){
|
||||
if(index > _length+1)
|
||||
std::cout << "超越范围之外" << std::endl;
|
||||
std::cerr << "超越范围之外:" << __FILE__ <<
|
||||
":" <<__LINE__ <<std::endl;
|
||||
|
||||
LNode* begin = _begin;
|
||||
LNode<T>* begin = _begin;
|
||||
for (int i = 1; i < index; ++i) {
|
||||
begin = _begin->next;
|
||||
}
|
||||
|
||||
LNode* node = begin->next;
|
||||
LNode<T>* node = begin->next;
|
||||
begin->next = begin->next->next;
|
||||
delete node;
|
||||
_length--;
|
||||
}
|
||||
|
||||
void update(int index, int el){
|
||||
LNode* node;
|
||||
void update(int index, T el){
|
||||
LNode<T>* node;
|
||||
for (int i = 0; i < index; ++i) {
|
||||
node = _begin->next;
|
||||
}
|
||||
@ -74,7 +77,7 @@ public:
|
||||
return _length;
|
||||
}
|
||||
|
||||
LNode*& begin(){
|
||||
LNode<T>*& begin(){
|
||||
return _begin->next;
|
||||
}
|
||||
};
|
26
h/Singleton.h
Normal file
26
h/Singleton.h
Normal file
@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by dongl on 22-9-26.
|
||||
//
|
||||
|
||||
#ifndef MSTL_SINGLETON_H
|
||||
#define MSTL_SINGLETON_H
|
||||
|
||||
#endif //MSTL_SINGLETON_H
|
||||
|
||||
|
||||
/// 设计模式 单链 单体 单键
|
||||
class A{
|
||||
public:
|
||||
static A& getInstance();
|
||||
void setup(){
|
||||
|
||||
}
|
||||
private:
|
||||
A();
|
||||
A(const A& rhs);
|
||||
};
|
||||
|
||||
A& A::getInstance() {
|
||||
static A a;
|
||||
return a;
|
||||
}
|
@ -61,7 +61,13 @@ String::String(const String& str) {
|
||||
}
|
||||
|
||||
inline
|
||||
String &String::operator=(const String &str) {
|
||||
String &String::operator=(const String& str) { // String& 引用
|
||||
// 拷贝赋值 是否是自我赋值
|
||||
// 来源端与目的端是否相同 指针对比地址 &str取地址
|
||||
if(this == &str){
|
||||
return *this;
|
||||
}
|
||||
|
||||
// 目的端 本来存在
|
||||
delete[] m_data;
|
||||
// 在重新申请一块空间 堆
|
||||
|
12
main.cpp
12
main.cpp
@ -1,13 +1,11 @@
|
||||
#include <iostream>
|
||||
#include "h/LinkList.h"
|
||||
#include "h/List.h"
|
||||
#include "h/String.h"
|
||||
#include "h/Faction.h"
|
||||
|
||||
int main() {
|
||||
String string("123456");
|
||||
// 赋值 重载函数 返回了 *this
|
||||
// 这使得以 目的值具有 对象的this
|
||||
// 更改的值 不在函数内 而是申请的堆中
|
||||
String str = string;
|
||||
std::cout << str.get_c_str() << std::endl;
|
||||
double f = *new Faction(7,5);
|
||||
|
||||
std::cout << f << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user