#include "ListNode.h"
template
class SingleList{
public:
SingleList(){head = new ListNode;}
~SingleList(){
makeEmpty();
delete head;
}
public:
void makeEmpty();
int length();
ListNode* find(Type item , int n)const;
ListNode* find(int n)const;
bool insert(Type item, int n =0);
Type remove(int n);
bool removeAll(Type item);
Type get(int n);
void ReverseSinglyLinkedList();
void print();
private:
ListNode *head;
};
template
void SingleList::makeEmpty(){
ListNode * pdel = NULL;
while(head->m_next!=NULL){
pdel = head->m_next;
head->m_next = pdel->m_next;
delete pdel;
}
}
template
int SingleList::length(){
int count = 0 ;
ListNode *p = head->m_next;
while (p!=NULL){
p = p->m_next;
count++;
}
return count;
}
template
ListNode*SingleList::find(int n)const{
if(n<0){cout << "out of the boundary"<< endl
;return NULL;}
ListNode* p = head;
for(int i =0 ; i<=n&&p ;i++){
p = p->m_next;
}
if(p==NULL){
cout << "can't find the element"<
return NULL;
}
return p;
}
template
ListNode* SingleList::find(Type item , int
n)const{
if(n<1){
cout << "the n is illegal" << endl;
return NULL;
}
ListNode *p = head;
int count = 0;
while(count!=n && p){
p=p->m_next;
if(p->m_data==item)
count++;
}
if(p==NULL){
cout << "can't find the element"<
return NULL;
}
return p;
}
template
bool SingleList::insert(Type item, int n=0){
ListNode *p = head;
if(n<0){
cout << "the n is illegal"<
return false;
}
ListNode* pnode = new ListNode(item);
ListNode* pmove = head;
if(pnode==NULL){
cout << "Application is error "<< endl;
return false;
}
for(int i = 0 ; i
pmove = pmove->m_next;
}
if(pmove==NULL){
cout << "the n is illegal " << endl;
return false;
}
pnode->m_next = pmove->m_next;
pmove->m_next = pnode;
return true;
}
template
Type SingleList::remove(int n){
if(n<0){
cout << "can't find th element"<
exit(0);
}
ListNode* pmove=head ,*pdel;
for(int i = 0 ; i
pmove = pmove->m_next;
}
if(pmove->m_next==NULL){
cout << "can't find the element" <
exit(0);
}
pdel = pmove->m_next;
pmove->m_next = pdel->m_next;
Type item = pdel->m_data;
delete pdel;
return item;
}
template
bool SingleList::removeAll(Type item){
ListNode *pmove = head,*pdel = pmove->m_next;
while(pdel!=NULL){
if(pdel->m_data==item){
pmove->m_next = pdel->m_next;
delete pdel;
pdel = pmove->m_next;
continue;
}
pmove=pmove->m_next;
pdel = pdel->m_next;
pmove = pmove->m_next;
}
return true;
}
template
Type SingleList::get(int n){
if(n<0){
cout << "can't find the element "<
exit(0);
}
ListNode *pmove =head->m_next;
for(int i =0 ; i
pmove = pmove->m_next;
if(NULL == pmove){
cout << "can't find the element "<
exit(0);
}
}
return pmove->m_data;
}
template
void SingleList::print(){
ListNode *pmove = head->m_next;
cout << "head";
while (pmove!=NULL){
cout << "->" << pmove->m_data;
pmove = pmove->m_next;
}
cout << endl;
}
template
void SingleList::ReverseSinglyLinkedList(){
if(head->m_next==NULL)return;
ListNode *pmove = head->m_next;
ListNode *temp ;
while(pmove->m_next!=NULL){
temp = pmove->m_next;
pmove->m_next = temp->m_next;
temp->m_next = head->m_next;
head->m_next = temp;
}
}