单项循环链表

单项循环链表

//
//  单项循环链表
//
//  Created by chenshang on 14-2-6.
//  Copyright (c) 2014年 chenshang. All rights reserved.
//

#ifndef TestList_CircularList_h
#define TestList_CircularList_h

#include <iostream>
using namespace std;
typedef int T;
//创建一个节点
class ListNode
{
public:
    ListNode():next(NULL){}//构造方法,初始化,目的为了创建头结点
    ListNode(const T &d) :data(d), next(NULL){} //构造方法,初始化,创建结点
    T data;
    ListNode * next;
};
//创建单项循环链表
class CircularList{
public:
    //初始化单项循环链表,并创建一个头结点
    CircularList():head(new ListNode())
    {
        head->next = head;
    }
    ~CircularList()
    {
        MakeEmpty();
        delete head;
    }
public:
    void MakeEmpty();//清除
    int Length(); //链表的长度
    ListNode* Find(T value,int n);//找到第n个与value值相等的结点
    ListNode* Find(int n);//找到第n个结点
    bool Insert(T item, int n=0);//往第n个结点的后面插入一个结点,如果要插入第一个结点,就应该在第零个之后
    T Remove(int n=0); //删除第n个结点之后的结点,如果要删除第一个结点,就应该在第零个之后
    bool RemoveAll(T item);//删除所有值为item的结点
    T Get(int n);//得到第n个结点的数据
    void Print(); //打印链表
    
private:
    ListNode *head;
};
void CircularList::MakeEmpty(){
    ListNode *pdel,*pmove =head;
    //算法解释:pmove不动,移动pdel
    while (pmove->next!=head) {
        pdel=pmove->next;
        pmove->next=pdel->next;
        delete pdel;
    }
}
int CircularList::Length(){
    ListNode* pmove =head;
    int count=0;
    while (pmove->next!=head) {
        pmove=pmove->next;
        count++;
    }
    return count;
}
ListNode* CircularList::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==head) {
            cout<<"The n is out of boundary"<<endl;
            return NULL;
        }
    }
    return pmove;
}
ListNode* CircularList::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==head) {
            cout<<"can not find the element"<<endl;
            return NULL;
        }
    }
    return pmove;
}
//往第n个结点后面插入一个结点,如果我们要插入第一个结点 n=0,即在头结点后插入一个结点。
bool CircularList::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; i++) {
        pmove = pmove->next;
        if (pmove==head&&pmove) {
            cout<<"The n is out of boundary" <<endl;
        }
    }
    pnode->next = pmove ->next;
    pmove->next = pnode;
    return true;
}
bool CircularList::RemoveAll(T item){
    ListNode* pmove = head;
    ListNode* pdel = head->next;
    while (pdel!=head) {
        if (pdel->data == item) {
            pmove->next = pdel->next;
            delete pdel;
            pdel = pmove->next;
            continue;
        }
        pmove=pmove->next;
        pdel=pdel->next;
    }
    return true;
}
//删除第n个结点之后的结点,如果要删除第一个,n=0;
T CircularList::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==head) {
        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 CircularList::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==head) {
            cout<<"The n is out of boundary"<<endl;
            exit(1);
        }
    }
    return pmove->data;
}

void CircularList::Print(){
    ListNode* pmove=head->next;
    cout<<"head";
    while(pmove!=head){
        cout<<"--->"<<pmove->data;
        pmove=pmove->next;
    }
    cout<<"--->over"<<endl<<endl<<endl;
}

#endif



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值