
#include
using namespace std;
template
struct Node
{
T Value;
Node
};
template
class LinkedList
{
public:
Node
//创建链表
LinkedList(unsigned int Count)
{
Node
if (pHeader == nullptr)
{
List = nullptr;
return;
}
pHeader->Value = 445145145;
pHeader->NextNode = nullptr;
Node
for (unsigned int i = 0; i < Count - 1; i++)
{
Node
if (pNode != nullptr)
{
pNode->Value = 0;
pNode->NextNode = nullptr;
pTail->NextNode = pNode;
pTail = pNode;
}
}
List = pHeader;
return;
}
//获取长度
unsigned int GetLength()
{
Node
int Count = 0;
do
{
Count++;
p = p->NextNode;
}while (p != nullptr);
return Count;
}
//获取索引对应节点指针
Node
{
Node
for (unsigned int i = 0; i < Index; i++)
{
p = p->NextNode;
}
return p;
}
//获取索引对应节点值
T GetNodeValue(unsigned int Index)
{
if (Index >= GetLength()) return 0;
Node
for (unsigned int i = 0; i < Index; i++)
{
p = p->NextNode;
}
return p->Value;
}
//遍历链表
void Traverse()
{
Node
do
{
cout << p->Value << endl;
p = p->NextNode;
} while (p != nullptr);
}
//插入值
void Insert(unsigned int Index, T Value)
{
if (Index >= GetLength()) return;
Node
if (AddedNode != nullptr)
{
AddedNode->Value = Value;
Node
Node
p->NextNode = AddedNode;
AddedNode->NextNode = q;
}
}
//向链表最后插入值
void Push_Back(T Value)
{
Node
if (AddedNode != nullptr)
{
AddedNode->Value = Value;
AddedNode->NextNode = nullptr;
Node
p->NextNode = AddedNode;
}
}
//删除链表最后的节点
void Pop_Back()
{
Node
free(p->NextNode);
p->NextNode = nullptr;
}
//删除链表特定索引对应节点
void Delete(unsigned int Index)
{
if (Index == GetLength() - 1)
{
cout << "e" << endl;
Node
cout << p->NextNode << endl;
p->NextNode = nullptr;
free(p->NextNode);
}
else
{
Node
Node
Node
p->NextNode = q;
free(Del);
}
}
//删除链表
void Dispose()
{
Node
Node
while (p->NextNode != nullptr)
{
p = p->NextNode;
free(q);
q = p->NextNode;
}
}
};
int main()
{
//创建链表
LinkedList
//初始化链表
for (int i = 0; i < 3; i++)
{
Node
p->Value = i * 123;
}
List.Traverse();
cout << "---" << endl;
List.Push_Back(114514);
List.Push_Back(114513);
List.Insert(4, 40);
List.Traverse();
cout << "---" << endl;
List.Pop_Back();
List.Traverse();
cout << "---" << endl;
cout << List.GetNodeValue(4) << endl;
cout << "---" << endl;
List.Dispose();
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)