求c++模板实现电话簿管理程序
#ifndef LINKEDLIST
#define LINKEDLIST
#include <fstream.h>
#include <assert.h>
template <class T>
class LList
{
protected:
struct Node
{
T Data;
Node * Next;
Node()
{
Next=0;
}
Node(T element, Node *ptr=0)
{
Data = element;
Next=ptr;
}
} *First,*Last;
int NumberNodes;
public:
LList(){ First=0; Last=0; NumberNodes=0;}
~LList();
LList(const LList& L);
int Length();
T& operator[](int i);
void Print();
const T& operator[](int i) const;
void Insert(const T& el);
void Append(const LList<T>& L);
void Remove(int location);
int Search(const T& value)const;
friend ostream& operator<<(int s,const LList<T>& L) ;
};
template <class T>
LList<T>::~LList()
{
Node * p=First;
while( p !=0)
{
First=p->Next;
delete p;
p=First;
}
NumberNodes=0;
Last=0;
}
template <class T>
int LList<T>::Length() { return NumberNodes;}
template <class T>
LList<T>::LList(const LList& L)
{
Node *Old=L.First, *New=0;
NumberNodes=L.NumberNodes;
if(NumberNodes==0) First=0;
else
{
New= new Node(Old->Data);
assert(New !=0);
Old=Old->Next;
First=New;
}
while(Old !=0)
{
New->Next= new Node(Old->Data);
assert(New->Next !=0);
New= New->Next;
Old= Old->Next;
}
Last=New;
}
template <class T>
ostream& operator<<(ostream & s,const LList<T>& L)
{
int i,M=L.Length();
for(i=0;i<M-1;i++)
{
s << L[i] << ",";
}
s << L[M-1] ;
return s;
}
template <class T>
void LList<T>::Print()
{
Node *p=First;
while (p!=Last)
{
cout << p->Data <<",";
p=p->Next;
}
cout << p->Data << endl;
}
template <class T>
T& LList<T>::operator[](int i)
{
assert(i >=0);
assert(i <NumberNodes);
Node *p=First;
for(int k=0;k<i;k++)
{
p = p->Next;
}
return p->Data;
}
template <class T>
const T& LList<T>::operator[](int i) const
{
assert(i >=0);
assert(i <NumberNodes);
Node *p=First;
for(int k=0;k<i;k++)
{
p = p->Next;
}
return p->Data;
}
template <class T>
void LList<T>::Insert(const T& el)
{
Node *newnode= new Node(el);
assert(newnode !=0);
newnode->Next=First;
First=newnode;
if(NumberNodes==0) Last=newnode;
NumberNodes++;
}
template <class T>
void LList<T>::Remove(int location)
{
assert(location >=0);
assert(location < NumberNodes);
Node * p;
if(location==0)
{
p=First;
First=p->Next;
p->Next=0;
}
else
{
Node *pp=First;
for(int i=0;i<location-1;i++) pp=pp->Next;
p=pp->Next;
pp->Next=p->Next;
p->Next=0;
if(location==NumberNodes-1) Last=pp;
}
delete p;
NumberNodes--;
}
template<class T>
int LList<T>::Search(const T& value)const
{
Node *p=First;
for(int i=0;i<NumberNodes;i++)
{
if(p->Data==value) return i;
else p=p->Next;
}
return -1;
}
template <class T>
void LList<T>::Append(const LList<T>& L)
{
Node *p=L.First;
while(p !=0)
{
Insert(p->Data);
p=p->Next;
}
}
#endif
main()
{
LList<int> L,M;
L.Insert(2);
L.Insert(3);
for(int i=0;i<10;i++) L.Insert(i);
L.Print();
cout << endl;
L.Remove(0);
L.Print();
cout << endl;
L.Remove(1);
L.Print();
}
这是给的关键程序代码,修改main 函数实现电话簿从文件中输入输出,分别根据姓名及电话号码查找数据,根据姓名及电话号码删除数据,输出电话簿数据内容