单链表
//
// 单链表
// TestList
//
// Created by chenshang on 14-2-7.
// Copyright (c) 2014年 chenshang. All rights reserved.
//
#ifndef TestList_SingleList_h
#define TestList_SingleList_h
#include <iostream>
using namespace std;
typedef int T;
class ListNode{
public:
ListNode():next(NULL){}
ListNode(const T item):data(item),next(NULL){}
~ListNode(){
next=NULL;
}
T data;
ListNode* next;
};
class SingleList{
public:
SingleList():head(new ListNode()){}
~SingleList(){
Empty();
delete head;
}
public:
void Empty();
int length();
ListNode* Find(T value, int n);
ListNode* 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 *head;
};
void SingleList::Empty(){
ListNode* pdel;
while (head->next!=NULL) {
pdel = head->next;
head->next = pdel->next;
delete pdel;
}
}
int SingleList::length(){
ListNode* pmove =head->next;
int count=0;
while (pmove!=NULL) {
pmove=pmove->next;
count++;
}
return count;
}
ListNode* SingleList::Find(int n){
if (n<0) {
cout<<"The n is out of boundary"<<endl;
return NULL;
}
ListNode* pmove=head;
for(int i=0;i<n;i++){
pmove=pmove->next;
if (pmove==NULL) {
cout<<"The n is out of boundary"<<endl;
return NULL;
}
}
return pmove;
}
ListNode* SingleList::Find(T value,int n){
if (n<1) {
cout<<"The n is illegal"<<endl;
return NULL;
}
ListNode* pmove = head;
int count =0;
while (count!=n) {
pmove = pmove->next;
if (pmove->data==value) {
count++;
}
if (pmove==NULL) {
cout<<"can not find the element"<<endl;
return NULL;
}
}
return pmove;
}
bool SingleList::insert(T item,int n){
if (n<0) {
cout<<"The n is out of boundary"<<endl;
return 0;
}
ListNode *pmove = head;
ListNode *pnode = new ListNode(item);
if (pnode==NULL) {
cout<<"Appliaction error!"<<endl;
return false;
}
for (int i=0; i<n&&pmove; i++) {
pmove = pmove->next;
if (pmove==NULL) {
cout<<"The n is out of boundary" <<endl;
}
}
pnode->next = pmove ->next;
pmove->next = pnode;
return true;
}
bool SingleList::removeAll(T item){
ListNode* pmove = head;
ListNode* pdel = head->next;
while (pdel!=NULL) {
if (pdel->data == item) {
pmove->next = pdel->next;
delete pdel;
pdel = pmove->next;
continue;
}
pmove=pmove->next;
pdel=pdel->next;
}
return true;
}
T SingleList::remove(int n){
if (n<0) {
cout<<"can not find the element"<<endl;
return false;
}
ListNode* pmove = head;
ListNode* pdel;
for (int i=0; i<n&&pmove->next!=head; i++) {
//n次循环后,指向当前第n个结点的指针
pmove=pmove->next;
}
if (pmove->next==NULL) {
cout<<"can not find the element"<<endl;
return false;
}
//指向n的下一个结点
pdel = pmove->next;
pmove->next = pdel->next;
T temp =pdel->data;
delete pdel;
return temp;
}
T SingleList::get(int n){
if (n<0) {
cout<<"The n is out of boundary"<<endl;
exit(1);
}
ListNode* pmove = head;
for (int i=0; i<n; i++) {
pmove=pmove->next;
if (pmove==NULL) {
cout<<"The n is out of boundary"<<endl;
exit(1);
}
}
return pmove->data;
}
void SingleList::print(){
ListNode* pmove=head->next;
cout<<"head";
while(pmove!=NULL){
cout<<"--->"<<pmove->data;
pmove=pmove->next;
}
cout<<"--->over"<<endl<<endl<<endl;
}
#endif