test ok with vc 6.0 and Dev-c 4.99
//filename is test.cpp
#include <iostream>
#include "SingleLink.h"
using namespace std;
int main(int argc, char *argv[])
{
cout<<"SingleList template class demo..."<<endl;
//1.constrator SingleLink() test ok
SingleLink<int> demo,demo1,int1,int2; //SingleLink() test
//2.copy constrator,=,destructor test ok
int1.InsertHead(10);
int1.InsertHead(20);
int1.InsertHead(30);
int2=int1; //= test
SingleLink<int> int3(int2); //copy constrator test
cout<<int3; //<<test
//3.IsFull(),Length() test ok
cout<<"int1.IsFull()="<<int1.IsFull()<<endl; //IsFull()
cout<<"int1.Length()="<<int1.Length()<<endl; //Length()
//4.FindItem(item),GetFirst(),GetLast() test ok
cout<<"int12.FindItem(20)="<<int2.FindItem(20)<<endl;//FindItem(item)
cout<<"int2.GetFirst()="<<int2.GetFirst()<<endl; //GetFirst()
cout<<"int2.GetLast()="<<int2.GetLast()<<endl; //GetLast()
//5.InsertHead(item),InsertEnd(item),copylist(SingleLink&)
demo.InsertEnd(11); //InsertHead()
demo.InsertHead(88); //InsertEnd()
demo.InsertHead(99);
cout<<demo;
demo1.copylist(demo); //copylist()
cout<<demo1;
//6.RemoveNode(item),RemoveAll()
demo.RemoveNode(88); //RemoveNode(item)
cout<<demo;
demo1.RemoveAll(); //RemoveAll()
cout<<demo1;
//7.<<
cout<<demo; //<< test
cout<<demo1;
cout<<int1;
cout<<int2;
cout<<int3;
system("Pause");
return 0;
}
//--------------------------------------------------------------------------------------------
/*
*file name is SingleLink.h
*this is a SingleLink template by pointer
*SingleLink ADT
*complied ok with vc 6.0 and Dev-c 4.99
*writer:chinanetboy ,QQ:44633197
*blog http://chinanetboy.bokee.com
*date:07/01/2008
*/
#ifndef H_SingleLink
#define H_SingleLink
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>
using namespace std;
//SingleLink ADT list
template <class T>
struct NodeType{
T info;
NodeType<T> *link;
};
template <class T>
class SingleLink
{
public:
//1.constrator SingleLink()
SingleLink();
//2.copy constrator,=,destructor
SingleLink(const SingleLink <T> & O);
const SingleLink <T>& operator=(const SingleLink<T>& O);
~SingleLink();
//3.IsFull(),Length(),
bool IsFull();
int Length();
//4.FindItem(item),GetFirst(),GetLast()
bool FindItem(const T & item);
const T GetFirst()const;
const T GetLast()const;
//5.InsertHead(item),InsertEnd(item),copylist(SingleLink&)
void InsertHead(const T& item);
void InsertEnd(const T& item);
void copylist(const SingleLink<T>& O);
//6.RemoveNode(item),RemoveAll()
void RemoveNode(const T& item);
void RemoveAll();
//7.<<
friend ostream& operator<<(ostream& o,const SingleLink <T> & link){
NodeType<T> *current;
current=link.First;
while (current!=NULL){
o<<current->info<<" ";
current=current->link;
}
o<<endl;
return o;
}
private:
//protected:
int count;
NodeType<T> *First;
NodeType<T> *Last;
};
//0.copylist()
template <class T>
void SingleLink<T>::copylist(const SingleLink<T>& O){
NodeType<T> *newnode;
NodeType<T> *current;
if(First!=NULL)
RemoveAll();
if (O.First==NULL){
First=NULL;
Last=NULL;
count=0;
}
else
{
current= O.First;
count= O.count;
First=new NodeType<T>;
assert(First!=NULL);
First->info=current->info;
First->link=NULL;
Last=First;
current=current->link;
while(current!=NULL){
newnode= new NodeType<T>;
assert(newnode!=NULL);
newnode->info=current->info;
newnode->link=NULL;
Last->link=newnode;
Last=newnode;
current=current->link;
}//endwhile
}//end else
}//end copylist
//1.constrator SingleLink()
template <class T>
SingleLink<T>::SingleLink(){
count=0;
First=NULL;
Last=NULL;
}
//2.copy constrator,=,destructor
template <class T>
SingleLink<T>::SingleLink(const SingleLink <T> & O){
First=NULL;
copylist(O);
}
template <class T>
const SingleLink<T>& SingleLink<T>::operator=(const SingleLink<T>& O){
if (this!=&O)
copylist(O);
return *this;
}
template <class T>
SingleLink<T>::~SingleLink(){
RemoveAll();
}
//3.IsFull(),Length()
template <class T>
bool SingleLink<T>::IsFull(){
return (First==NULL);
}
template <class T>
int SingleLink<T>::Length(){
return count;
}
//4.FindItem(item),GetFirst(),GetLast()
template <class T>
bool SingleLink<T>::FindItem(const T & item){
NodeType<T> *current;
bool found=false;
current=First;
while(current!=NULL && !found){
if (current->info==item)
found=true;
else
current=current->link;
}//end while
return found;
}
template <class T>
const T SingleLink<T>::GetFirst()const{
assert(First!=NULL);
return First->info;
}
template <class T>
const T SingleLink<T>::GetLast()const{
assert(Last!=NULL);
return Last->info;
}
//5.InsertHead(item),InsertEnd(item)
template <class T>
void SingleLink<T>::InsertHead(const T& item){
NodeType<T> * newnode;
newnode=new NodeType<T>;
assert(newnode!=NULL);
newnode->info=item;
newnode->link=First;
First=newnode;
count ;
if(Last=NULL)
Last=newnode;
}
template <class T>
void SingleLink<T>::InsertEnd(const T& item){
NodeType<T> * newnode;
newnode=new NodeType<T>;
assert(newnode!=NULL);
newnode->info=item;
newnode->link=NULL;
if(First==NULL){
First=newnode;
Last=newnode;
count ;
}
else
{
Last->link=NULL;
Last=newnode;
count ;
}
}
//6.RemoveNode(item),RemoveAll()
template <class T>
void SingleLink<T>::RemoveNode(const T& item){
NodeType<T> *current,*trailcurrent;
bool found;
if(First==NULL)
cerr<<"can not delete from a empty list."<<endl;
//case 2
if(First->info==item){
current=First;
First=First->link;
count--;
}
//case 3
if(First!=NULL)
{
found=false;
trailcurrent=First;
current=First->link;
while(current!=NULL && !found)
{
if(current->info!=item){
trailcurrent=current;
current=current->link;
}
else
found=true;
}//end while
if (found){
trailcurrent->link=current->link;
count--;
if(Last==current)
Last=trailcurrent;
delete current;
}//end found
}//end else
}
template <class T>
void SingleLink<T>::RemoveAll(){
NodeType<T> * temp;
while(First!=NULL){
temp=First;
First=First->link;
delete temp;
}
First=NULL;
Last=NULL;
count=0;
}
#endif