日期:2014-05-20  浏览次数:20666 次

求用java编一个双向链表的程序……
[b][/b][size=12px][/size]4、 设计一个链表结点类LinkNode(参考Link类),此类可以存放int、long、float、double、byte、short、String、StringBuffer类型的数据。用此类:a、随机产生100个整数(范围自定)的链表,在生成的过程中从小到大排列,然后输出;b、随机产生100个6个英文字母的单词的链表,在生成的过程中从小到大排列,然后输出。(和实验4中的链表不同的是,这里不仅仅是字符串链表, 提示: 使用Object类)
5、 A、在main()中使用LinkNode类创建4个实例,并赋予不同的值(long、double、StringBuffer、MyDate),然后使用Object中默认的toString()方法(从超级父类继承而来)显示结果。B、继承LinkNode类创建新类LinkNodeS,在其中重写Object中默认的toString()方法,在main()中用LinkNodeS类同样创建4个实例,并赋予和上面同样的值(long、double、StringBuffer、MyDate),观察使用新的toString()方法的效果,体会继承与多态。
6、 用继承来扩充题4中的链表结点类,使其成为双向链表,利用此双向链表来解决斗地主发牌问题。(引用可以直接移动到要操作的结点上,这带来一定的方便)


------解决方案--------------------
Java code
class Chain...{
    Chain pre=null,next=null;
    int id;
    String name;
}

class List...{
    private Chain header=new Chain();
    
    public Chain add(int id,String name)...{ //在链表尾添加节点
        Chain current=new Chain(); //创建链表头
        Chain temp=header;
    
        while(temp.next!=null) //循环至链表尾
            temp=temp.next;
        temp.next=current;
        current.pre=temp;    
        current.id=id;
        current.name=name;
        return current;
    }
    
    public Chain remove(int id)...{ //删除指定id的节点
        Chain temp=header;
        Chain current=null;
        while(temp.next!=null)...{
            temp=temp.next;
            if(temp.id==id)...{
                current=temp;
                break;
            }
        }
        if(current==null)
            return null;        
        
        current.pre.next=current.next;
        if(current.next!=null)
            current.next.pre=current.pre;
        return current;
    }
    
    public Chain remove(String name)...{ //删除指定name的节点
        Chain temp=header;
        Chain current=null;
        while(temp.next!=null)...{
            temp=temp.next;
            if(temp.name==name)...{
                current=temp;
                break;
            }
        }
        if(current==null)
            return null;
        
        current.pre.next=current.next;
        if(current.next!=null)
            current.next.pre=current.pre;
        return current;
    }    

    public Chain remove()...{ //删除最后一个节点
        Chain temp=header;
        while(temp.next.next!=null)...{
            temp=temp.next;
        }
        temp.next=null;    
        return temp;
    }
    
    public void clear()...{ //删除所有节点
        header.next=null;
    }
    
    public Chain insert(int id,String name,int pos)...{ //在指定位置插入节点
        Chain temp=header;
        Chain current=new Chain();
        int i=0;
        
        for(i=0;i<=pos;i++)...{
            if(temp.next!=null)...{
                temp=temp.next;
            }
            else...{
                return null;
            }        
        }
        
        current.id=id;
        current.name=name;
        
        if(temp.next!=null)...{
            temp.next.pre=current;
            current.next=temp.next;
        }
        temp.next=current;
        current.pre=temp;
        return current;    
    }
    
    public void print_all()...{
        Chain temp=header;
        
        System.out.println("--------------------------------------");
        while(temp.next!=null)...{
            temp=temp.next;
            System.out.println("ID: "+temp.id);
            System.out.println("Name: "+temp.name);
        }
        System.out.println("--------------------------------------");
    }
}