困惑啊。不知道怎么将A链表分成奇偶的A,B链表。编了个程序,没错误但是出不来。求指导修改啊!!!
#include<iostream.h>enum error_code{success,arrange_error};
typedef struct node{
int data;
struct node*next;
}node;
class list{
public:
list();
~list();
void creat_R();
error_code get_element(const int i,int&x)const;
int length()const;
node*locate(const int x)const;
error_code insert(const int i,const int x);
error_code delete_element(const int i);
node * get_head(){return head;}
void divide(list a);
void display();
private:
int count;
node*head;
};
list::list (){
head=new node;
head->next =NULL;
count=0;
}
int list::length ()const{
return count;
}
void list::creat_R (){
int x;
cin>>x;
node*rear=head;
while(x!=-1){
count++;
node*s=new node;
s->data =x;
s->next =NULL;
rear->next =s;
rear=s;
cin>>x;
}
}
error_code list::get_element (const int i,int&x)const{
node*p;
int j;
p=head->next ; j=1;
while(p!=NULL&&j!=i){
p=p->next ;
j++;
}
x=p->data ;
return success;
}
node*list::locate (const int x)const{
node*p;
p=head->next ;
while(p!=NULL){
if(p->data ==x)return p;
else p=p->next ;
}
return NULL;
}
error_code list::insert (const int i,const int x){
node*p;
int j=0;
p=head;
while(j!=i-1&&p!=NULL){
p=p->next ;
j++;
}
if(i<1||i>count+1)
return arrange_error;
node*s=new node;
s->data =x;
s->next =p->next ;
p->next =s;
count++;
return success;
}
error_code list::delete_element (const int i){
node *u;
node*p=head;
int j=0;
while(j!=i-1&&p!=NULL){
p=p->next ;
j++;
}
if(i<1||i>count)
return arrange_error;
u=p->next ;
p->next =u->next ;
delete u;
count--;
return success;
}
list::~list (){
node*p=head->next ;
node*q;
while(p!=NULL){
q=p;
p=p->next ;
delete q;
}
p=NULL;
}
void list::display (){
node*p=head->next ;
while(p!=NULL){
cout<<p->data <<" ";
p=p->next ;
}
}
void list::divide (list a){
node*pa,*pb,*u,*v;
list b;
pa=a.get_head()->next ;
pb=b.get_head();
while(pa!=NULL){
if(pa->data %2==0){
u=new node;
u->data =pa->data ;
u->next =NULL;
pb->next =u;
pb=u;
b.count ++;
v=pa;
pa=v->next ;
delete v;}
}
b.display ();
a.display ();
}
void main(){
list a,c;
a.creat_R ();
c.divide (a);
}