单链表
#pragma once
#include<iostream>
using namespace std;
const int ERROR = 0;
const int OK = 1;
template<class Elemtype>
class LNode
{
public:
Elemtype data;
LNode* next;
};
template<class Elemtype>
class Mylist
{
public:
Mylist();
~Mylist();
void CreatList(int i);//创建链表
void Show_List();//显示链表元素
void GetElem(int i, Elemtype& record);//根据序号i的获取元素的值,并以引用的方式返回该结点的元素
LNode<Elemtype > * LocateElem(Elemtype & record);//查找链表中一个数据域为data的元素,并且将他的结点指针返回
void InsertElemAtIndex(int n, Elemtype& record); //在指定位置插入指定元素
void Delete_index(int i);//删除链表中第i个结点
void Delete_all();//删除所有数据,但是保留链表
private:
LNode<Elemtype> * head;//单链表头节点
};
template<class Elemtype>
Mylist<Elemtype>::Mylist()
{
this->head = new LNode<Elemtype>();
head->next = NULL;
cout << "定义头节点!" << endl;
}
template<class Elemtype>
void Mylist<Elemtype>::CreatList(int i)//前插法
{
for (int j = 0; j < i; j++)
{
LNode<Elemtype>* p;
p = new LNode<Elemtype>;
cout << "输入数据:";
cin >> p->data;
p->next = this->head->next;
head->next = p;
}
}
template<class Elemtype>
void Mylist<Elemtype>::Show_List()
{
LNode<Elemtype>* p;
p = head->next;
cout << "--------------显示数据!---------------" << endl;
while (p)
{
cout << p->data << "\t";
p = p->next;
}
}
template<class Elemtype>
void Mylist<Elemtype>::GetElem(int i, Elemtype& record)
{
LNode<Elemtype> *p;
p = this->head->next;
int j = 1;
while (p&&j != i)//p不为空,或者j不等于i的时候,p继续往下执行
{
p = p->next;
j++;
}
//循环结束后,不是啥都要你的data,一定要验证p此时不为空
if(p)
record = p->data;
else
{
cout << "未找到!" << endl;
exit(ERROR);
}
}
template<class Elemtype>
LNode<Elemtype>* Mylist<Elemtype>:: LocateElem(Elemtype& record)
{
LNode<Elemtype>* p;
p = head->next;
while (p->data!=record&&p)
{
p = p->next;
}
return p;//不用担心p的问题,因为如果跳出循环,是查找成功,则一切顺利返回p,如果p为空,直接返回NULL,也没事
}
template<class Elemtype>
void Mylist<Elemtype>::InsertElemAtIndex(int i, Elemtype& record)
{
if (i == 1)
{
LNode<Elemtype>* p;
p = new LNode<Elemtype>;
p->data = record;
p->next = this->head->next;
head->next = p;
}
else
{
Elemtype temp;
this->GetElem(i - 1, temp);
LNode<Elemtype>* p;
p = this->LocateElem(temp);
if (!p)
exit(ERROR);
LNode<Elemtype>* q;
q = new LNode<Elemtype>;
q->data = record;
q->next = p->next;
p->next = q;
}
}
template<class Elemtype>
void Mylist<Elemtype>::Delete_index(int i)
{
Elemtype temp;
this->GetElem(i-1, temp);
LNode<Elemtype>* p;
p = this->LocateElem(temp);
if (!p)
exit(ERROR);
LNode<Elemtype>* q;
q = p->next;
p->next = q->next;
delete q;
}
template<class Elemtype>
void Mylist<Elemtype>::Delete_all()
{
LNode<Elemtype>* p;
LNode<Elemtype>* q;
p = head->next;
q = p;
while (p)
{
q = p->next;
delete p;
p = q;
}
this->head->next = NULL;
}
template<class Elemtype>
Mylist<Elemtype>::~Mylist()
{
if(!this->head->next)
this->Delete_all();
delete this->head;
this->head = NULL;
cout << "析构函数" << endl;
}
主程序
#include<iostream>
#include<string>
#include"线性表的链式存储.hpp"
using namespace std;
void test()
{
Mylist<int> L;
LNode<int>* node;
int c = 5;
L.CreatList(c);
int temp1 = 99;
L.InsertElemAtIndex(1, temp1);
int result;
L.GetElem(1, result); cout << "第1个元素是:" << result << endl;
L.Delete_index(2);
node = L.LocateElem(result);
cout << "第二个元素的值是:" << node->next->data << endl;
}
int main()
{
test();
system("pause");
return 0;
}
可以复制下来,自行验证!

本文介绍了如何使用C++模板类实现单链表,包括创建链表、显示元素、元素定位、插入与删除操作。通过实例展示了前插法创建链表、根据序号获取和修改元素,以及删除特定位置和全部节点的功能。
361

被折叠的 条评论
为什么被折叠?



