// list.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include"iostream.h"
template<class T> class list;
template<class T>
class node
{
friend list<T>;
private:
T data;
node<T>*next;
};
template<class T>
class list
{
firend iterator <T>;
public:
list(){first=0;}
~list();
bool empty()const{return fistt==0;}
int size() const;
bool retrieve(int k,T& x) const;
int locate (const T& x)const;
list<T>& insert(int k,const T& x);
list<t>& erase(int k,T& x);
void print_list (ostream& out) const;
private:
node<T>*first;
};
template<class T>
list <T> ::~list()
{
node<T>*next;
while (first)
{
next=first->next;
delete first;
first=next;
}
}
template<class T>
int list<T>::size()const
{
node<T>*current=first;
int len=0;
while(current)
{
len++;
current=current->next;
}
return len;
}
template<class T>
bool list<T>::retrieve (int k,T& x)
{
if(k<1)return false;
node<T>*current=first;
int index=1;
while(index<k && current)
{
current=current->next;
index++;
}
if(current)
{
x=current->data;
return ture;
}
return false;
}
template<class T>
int list<T>::locate (const T& x)const
{
node<T>*current=first;
int index=1;
while(current && current->data!=x)
{
current=current->next;
index;
}
if(current)return index;
return 0;
}
template<class T>
list<T>& list<T>::insert (int k,const T& x)
{
if(k<0 )throw out_bounds();
node<T>*p=first;
for(int index=1;index<k &&p;index++) p=p->next;
if(k<0 && !P) throw out_bounds();
node<T>*y=new node <T>;
y->data=x;
if(k)
{
y->next=p->next;
p->next=y;
}
else
{
y->next=first;
first=y;
}
return this;
}
template<class T>
list<T>&list<T>::erase (int k,T& x)
{
if(k<1||!first )throw out_bounds();
node <T>*p=first;
if (k==1)
first=first->next;
else
{
node<T>*q=first;
for (int index=1;index<k-1&&q;index++)q=q->next;
if (!q||!q->next)throw out-bounds();
p=q->next;
q->next=p->next;
}
x=p->data;
delete p;
return *this;
}
template<class T>
void list<T>::print_list (ostream &out) const
{
node<T>*current;
for (current=first;current;current=current->next)
out<<current->data<<" ";
}
template<class T>
ostream&operator<<(ostream &out,const list<T> &x)
{x.print_list(out);return out;}
int main(int argc, char* argv[])
{
return 0;
}