/*
 * 
 */
 
package com.boole;interface GComparable
{
public int compareTo(Object o);
}class GListNode
{
protected Object  data = null; //数据域
protected GListNode  next = null; //尾指针

public GListNode() {
this.data = null;
this.next = null;
}

public GListNode(Object data) {
this.data = data;
this.next = null;
}

public Object get() {
return data;
}

public GListNode next() { 
return next;
}

public boolean hasNext() {
if(next != null) return true;
else return false;
}
}class GList {
protected GListNode  head; //头指针
protected int  count; //计数器

public GList() {
head = null;
count = 0;
}

public void addFirst(Object data) {
GListNode node = new GListNode(data);

//node.next=null.head内容保存至node.next
node.next = head;
//使得头指针指向第一个对象
head = node;
//count自加1
count++;
}

public void addLast(Object data) {
GListNode node = new GListNode(data);

if(count > 0) {
GListNode p = head;
while(p.next != null) p = p.next;
p.next = node;
}
else head = node;
count++;
}

public boolean insert(int index, Object data) {
if(index < 0 || index > count) return false;

if(index == 0) addFirst(data);
else {
GListNode node = new GListNode(data);
GListNode p = head;
for(int i=0; i<index-1; i++) p = p.next;
node.next = p.next;
p.next = node;
count++;
}
return true;
}

public boolean removeFirst() {
if(count == 0) return false;

GListNode p = head;
head = p.next;
p.data = null;
p = null;
count--;
return true;
}

public boolean removeLast() {
if(count == 0) return false;

if(count == 1) return removeFirst();
else {
GListNode p = head;
while(p.next.next!= null) p = p.next;
p.next.data = null;
p.next = null;
count--;
return true;
}
}

public boolean remove(int index) {
if(index < 0 || index >= count) return false;

if(index == 0) return removeFirst();
else {
GListNode p = head;
for(int i=0; i<index-1; i++) p = p.next;
p.next.data = null;
p.next = p.next.next;
count--;
return true;
}
}

public int count() {
return count;
}

public Object get(int index) {
if(index < 0 || index >= count) return null;

GListNode p = head;
for(int i=0; i<index; i++) p = p.next;
return p.data;
}

public boolean set(int index, Object data) {
if(index < 0 || index >= count) return false;

GListNode p = head;
for(int i=0; i<index; i++) p = p.next;
p.data = data;
return true;
}

public GListNode getHead() {
return head;
}

public void sort() {
GListNode p, q;
for(p=head; p.next!=null; p=p.next) {
for(q=p.next; q!=null; q=q.next) {
if(((GComparable)p.data).compareTo(q.data) > 0) {
Object tmp;
tmp = p.data;
p.data = q.data;
q.data = tmp;
}
}
}
}

// 查找,输入对象o中包含待查找的关键字段,返回查找对象
public Object search(Object o) {
GListNode p = head;
while(p != null) {
if(((GComparable)p.data).compareTo(o) == 0) return p.data;
p = p.next;
}

return null;
}

}class Student implements GComparable
{
public int code;

public Student(int code) {
this.code = code;
}

public int compareTo(Object o)
{
Student stu = (Student)o;
if(code > stu.code) return 1;
else if(code < stu.code) return -1;
else return 0;
}
}class TestApp 
{
public static void main(String args[]) {
GList list = new GList();

int i;
for(i=0; i<10; i++) list.addFirst(new Student(i));

list.removeFirst();
list.remove(3);
list.removeLast();

for(i=0; i<list.count(); i++) {
Student stu = (Student)(list.get(i));
System.out.print(stu.code+" ");
}
System.out.print("\n");

list.sort();
GListNode p = list.getHead();
while(p != null)
{
Student stu = (Student)(p.get());
System.out.print(stu.code+" ");
p = p.next();
}

System.out.print("\n");
Student stu = (Student)(list.search(new Student(0)));
if(stu != null) System.out.println(stu.code);
else System.out.println("not find");
}
}

解决方案 »

  1.   

    问题说明:
    GListNode类的if(((GComparable)p.data).compareTo(o) == 0) return p.data;中compareTo(o)和
    Student中实现方法接口GComparable的compareTo方法
    public int compareTo(Object o) 

    Student stu = (Student)o; 
    if(code > stu.code) return 1; 
    else if(code < stu.code) return -1; 
    else return 0; 


    TestApp类中search方法
    Student stu = (Student)(list.search(new Student(0))); 它们之间是如何工作的,code > stu.code中前一code比较大于符后一个code都说以参数方式传递进来的.那前一个是何时和怎样得到当前对象的值?
      

  2.   

    代码简单点不能表明问题吗?
    实现字定义对象比较 直接实现Comparator接口既可
      

  3.   

    实现Comparable接口以后,重写它的compareTo方法不就可以了吗?
      

  4.   

    code > stu.code中比较大于符后一个code都说以参数方式传递进来的.那前一个是何时和怎样得到当前对象的值?
    谢谢大家哈
      

  5.   

    Asinzy说得是
    Student类实现了接口GComparable方法compareTo,Student类是属性code在执行code > stu.code时,code是怎样得到值的?
      

  6.   

    晕头LZ表达能力咋这么牛.
    exp:
    public class Student implements Comparator{
        private String name;
    private int age;
    private String sex;
    public void setName(String name){
     this.name = name;
    }
    ........//构造geter seter
    public int compare(Object a, Object b){
       Student s1 = (Student) a;
       Student s2 = (Student) b;
       int i = 0 ;
       if(a.getAge()>b.getAge())
         i = 1;
    if(a.getAge()<b.getAge())
    i = -1;
    return i;
    }
    public static void main(String[] s){
      List l = new ArrayList();
      for(int i = 0 ;i < 100;i++){
       Student s = new Student();
       s.setName("name"+i);
       s.setAge(100 - i);
       l.add(s);
    }
       Collectons.sort(l);
    for(int i = 0 ;i < l.size();i++)
    {..........................}
    }
    }
      

  7.   

    a276202460
    你很表达方式很好,学习了
    (GComparable)p.data是接口指针,它是怎么对Student的code进行赋值呢?
      

  8.   

    这里的Code就是在New Student的的地方赋值的啊,也就是你Main方法里面New Student(i)的那句啦。class Student implements GComparable
    {
        public int code;    public Student(int code) {
          this.code = code;
        }
        public int compareTo(Object o)
        {
          Student stu = (Student)o;
          if(code > stu.code) 
            return 1;
          else if(code < stu.code) 
            return -1;
          else 
            return 0;
        }
    } [code]
    public static void main(String args[]) {
    GList list = new GList();int i;
    for(i=0; i <10; i++) 
        list.addFirst(new Student(i));
    .....
    [/code]
      

  9.   

    是的,在Student stu = (Student)(list.search(new Student(0))); new 中对要比较的对象赋值.if(code > stu.code) 
            return 1;那怎样对if语句里面第一个code赋值呢?是何种方式!
      

  10.   

    唉~~~
    我终于明白楼主在想什么了
    楼主之所以不明白是对这个程序的运行过程还没弄明白,建议你一步一步的Debug看一下整个程序是怎么跑的,也就不需要我解释了。
    你只是一下子没转过弯