#include
using namespace std;
template
struct Linknode
{
T data;
Linknode *next;
Linknode():next(NULL){}
Linknode(T d):data(d),next(NULL){}
};
template
class LinkList
{
public:
Linknode *head;
LinkList()
{
head=new Linknode();
}
~LinkList()
{
Linknode *p,*pre;
pre=head;
p=pre->next;
while(p!=NULL)
{
delete pre;
pre=p;
p=p->next;
}
delete pre;
}
void CreateLinkList(T a[],int n)
{
Linknode<T> *s,*r;
r=head;
for(int i=0;i<n;i++)
{
s=new Linknode<T>(a[i]);
r->next=s;
r=s;
}
}
void Delete(int x)
{
Linknode<T> *p,*pre;//要删除的节点 要删除节点的前驱
pre=head;
p=pre->next;
while(p!=NULL)
{
if(p->data==x)
{
cout<<"头节点为前驱节点无法删除"<<endl;
break;
}
else if(p->next->data==x)
{
pre->next=p->next;
delete p;
break;
}
else
{
pre=p;
p=p->next;
}
}
}
bool Judge()
{
Linknode<T> *h;
bool temp;
temp=true;
h=head->next;
while(h->next!=NULL)
{
if(h->data>h->next->data)
{
temp=false;
}
h=h->next;
}
if(temp)
{
cout<<"该列表是递增链表"<<endl;
return true;
}
else
{
cout<<"该链表不是递增链表"<<endl;
return false;
}
cout<<endl;
}
void Display()
{
Linknode<T> *p;
p=head->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
};
int main()
{
LinkList L;
int j,a,m[50];
cout<<"建立链表:";
for(a=0;a<50;a++)
{
cin>>m[a];
if(cin.get()=='\n')break;
}
L.CreateLinkList(m,a+1);
L.Judge();
cout<<"请输入要删除的结点后继元素的数据:";
cin>>j;
L.Delete(j);
L.Display();
return 0;
}