/*
*
*/
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");
}
}
*
*/
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");
}
}
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都说以参数方式传递进来的.那前一个是何时和怎样得到当前对象的值?
实现字定义对象比较 直接实现Comparator接口既可
谢谢大家哈
Student类实现了接口GComparable方法compareTo,Student类是属性code在执行code > stu.code时,code是怎样得到值的?
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++)
{..........................}
}
}
你很表达方式很好,学习了
(GComparable)p.data是接口指针,它是怎么对Student的code进行赋值呢?
{
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]
return 1;那怎样对if语句里面第一个code赋值呢?是何种方式!
我终于明白楼主在想什么了
楼主之所以不明白是对这个程序的运行过程还没弄明白,建议你一步一步的Debug看一下整个程序是怎么跑的,也就不需要我解释了。
你只是一下子没转过弯