`
cloudtech
  • 浏览: 4610206 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

基本链表

 
阅读更多

list.h

#ifndef H_M
#define H_M

#include<iostream>
using namespace std;
namespace list{
class node{
int value;
node* next;

public:
node(const node&);
node(const int& v=int(),node* n=0):value(v),next(n){}
~node(){}

void s_val(int& v){value=v;}
void s_nex(node* n){next=n;}

int g_val()const{return value;}
node* g_nex()const{return next;}

bool operator<(node& b){return value<b.value;}
friend ostream& operator<<(ostream&,const node&);
};

class list{
node* head;
node* iter;
int length;

node* f_pre(const node*);

public:
explicit list(node* h=0):head(h),iter(head),length(0){}
list(const int& v,node* n=0):head(new node(v,n)),iter(head),length(1){}
~list();

int g_len()const{return length;}
bool is_tail(const node* aim){return !aim->g_nex();}

node* g_now(){
if(iter==0)iter=head;
return iter;}
node* g_nex(){
if(iter==0)
iter=head;
if(iter=iter->g_nex())
return iter;
}
node* g_head(){return head;}

void in_new(node*);
void insert(node*,node*);
void p_back(node*);
void p_front(node*);
node* find(int);
void del(node*);

void sort();
friend ostream& operator<<(ostream&,const list&);
};
};

#endif

list.cpp

#include"list.h"
using namespace list;
node::node(const node& n){
value=n.value;
next=0;
}
ostream& operator<<(ostream& os,const node& aim){
os<<aim.value;
if(aim.next)cout<<*(aim.next);
return os;
}

list::~list(){
iter=head;
while(iter){
head=head->g_nex();
delete iter;
iter=head;
}
}
node* list::f_pre(const node* aim){
node* temp=head;
if(aim==head){
return head;
}else{
for(;temp->g_nex()!=aim && (!is_tail(temp));temp=temp->g_nex());
}
return temp;
}
void list::in_new(node* aim){
cout<<"输入"<<endl;
int temp;
cin>>temp;
node* add=new node(temp);
insert(add,aim);
}
void list::insert(node* add,node* aim){
node* pre=f_pre(aim);
if(aim==head){
p_front(add);
}
else{
pre->s_nex(add);
add->s_nex(aim);
++length;
}
}
void list::p_back(node* add){
node* temp=head;
if(head==0){
head=add;
++length;
}
else{
for(;(!is_tail(temp));temp=temp->g_nex());
temp->s_nex(add);
add->s_nex(0);
++length;
}
}
void list::p_front(node* add){
add->s_nex(head);
head=add;
++length;
}
node* list::find(int aim){
node* temp=head;
for(;temp->g_val()!=aim&&(!is_tail(temp));temp=temp->g_nex());
return temp;
}
void list::del(node* aim){
if(aim==head){
if(is_tail(aim)){
cout<<"EMPTY";
return;
}
head=aim->g_nex();
delete aim;
--length;
}
else if(is_tail(aim)){
node* pre=f_pre(aim);
pre->s_nex(0);
delete aim;
--length;
}
else{
node* pre=f_pre(aim);
pre->s_nex(aim->g_nex());
delete aim;
--length;
}
}
void list::sort(){
node *end=head;
while(end->g_nex()){
node* i,*ite;
for(i=end->g_nex(),ite=head;(*ite)<(*i)&&ite!=end;ite=ite->g_nex());
if(ite==head &&i->g_val()<ite->g_val()){
p_front(new node(i->g_val()));
del(i);
}
else if((*end)<(*i)){
end=i;
}
else{
insert(new node(i->g_val()),ite);
del(i);
}
}
}

ostream& operator<<(ostream& os,const list& li){
os<<*(li.head);
return os;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics