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