注册 登录
编程论坛 C++教室

困惑啊。不知道怎么将A链表分成奇偶的A,B链表。编了个程序,没错误但是出不来。求指导修改啊!!!

鱼鱼儿丸子 发布于 2012-03-06 22:01, 699 次点击
#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);
    }



8 回复
#2
非死亡!2012-03-06 22:37
分数太少了吧  我不是看重分的人 你也知道的  唉  
#3
榴紫丫2012-03-06 23:14
编译,连接没错,执行有错啊,好像creat_R ();这个函数有问题吧,自己调试一下呗
你好赖也有个注释吧,只有代码,没有文字描述
#4
pangding2012-03-06 23:33
什么叫分成奇偶的?你先用语言把意途描述清楚一点。
然后在你的代码里把有关分链表的部分变变色呀,或者给个注释,这样能帮助我们定位错误。
#5
鱼鱼儿丸子2012-03-07 09:29
回复 4楼 pangding
嗯。就是将单链表A分解成两个单链表A和B。使得A链表中含有原链表A中序号为奇数的元素。而B链表含有原链表A中序号为偶数的元素,且保持原来的相对顺序。
其实有困惑的代码为这一段。昨天对题目理解错了。
void list::display (){
        node*p=head->next ;
        while(p!=NULL){
            cout<<p->data <<" ";
            p=p->next ;
        }
    }

void list::divide (list a,list b){
    node*p,*k,*r,*q;
    k=b.get_head ();
    p=a.get_head ();
    k=a.get_head()->next ;
    r=k;
    while(p!=NULL&&p->next !=NULL){
        q=p->next ;
        p->next =q->next ;
        r->next =q;
        r=q;
        p=p->next ;
    }
    r->next =NULL;
}
void main(){
    list a,b,c;
    a.creat_R ();
c.divide (a,b);
    a.display ();
b.display ();
}
#6
pangding2012-03-07 22:01
有点明白你的意思了。
你那个 main 里的 list c 是干什么用的?
#7
pangding2012-03-07 22:37
另外你要如果是想把链表 a 分到 b 里去。恐怕得传一下 b 的引用。要不然操作的是 b 的“分身”。
#8
鱼鱼儿丸子2012-03-09 12:29
回复 6楼 pangding
list c 是用来调用那个divide函数的。B不是个空链表么,为什么还是要引用??如果改成引用的形式能够跑出结果么?
#9
hebinmyland2012-03-26 16:08
如果是根据data来划分成两个链表的话:
         1.divide()中的if..else。。并且a的
1