代码如有问题和优化的地方,请指出,谢谢!
1.头文件
#ifndef CHAIN_H
#define CHAIN_H
#include<iostream>
template<class T>
class chainnode
{
template<class T>
friend class chain;
public:
chainnode()
{
data = 0;
next = nullptr;
}
~chainnode()
{
}
public:
T getdata()
{
return data;
}
chainnode<T>* getnext()
{
return next;
}
private:
T data;
chainnode<T>* next;
};
template<class T>
class chain
{
public:
chain()
{
first = nullptr;
length = 0;
}
~chain()
{
chainnode<T>* temp;
while (first)
{
temp = first->next;
delete first;
first = temp;
}
}
public:
bool isempty()
{
return top == nullptr;
}
chainnode<T>* getfirst()
{
return first;
}
void deletenode(int pos);
int getlength();
void insertbefore(T dat, int pos);
void insertafter(T dat, int pos);
T find(int pos);
private:
chainnode<T>* first;
int length;
};
template<class T>
void chain<T>::insertbefore(T dat, int pos)
{
if (pos< 0 || pos>length)
{
std::cout << "输入的位置数错误" << endl;
exit(1);
}
chainnode<T>* now = new chainnode < T >;
now->data = dat;
if (first == nullptr)
{
first = now;
first->next = nullptr;
++length;
}
else
{
chainnode<T>* pp = first;
for (int i = 1; i < pos - 1 && pp; ++i)
pp = pp->next;
if (pp)
{
if (pos == 1)
{
now->next = first;
first = now;
}
else
{
now->next = pp->next;
pp->next = now;
}
++length;
}
}
}
template<class T>
void chain<T>::insertafter(T dat, int pos)
{
if (pos < 0 || pos>length)
{
std::cout << "输入的位置数错误" << endl;
exit(1);
}
chainnode<T>* now = new chainnode < T >;
now->data = dat;
if (first == nullptr)
{
first = now;
first->next = nullptr;
++length;
}
else
{
chainnode<T>* pp = first;
for (int i = 1; i < pos && pp; ++i)
pp = pp->next;
if (pp)
{
if (pos == length)
{
now->next = nullptr;
pp->next = now;
}
else
{
now->next = pp->next;
pp->next = now;
}
++length;
}
}
}
template<class T>
T chain<T>::find(int pos)
{
if (first)
{
if (pos<0 || pos>length)
{
std::cout << "输入的位置数错误" << endl;
exit(1);
}
chainnode<T>* curr=first;
for (int i = 1; i < pos; ++i)
curr = curr->next;
return curr->data;
}
}
template<class T>
int chain<T>::getlength()
{
chainnode<T>* current = first;
int len = 0;
while (current)
{
++len;
current = current->next;
}
length = len;
return len;
}
template<class T>
void chain<T>::deletenode(int pos)
{
if (first)
{
if (pos<0 || pos>length)
{
std::cout << "输入的位置数错误" << endl;
exit(1);
}
if (pos == 1)
{
chainnode<T>* temp;
temp = first;
first = temp->next;
delete temp;
--length;
}
else
{
chainnode<T>* tem = first;
for (int i = 1; i < pos - 1 && tem; ++i) //获得pos前一个元素的地址
tem = tem->next;
if (tem &&tem->next) //确保第pos个元素存在
{
chainnode<T>* curr = tem->next; //curr是pos位置元素的地址
tem->next = curr->next;
delete curr;
--length;
}
}
}
}
template<class T>
std::ostream& operator <<(std::ostream& out, chain<T>& list)
{
chainnode<T>* cc = list.getfirst();
for (int i = 1; i <= list.getlength(); ++i)
{
out <<cc->getdata() << " ";
cc = cc->getnext();
}
std::cout << std::endl;
return out;
}
#endif
2.主文件:
#include<iostream>
#include"chain.h"
using namespace std;
int main()
{
chain<int> liner;
liner.insertafter(10, 0);
liner.insertafter(9, 1);
liner.insertafter(8, 2);
liner.insertafter(7, 3);
liner.insertbefore(2,1);
auto dd = liner.find(3);
cout << dd << endl;
cout<< liner << endl;
system("pause");
return 0;
}
PS:我只知道用插入,产生一个线性链表,不知道大家还有什么其他方法?