#include <iostream>
using namespace std;
template<typename T>
class List
{
public:
struct Node
{
T data;
Node *next;
};
int theSize;
List()
{
head = new Node;
int theSize = 0;
}
~List()
{
if(theSize == 0)
{
delete head;
head = NULL;
cout<<endl;
cout<<"SIZE=0析构"<<endl;
}
else
{
while(head->next != NULL)
{
Node *ptemp = head;
head = head->next;
delete ptemp;
}
delete head;
head = NULL;
cout<<endl;
cout<<"清除链表析构"<<endl;
}
cout<<endl;
cout<<"*************析构函数***************";
}
void CreateList(int n) //创建链表
{
int i = n;
theSize = n;
if(n<0)
cout<<"输入错误结点个数:"<<endl;
Node *p = head;
while(n-->0)
{
cout<<"输入第"<<i-n<<"个结点的值:"<<endl;
Node *ptemp = new Node;
cin>>ptemp->data;
p->data = ptemp->data;
p->next = ptemp;
p = ptemp;
}
}
void erasePos(int pos) //删除链表某个位置的值
{
if(this->theSize == 0)
cout<<"链表为空!"<<endl;
if(pos > theSize)
cout<<"输入位置超出链表长度!"<<endl;
else
{
Node *p = head;
for(int i = 1; i < pos-1; i++)
p = p->next;
p->next = p->next->next;
theSize--;
}
}
void eraseAll() //删除链表
{
if(theSize > 0)
{
Node *cur = head;
while(theSize>0)
{
Node *p = new Node;
p = cur->next;
cur = p;
delete p;
theSize--;
}
delete cur;
cout<<"链表清除成功!";
cout<<"*******"<<"链表一共有"<<theSize<<"个结点!"<<endl;
}
else
cout<<"链表本身为空!"<<endl;
}
void push_back() //在链表尾部插入值
{
Node *p = head;
if(theSize >= 0)
{
T dd;
cout<<"插入的值为:";
cin>>dd;
int i = theSize;
for(; i>1; --i)
p = p->next;
Node *ptemp = new Node;
ptemp->data = dd;
ptemp->next= NULL;
p->next = ptemp;
theSize++;
cout<<"成功在尾部添加:"<<dd<<endl;
}
else
cout<<"结点数有误!"<<endl;
this->Show();
}
void findNode(int k) //查找第k个结点
{
if(theSize ==0)
{
cout<<"链表为空!"<<endl;
}
if(k > theSize || k <= 0)
{
cout<<"输入的结点数有误!"<<endl;
}
else
{
Node *p = head;
for(int i = 1; i<k; i++)
p = p->next;
cout<<"第"<<k<<"个结点为:"<<p->data<<endl;
}
}
void insertPos(int pos) //指定位置插入结点
{
T d;
if(pos<0 || pos>theSize+1)
cout<<"输入的结点数有误!"<<endl;
if(pos == (theSize+1))
{
this->push_back();
}
else
{
Node *p = head;
for(int i = 1; i<pos-1; i++)
p = p->next;
cout<<"在第"<<pos<<"个结点插入:";
cin>>d;
Node *ptemp = new Node;
ptemp->next = p->next;
ptemp->data = d;
p->next = ptemp;
p = ptemp;
theSize++;
cout<<"插入结点后的链表为:";
this->Show();
}
}
void isExist() //查找某个值是否存在
{
if(theSize == 0)
cout<<"链表为空!"<<endl;
else
{
cout<<"你所要查找的元素为:";
T d;
cin>>d;
Node *p = head;
int Num = 0;
for(int i = 1; i <= theSize; i++)
{
Num += p->data == d?1:0;
p = p->next;
}
if(Num>0)
cout<<"值为"<<d<<"的结点"<<"有"<<Num<<"个!"<<endl;
else
cout<<"不存在值为"<<d<<"的结点!"<<endl;
}
}
void inverseList() //逆序链表
{
if(theSize == 0)
cout<<"链表为空!"<<endl;
else
{
int n = theSize;
for(; n>=1; n--)
{
Node *p = head;
for(int i=1; i<n; i++)
p = p->next;
cout<<p->data<<" ; ";
}
cout<<"*******"<<"链表一共有"<<theSize<<"个结点!"<<endl;
}
}
void Show() //显示链表信息
{
Node *p = head;
for(int i = 1; i <= theSize; i++)
{
cout<<p->data<<" ; ";
p = p->next;
}
cout<<"*******"<<"链表一共有"<<theSize<<"个结点!"<<endl;
}
private:
Node *head;
};
#include "List.h"
int main()
{
List<double> ln1;
int n;
cout<<"一:确定建立链表的结点数:";
cin>>n;
ln1.CreateList(n);
ln1.Show();
cout<<endl;
cout<<"二:在链表结尾插入:";
ln1.push_back();
cout<<endl;
cout<<"三:所要查询的结点位置:";
int k;
cin>>k;
ln1.findNode(k);
cout<<endl;
cout<<"四:插入结点的位置:";
int t;
cin>>t;
ln1.insertPos(t);
cout<<endl;
cout<<"五:判断你所要查找的元素是否存在。"<<endl;
ln1.isExist();
cout<<endl;
cout<<"六:要删除的结点:";
int h;
cin>>h;
ln1.erasePos(h);
ln1.Show();
cout<<endl;
cout<<"七:逆序链表:";
ln1.inverseList();
cout<<endl;
cout<<"八:删除链表"<<endl;
ln1.eraseAll();
return 0;
}
