/***************************************************************************
* file name : LinkNode.h
* created : 2011/11/02
* description :
* author : Gavin Dai XLX
* update :
****************************************************************************/
#ifndef __LINKNODE_H
#define __LINKNODE_H
#include <iostream>
using namespace std;
template <class T>
class SingleList;
template <class T>
class ListNode
{
private:
friend class SingleList<T>;
ListNode() : m_pnext(NULL){ }
ListNode(const T item, ListNode<T> *next = NULL) : m_data(item), m_pnext(next) { }
~ListNode() { m_pnext = NULL; }
public:
T getData();
friend ostream& operator<<(ostream& , ListNode<T>&);
private:
T m_data;
ListNode<T>* m_pnext;
};
template <class T>
T ListNode<T>::getData()
{
return this->m_data;
}
template <class T>
ostream& operator<<(ostream& os, ListNode<T>& out)
{
os<<out.m_data;
return os;
}
#endif
/***************************************************************************
* file name : SingleList.h
* created : 2011/11/02
* description :
* author : Gavin Dai XLX
* update :
****************************************************************************/
#ifndef __SINGLELIST_H
#define __SINGLELIST_H
#include "LinkNode.h"
template <class T>
class SingleList
{
public:
//----------------------------------------------------
// Name: SingleList::SingleList - public
// Description: Construct function, construct a link list contain a head node
// Arguments: None
// Return Value: None
//----------------------------------------------------
SingleList() : head(new ListNode<T>()) { }
~SingleList()
{
makeEmpty();
delete head;
}
void makeEmpty();
int length();
ListNode<T>* find(T value);
//ListNode<T>* find(int n);
bool insert(T item, int n = 0);
T remove(int n = 0);
bool removeAll(T item);
T get(int n);
void print();
private:
ListNode<T> *head;
};
template <class T>
void SingleList<T>::makeEmpty()
{
ListNode<T> *pdel;
while(head->m_pnext != NULL)
{
pdel = head->m_pnext;
head->m_pnext = pdel->m_pnext;
delete pdel;
}
}
template <class T>
int SingleList<T>::length()
{
ListNode<T> *pmove = head->m_pnext;
int count = 0;
while(pmove != NULL)
{
pmove = pmove->m_pnext;
count++;
}
return count;
}
//template <class T>
//ListNode<T>* SingleList<T>::find(int n)
//{
// if (n < 0)
// {
// cout<<" The n is out of boundary"<<endl;
// return NULL;
// }
// ListNode<T> *pmove = head->m_pnext;
// for (int i = 0; i < n && pmove; i++)
// {
// pmove = pmove->m_pnext;
// }
// if (pmove == NULL)
// {
// cout<<"The n is out of boundary"<<endl;
// return NULL;
// }
// return pmove;
//
//}
template <class T>
ListNode<T>* SingleList<T>::find(T value)
{
ListNode<T>* pmove = head->m_pnext;
while(pmove != NULL)
{
if (pmove->m_data == value)
{
return pmove;
}
pmove = pmove->m_pnext;
}
return pmove;
}
//----------------------------------------------------------
// Name; SingleList::insert - public
// Description: insert item before node n.
// Arguments: item - the node value you want to insert, n - insert before n.
// Return Value:
//-----------------------------------------------------------
template <class T>
bool SingleList<T>::insert(T item, int n = 0)
{
if (n < 0)
{
cout<<"The n is illegal"<<endl;
return false;
}
ListNode<T>* pmove = head;
ListNode<T>* pnode = new ListNode<T>(item);
if (pnode == NULL)
{
cout<<"Application error!"<<endl;
return false;
}
for (int i = 0; i < n && pmove; i++)
{
pmove = pmove->m_pnext;
}
if (pmove == NULL)
{
return false;
}
pnode->m_pnext = pmove->m_pnext;
pmove->m_pnext = pnode;
return true;
}
template <class T>
bool SingleList<T>::removeAll(T item)
{
ListNode<T>* pmove = head;
ListNode<T>* pdel = head->m_pnext;
while(pdel != NULL)
{
if (pdel->m_data == item)
{
pmove->m_pnext = pdel->m_pnext;
delete pdel;
pdel = pmove->m_pnext;
continue;
}
pmove = pmove->m_pnext;
pdel = pdel->m_pnext;
}
return true;
}
template <class T>
T SingleList<T>::remove(int n = 0)
{
if(n < 0)
{
cout<<"can't find the element"<<endl;
exit(1);
}
ListNode<T> *pmove = head, *pdel;
for (int i = 0; i < n && pmove->m_pnext; i++)
{
pmove = pmove->m_pnext;
}
if (pmove->m_pnext == NULL)
{
cout<<"cant't find the element"<<endl;
exit(1);
}
pdel = pmove->m_pnext;
pmove->m_pnext = pdel->m_pnext;
T temp = pdel->m_data;
delete pdel;
return temp;
}
template <class T>
T SingleList<T>::get(int n)
{
if (n < 0)
{
cout<<"The n is out of boundry"<<endl;
exit(1);
}
ListNode<T> *pmove = head->m_pnext;
for (int i = 0; i < n; i++)
{
pmove = pmove->m_pnext;
if (NULL == pmove)
{
cout<<"The n is out of boundary"<<endl;
exit(1);
}
}
return pmove->m_data;
}
template <class T>
void SingleList<T>::print()
{
ListNode<T>* pmove = head->m_pnext;
cout<<"head ";
while(pmove)
{
cout<<"-->"<<pmove->m_data;
pmove = pmove->m_pnext;
}
cout<<"--->over"<<endl<<endl<<endl;
}
#endif
/***************************************************************************
* file name : main.cpp
* created : 2011/11/02
* description :
* author : Gavin Dai XLX
* update :
****************************************************************************/
#include "SingleList.h"
int main()
{
SingleList<int> list;
for (int i = 0; i < 20; i++)
{
list.insert(3 * i, i);
}
list.print();
list.insert(12,8);
list.print();
cout<<"find"<<list.find(18)<<endl;
//list.removeAll(12);
cout<<list.remove(3)<<endl;
list.print();
list.makeEmpty();
cout<<"the length of the list "<<list.length();
system("pause");
return 1;
}