单链表

单链表

//
//  单链表
//  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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值