new file: LSList.h
new file: LSListItem.h new file: LSMacros.h modified: LSString.h modified: LSString.vcxproj new file: LSVector.h modified: main.cpp
This commit is contained in:
parent
6e22717a81
commit
4f9b04c049
7 changed files with 194 additions and 26 deletions
106
LSString/LSList.h
Normal file
106
LSString/LSList.h
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
#pragma once
|
||||||
|
#include "LSMacros.h"
|
||||||
|
#include "LSListItem.h"
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
|
||||||
|
class LSList
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LSList()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~LSList()
|
||||||
|
{
|
||||||
|
DELETE_ARRAY(m_pFirst)
|
||||||
|
DELETE_ARRAY(m_pLast)
|
||||||
|
}
|
||||||
|
|
||||||
|
LSListItem<T>* Append(T tObj)
|
||||||
|
{
|
||||||
|
LSListItem<T>* pNewLast;
|
||||||
|
if (!m_pFirst)
|
||||||
|
{
|
||||||
|
m_pFirst = new LSListItem<T>(tObj);
|
||||||
|
m_pLast = new LSListItem<T>(tObj);
|
||||||
|
m_pFirst->pNext = m_pLast;
|
||||||
|
pNewLast = m_pFirst;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pNewLast = new LSListItem<T>(tObj);
|
||||||
|
if (m_pLast->pBefore)
|
||||||
|
{
|
||||||
|
LSListItem<T>* pCurrentLast = m_pLast;
|
||||||
|
pCurrentLast->pNext = pNewLast;
|
||||||
|
pNewLast->pBefore = pCurrentLast;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_pFirst->pNext = pNewLast;
|
||||||
|
pNewLast->pBefore = m_pFirst;
|
||||||
|
}
|
||||||
|
m_pLast = pNewLast;
|
||||||
|
}
|
||||||
|
m_zSize++;
|
||||||
|
return pNewLast;
|
||||||
|
}
|
||||||
|
|
||||||
|
LSListItem<T>* Remove(T tObj)
|
||||||
|
{
|
||||||
|
if (m_zSize == 0) return nullptr;
|
||||||
|
|
||||||
|
LSListItem<T>* pItemToRemove = m_pFirst;
|
||||||
|
|
||||||
|
while (pItemToRemove->tValue != tObj)
|
||||||
|
{
|
||||||
|
pItemToRemove = pItemToRemove->pNext;
|
||||||
|
}
|
||||||
|
|
||||||
|
LSListItem<T>* pRet = nullptr;
|
||||||
|
|
||||||
|
if (m_zSize <= 1)
|
||||||
|
{
|
||||||
|
m_pFirst = nullptr;
|
||||||
|
m_pLast = nullptr;
|
||||||
|
}
|
||||||
|
else if (pItemToRemove == m_pFirst)
|
||||||
|
{
|
||||||
|
LSListItem<T>* pNewFirst = pItemToRemove->pNext;
|
||||||
|
pNewFirst->pBefore = nullptr;
|
||||||
|
m_pFirst = pNewFirst;
|
||||||
|
pRet = pNewFirst;
|
||||||
|
}
|
||||||
|
else if (pItemToRemove == m_pLast)
|
||||||
|
{
|
||||||
|
LSListItem<T>* pNewLast = pItemToRemove->pBefore;
|
||||||
|
pNewLast->pNext = nullptr;
|
||||||
|
m_pLast = pNewLast;
|
||||||
|
pRet = pNewLast;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LSListItem<T>* pBefore = pItemToRemove->pBefore;
|
||||||
|
LSListItem<T>* pNext = pItemToRemove->pNext;
|
||||||
|
pBefore->pNext = pNext;
|
||||||
|
pNext->pBefore = pBefore;
|
||||||
|
pRet = pNext;
|
||||||
|
}
|
||||||
|
|
||||||
|
DELETE_POINTER(pItemToRemove);
|
||||||
|
m_zSize--;
|
||||||
|
return pRet;
|
||||||
|
}
|
||||||
|
|
||||||
|
T* RemoveAt(size_t zPos)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
LSListItem<T>* m_pFirst = nullptr;
|
||||||
|
LSListItem<T>* m_pLast = nullptr;
|
||||||
|
|
||||||
|
size_t m_zSize = 0;
|
||||||
|
};
|
22
LSString/LSListItem.h
Normal file
22
LSString/LSListItem.h
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
|
||||||
|
class LSListItem
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LSListItem(T tObj)
|
||||||
|
: tValue(tObj)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
~LSListItem()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
LSListItem* pBefore = nullptr;
|
||||||
|
LSListItem* pNext = nullptr;
|
||||||
|
T tValue;
|
||||||
|
};
|
15
LSString/LSMacros.h
Normal file
15
LSString/LSMacros.h
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define DELETE_POINTER(Pointer) \
|
||||||
|
if (Pointer != nullptr)\
|
||||||
|
{\
|
||||||
|
delete Pointer;\
|
||||||
|
Pointer = nullptr;\
|
||||||
|
}
|
||||||
|
|
||||||
|
#define DELETE_ARRAY(Array) \
|
||||||
|
if (Array != nullptr)\
|
||||||
|
{\
|
||||||
|
delete[] Array;\
|
||||||
|
Array = nullptr;\
|
||||||
|
}
|
|
@ -1,23 +1,9 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include "LSMacros.h"
|
||||||
|
|
||||||
class LSString
|
class LSString
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
#define DELETE_POINTER(Pointer) \
|
|
||||||
if (Pointer != nullptr)\
|
|
||||||
{\
|
|
||||||
delete Pointer;\
|
|
||||||
Pointer = nullptr;\
|
|
||||||
}
|
|
||||||
|
|
||||||
#define DELETE_ARRAY(Array) \
|
|
||||||
if (Array != nullptr)\
|
|
||||||
{\
|
|
||||||
delete[] Array;\
|
|
||||||
Array = nullptr;\
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// - Constructor
|
// - Constructor
|
||||||
LSString() = default;
|
LSString() = default;
|
||||||
|
|
|
@ -157,7 +157,11 @@
|
||||||
<ClCompile Include="Timer.cpp" />
|
<ClCompile Include="Timer.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="LSList.h" />
|
||||||
|
<ClInclude Include="LSListItem.h" />
|
||||||
|
<ClInclude Include="LSMacros.h" />
|
||||||
<ClInclude Include="LSString.h" />
|
<ClInclude Include="LSString.h" />
|
||||||
|
<ClInclude Include="LSVector.h" />
|
||||||
<ClInclude Include="Timer.h" />
|
<ClInclude Include="Timer.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
|
8
LSString/LSVector.h
Normal file
8
LSString/LSVector.h
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
|
||||||
|
class LSVector
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "LSString.h"
|
#include "LSString.h"
|
||||||
|
#include "LSList.h"
|
||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
|
|
||||||
#define RUNS 50U
|
#define RUNS 50U
|
||||||
|
@ -7,6 +8,8 @@
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
// - STRING
|
||||||
|
{
|
||||||
printf("LSString:\n");
|
printf("LSString:\n");
|
||||||
printf("Number of runs: %d:\n", (int)RUNS);
|
printf("Number of runs: %d:\n", (int)RUNS);
|
||||||
Timer ttt;
|
Timer ttt;
|
||||||
|
@ -22,5 +25,29 @@ int main()
|
||||||
s.Append(PAYLOAD);
|
s.Append(PAYLOAD);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// - LIST
|
||||||
|
{
|
||||||
|
int i1 = 1187;
|
||||||
|
int i2 = 2361;
|
||||||
|
int i3 = 3257;
|
||||||
|
int i4 = 41337;
|
||||||
|
int i5 = 569;
|
||||||
|
|
||||||
|
LSList<int> liList;
|
||||||
|
auto a1 = liList.Append(i1);
|
||||||
|
auto a2 = liList.Append(i2);
|
||||||
|
auto a3 = liList.Append(i3);
|
||||||
|
auto a4 = liList.Append(i4);
|
||||||
|
auto a5 = liList.Append(i5);
|
||||||
|
|
||||||
|
liList.Remove(i1);
|
||||||
|
liList.Remove(i5);
|
||||||
|
liList.Remove(i3);
|
||||||
|
liList.Remove(i4);
|
||||||
|
liList.Remove(i2);
|
||||||
|
liList.Remove(i1);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue