#include <iostream>
using
namespace std;
template <
class E,
class K>
class SortedChain;
template <
class E,
class K>
class SortedChainNode {
friend
class SortedChain<E,K>;
private:
E data;
K key;
SortedChainNode<E,K> *link;
};
template<
class E,
class K>
class SortedChain{
public:
SortedChain(){
first=
0;
}
~SortedChain();
bool IsEmpty()
const{
return first==
0;
}
int Length()
const;
bool Search(
const K& k,E& e)
const;
SortedChain<E,K>& Delete(
const K& k,E& e);
SortedChain<E,K>& Insert(
const K& k,
const E& e);
SortedChain<E,K>& DistinctInsert(
const K&k,
const E& e);
void Output(ostream&
out)
const;
private:
SortedChainNode<E,K> *first;
};
template<
class E,
class K>
SortedChain<E,K>::~SortedChain()
{
SortedChainNode<E,K>* next;
while(first){
next=first->link;
delete first;
first=next;
}
}
template<
class E,
class K>
int SortedChain<E,K>::Length()
const
{
SortedChainNode<E,K>* p=first;
int len=
0;
while(p){
p=p->link;
len++;
}
return len;
}
template<
class E,
class K>
bool SortedChain<E,K>::Search(
const K& k,E& e)
const
{
SortedChainNode<E,K> *p=first;
while(p&&p->key<k)
p=p->link;
if(p&&p->key==k) {
e=p->data;
return
true;
}
return
false;
}
template<
class E,
class K>
SortedChain<E,K>& SortedChain<E,K>::Delete(
const K& k,E& e)
{
SortedChainNode<E,K>*p=first,*tp=
0;
while(p&&p->key<k){
tp=p;
p=p->link;
}
if(p&&p->key==k){
e=p->data;
if(tp)
tp->link=p->link;
else
first=p->link;
delete p;
return *
this;
}
throw
"
Bad Input
";
return *
this;
}
template<
class E,
class K>
SortedChain<E,K>& SortedChain<E,K>::Insert(
const K& k,
const E& e)
{
SortedChainNode<E,K>*p=first,*tp=
0;
while(p&&p->key<k){
tp=p;
p=p->link;
}
SortedChainNode<E,K> *q=
new SortedChainNode<E,K>;
q->key=k;
q->data=e;
q->link=p;
if(tp)
tp->link=q;
else
first=q;
return *
this;
}
template<
class E,
class K>
SortedChain<E,K>& SortedChain<E,K>::DistinctInsert(
const K& k,
const E& e)
{
SortedChainNode<E,K>*p=first,*tp=
0;
while(p&&p->key<k){
tp=p;
p=p->link;
}
if(p&&p->data==e)
throw
"
Bad Input
";
SortedChainNode<E,K> *q=
new SortedChainNode<E,K>;
q->key=k;
q->data=e;
q->link=p;
if(tp)
tp->link=q;
else
first=q;
return *
this;
}
template<
class E,
class K>
void SortedChain<E,K>::Output(ostream&
out)
const
{
SortedChainNode<E,K>*p;
p=first;
while(p){
out<<
"
(
"<<p->key<<
"
,
"<<p->data<<
"
)
";
p=p->link;
}
}
template<
class E,
class K>
ostream&
operator<<(ostream&
out,
const SortedChain<E,K>& x){
x.Output(
out);
return
out;
}
int main(
void)
{
SortedChain<
int,
int> Z;
int x;
Z.Insert(
2,
2).Insert(
6,
6).Insert(
4,
4);
cout <<
"
The chain is
" << Z << endl;
cout <<
"
Its length is
" << Z.Length() << endl;
Z.Delete(
2,x);
cout <<
"
Deleted
" << x << endl;
cout << Z << endl;
Z.Insert(
1,
1).Delete(
6,x);
cout << Z << endl;
Z.Insert(
8,
8).Insert(
9,
9).Search(
8,x);
cout <<
"
Found
" << x << endl;
Z.Insert(
7,
7).Delete(
9,x);
cout << Z << endl;
try {Z.DistinctInsert(
7,
7);}
catch (...)
{cout <<
"
Attempt to insert duplicate element
" << endl;}
cout << Z << endl;
Z.DistinctInsert(
10,
10);
cout << Z << endl;
}