/* 利用C++语言,实现循环单链表各种算法:建立链表、链表插入、链表删除、链表查找等。本可以把主菜单也做出来,但由于时间关系,主菜单暂时没加,此版本 只为演示链表的操作C++形式,即把C中的面向过程改为面向对象,表现为链表类。此外还有些输入类型检验等待完善*/
/* main.cpp */
#include <iostream.h>
#include <stdlib.h>
#include "CircList.h"
#include <string.h>
//using namespace std;
void main(int argc, char *argv[])
{
CircList cc;
cc.GetData();
cc.Show();
//system("PAUSE");
cout<<"The List`s length is:"<<cc.Length()<<endl;
cc.Find();
cc.Show();
cc.Insert();
cc.Show();
cc.Remove();
cc.Show();
exit(0);
}
/* CircList.h */
#ifndef _CircList_h
#define _CircList_h
class CircListNode{
friend class CircList;
public:
CircListNode(int d=0):data(d),link(NULL){}
private:
int data;
CircListNode *link;
};
class CircList{ //带头结点的
public:
CircList();
~CircList();
int Length() const; //Length of the Circle List
bool IsEmpty(){return first->link==first;}
bool Find(); //find the value in the CircleList
void GetData();
void Prior(); //指针前移
void Insert(); //insert a node in the position
int Remove(); //delete a node and return the data
void Show();
private:
CircListNode *first,*current,*last;
};
#endif
/* CircList.cpp*/
#include <iostream>
#include <stdlib.h>
#include "CircList.h"
#include <string.h>
using namespace std;
CircList::CircList(){
first=current=new CircListNode();
last=first;
}
CircList::~CircList(){
CircListNode *pcNode=first;
while((first->link)!=last){ //从头节点开始,逐个删除
pcNode=first;
first=first->link;
last=first;
delete pcNode;
}
delete first;
}
int CircList::Length()const{
int len=0;
CircListNode *pcNode=first;
while((pcNode->link)!=first){
len++;
pcNode=pcNode->link;
}
return len;
}
void CircList::GetData(){
cout<<"please input the integer figure that you want input to list:"<<endl;
cout<<"The q is to end!"<<endl;//实际上,只要是非INT的输入都会结束循环
int temp;
cin>>temp;
if(cin.good())
{
current=new CircListNode();
current->data=temp;
first->link=current;
last=current;
last->link=first;
for(int i=1;i;i)
{
cin>>temp;
if(cin.good())
{
current=new CircListNode();
current->data=temp;
last->link=current;
last=last->link;
last->link=first;
}
else
{
cin.clear();//清除缓存区
cin.ignore(10000,'/n');
i=0;
};
};
}
else
{first->link=last;
cin.clear();
cin.ignore(10000,'/n');
};
#include <iostream.h>
#include <stdlib.h>
#include "CircList.h"
#include <string.h>
//using namespace std;
void main(int argc, char *argv[])
{
CircList cc;
cc.GetData();
cc.Show();
//system("PAUSE");
cout<<"The List`s length is:"<<cc.Length()<<endl;
cc.Find();
cc.Show();
cc.Insert();
cc.Show();
cc.Remove();
cc.Show();
exit(0);
}
/* CircList.h */
#ifndef _CircList_h
#define _CircList_h
class CircListNode{
friend class CircList;
public:
CircListNode(int d=0):data(d),link(NULL){}
private:
int data;
CircListNode *link;
};
class CircList{ //带头结点的
public:
CircList();
~CircList();
int Length() const; //Length of the Circle List
bool IsEmpty(){return first->link==first;}
bool Find(); //find the value in the CircleList
void GetData();
void Prior(); //指针前移
void Insert(); //insert a node in the position
int Remove(); //delete a node and return the data
void Show();
private:
CircListNode *first,*current,*last;
};
#endif
/* CircList.cpp*/
#include <iostream>
#include <stdlib.h>
#include "CircList.h"
#include <string.h>
using namespace std;
CircList::CircList(){
first=current=new CircListNode();
last=first;
}
CircList::~CircList(){
CircListNode *pcNode=first;
while((first->link)!=last){ //从头节点开始,逐个删除
pcNode=first;
first=first->link;
last=first;
delete pcNode;
}
delete first;
}
int CircList::Length()const{
int len=0;
CircListNode *pcNode=first;
while((pcNode->link)!=first){
len++;
pcNode=pcNode->link;
}
return len;
}
void CircList::GetData(){
cout<<"please input the integer figure that you want input to list:"<<endl;
cout<<"The q is to end!"<<endl;//实际上,只要是非INT的输入都会结束循环
int temp;
cin>>temp;
if(cin.good())
{
current=new CircListNode();
current->data=temp;
first->link=current;
last=current;
last->link=first;
for(int i=1;i;i)
{
cin>>temp;
if(cin.good())
{
current=new CircListNode();
current->data=temp;
last->link=current;
last=last->link;
last->link=first;
}
else
{
cin.clear();//清除缓存区
cin.ignore(10000,'/n');
i=0;
};
};
}
else
{first->link=last;
cin.clear();
cin.ignore(10000,'/n');
};
}
void CircList::Show(){
cout<<"The List number is:/t";
CircListNode *p=first->link;
do{
cout<<p->data<<"/t";
p=p->link;
}while(p!=first);
}
bool CircList::Find(){//find the value in the CircleList
int value;
cout<<"Please input the figure that you want to find!"<<endl;
//cin.rdstate();
cin>>value;
CircListNode *p=first;
while(p->data!=value && p->link!=first){
p=p->link;
};
if(p->data==value){
cout<<"There has the number."<<endl;
return true;}
else if(p->link=first){
cout<<"There hasn`t the number."<<endl;
return false;};
}
void CircList::Insert(){
int position,figure;
cout<<"/nPlease input the position that you want to insert."<<endl;
cin>>position;
cout<<"/nPlease input the figure that you want to insert."<<endl;
cin>>figure;
cout<<endl;
CircListNode *p=first;
if(position>Length()||position<1)
cout<<"The position is over!";
else{
for(int i=2;i<=position;i++){//在第position个位置插入即为figure插入后为list的第position+1个元素,
p=p->link; //如position=1那么p=first,figure会成为新List的第二node;
};
CircListNode *insert=new CircListNode();//因为是单链表,所以插入位置选在了P的后面;
insert->data=figure;
insert->link=p->link;
p->link=insert;
};
}
void CircList::Prior(){
int a=Length();
for(int i=1;i<=a;i++){
current=current->link;
};
}
int CircList::Remove(){
int position;
cout<<"/nPlease input the position that you want to remove."<<endl;
cin>>position;
cout<<endl;
if(position>Length()||position<1)
cout<<"The position is over!";
else{
current=first->link;
for(int i=2;i<=position;i++){
current=current->link;
};
CircListNode *p=current;
int a=p->data;
Prior();
current->link=current->link->link;
delete p;
cout<<"The data that you removed is : "<<a<<endl;
return a;
}
}
while(p->data!=value && p->link!=first){
p=p->link;
};
if(p->data==value){
cout<<"There has the number."<<endl;
return true;}
else if(p->link=first){
cout<<"There hasn`t the number."<<endl;
return false;};
}
void CircList::Insert(){
int position,figure;
cout<<"/nPlease input the position that you want to insert."<<endl;
cin>>position;
cout<<"/nPlease input the figure that you want to insert."<<endl;
cin>>figure;
cout<<endl;
CircListNode *p=first;
if(position>Length()||position<1)
cout<<"The position is over!";
else{
for(int i=2;i<=position;i++){//在第position个位置插入即为figure插入后为list的第position+1个元素,
p=p->link; //如position=1那么p=first,figure会成为新List的第二node;
};
CircListNode *insert=new CircListNode();//因为是单链表,所以插入位置选在了P的后面;
insert->data=figure;
insert->link=p->link;
p->link=insert;
};
}
void CircList::Prior(){
int a=Length();
for(int i=1;i<=a;i++){
current=current->link;
};
}
int CircList::Remove(){
int position;
cout<<"/nPlease input the position that you want to remove."<<endl;
cin>>position;
cout<<endl;
if(position>Length()||position<1)
cout<<"The position is over!";
else{
current=first->link;
for(int i=2;i<=position;i++){
current=current->link;
};
CircListNode *p=current;
int a=p->data;
Prior();
current->link=current->link->link;
delete p;
cout<<"The data that you removed is : "<<a<<endl;
return a;
}
}