class ListNode {
public:
ListNode(const T &theData,ListNode<T>* afterMe);
ListNode<T>* getLink();
void setLink(ListNode<T> *node);
const T getData();
virtual ~ListNode();
private:
ListNode<T> *next;
T data;
};
template<class T>
ListNode<T>::ListNode(const T &theData,ListNode<T>* afterMe){
data = theData;
if(afterMe) afterMe->setLink(this);
next = NULL;
}
template<class T>
ListNode<T>::~ListNode(){
}
template<class T>
ListNode<T>* ListNode<T>::getLink(){
return next;
}
template<class T>
void ListNode<T>::setLink(ListNode<T> *node){
next = node;
}
template<class T>
const T ListNode<T>::getData(){
return data;
}
class ListIterator {
public:
ListIterator(ListNode<T>* thePtr);
virtual ~ListIterator();
const ListIterator<T> operator++(int);
const ListIterator<T> operator++();
bool operator==(const ListIterator rSide);
bool operator!=(const ListIterator rSide);
const T operator*();
private:
ListNode<T> *ptr;
};
template<class T>
ListIterator<T>::ListIterator(ListNode<T> *thePtr) {
// TODO Auto-generated constructor stub
ptr = thePtr;
}
template<class T>
const ListIterator<T> ListIterator<T>::operator++(int){
ListIterator<T> t = *this;
ptr = ptr->getLink();
return t;
}
template<class T>
const ListIterator<T> ListIterator<T>::operator++(){
ptr = ptr->getLink();
return *this;
}
template<class T>
const T ListIterator<T>::operator*(){
return ptr->getData();
}
template<class T>
bool ListIterator<T>::operator ==(ListIterator<T> rSide){
return ptr == rSide.ptr;
}
template<class T>
bool ListIterator<T>::operator !=(ListIterator<T> rSide){
return (ptr != rSide.ptr);
}
template<class T>
ListIterator<T>::~ListIterator() {
// TODO Auto-generated destructor stub
}
template<class T>
class LinkedList {
public:
typedef ListIterator<T> Iterator;
LinkedList();
const Iterator begin(){return Iterator(head);}
const Iterator end(){return Iterator(NULL);}
int getCount();
const T front();
const T back();
void orderInsert(const T &theData);
void headInsert(const T &theData);
void tailInsert(const T &theData);
void deleteNode(int pos);
void print();
virtual ~LinkedList();
private:
ListNode<T>* getNodeReference(int pos);
bool locate(const T &data,ListNode<T>* &before);
ListNode<T> *head,*tail;
};
}
namespace MyNameSpace{
template <class T>
LinkedList<T>::LinkedList():head(NULL),tail(NULL){
}
template <class T>
LinkedList<T>::~LinkedList() {
ListNode<T> *p,*q;
p = head;
while(p != NULL){
q = p;
p = p->getLink();
delete q;
}
}
template <class T>
int LinkedList<T>::getCount(){
ListNode<T> *p;
p = head;
int size = 0;
while(p != NULL){
p = p->getLink();
size++;
}
return size;
}
template <class T>
const T LinkedList<T>::front(){
return head->getData();
}
template <class T>
const T LinkedList<T>::back(){
return tail->getData();
}
template <class T>
void LinkedList<T>::orderInsert(const T& theData){
ListNode<T> *before;
bool in = locate(theData,before);
if(in) return;
if(before == NULL){
headInsert(theData);
}else{
ListNode<T> *node = new ListNode<T>(theData,NULL);
node->setLink(before->getLink());
before->setLink(node);
if(before == tail) tail = node;
}
}
template <class T>
void LinkedList<T>::headInsert(const T& theData){
ListNode<T> *node = new ListNode<T>(theData,NULL);
node->setLink(head);
if(head) head = node;
else head = tail = node;
}
template <class T>
void LinkedList<T>::tailInsert(const T &theData){
ListNode<T> *node = new ListNode<T>(theData,NULL);
if(tail){
tail->setLink(node);
tail = node;
}else{
head = tail = node;
}
}
template <class T>
void LinkedList<T>::deleteNode(int pos){
if(pos < 1 || pos > getCount())return;
if(pos == 1){
ListNode<T> *tmp = head;
head = head->getLink();
if(!head) tail=NULL;
delete tmp;
return;
}
ListNode<T> *before = getNodeReference(pos-1);
ListNode<T> *tmp = before->getLink();
before->setLink(tmp->getLink());
delete tmp;
}
template <class T>
void LinkedList<T>::print(){
for(ListNode<T> *p = head;p!=NULL;p=p->getLink()){
printf("%d",p->getData());
}
}
template <class T>
ListNode<T>* LinkedList<T>::getNodeReference(int pos){
ListNode<T> *p = head;
for(int i = 1; i < pos; i++){
p = p->getLink();
}
return p;
}
template <class T>
bool LinkedList<T>::locate(const T &data,ListNode<T>* &before){
ListNode<T> *p,*q;
p = q = head;
while(p && p->getData() < data){
q = p;
p = p->getLink();
}
before = q;
return (p) && (p->getData() == data);
}
}
#include "LinkedList.cpp"
using MyNameSpace::LinkedList;
int main(int argc,char *argv[]){
LinkedList<int> *list = new LinkedList<int>();
for(int i = 0; i < 10; i++)
list->orderInsert(i);
list->tailInsert(10);
list->orderInsert(11);
for(LinkedList<int>::Iterator iter = list->begin(); iter != list->end(); iter++){
printf("%d ",*iter);
}
printf("\n");
delete list;
}