读书人

LinkList 感触

发布时间: 2012-08-27 21:21:57 作者: rapoo

LinkList 感想

看了数据结构与算法 “链表” ,少许感悟

?

package cn.com.ld.dsaa.impl;

?

public class LinkList {

private Link first;

?

public LinkList() {

first = null;

}

?

public boolean isEmpty() {

return (first == null);

}

?

public void insertFirst(int iData, int dData) {

Link link = new Link(iData, dData);

link.next = first;

first = link;

}

?

public Link deleteFirst() {

Link temp = first;

first = first.next;

return temp;

}

?

public void displayLinkList() {

Link current = first;

while (current != null) {

current.displayLink();

current = current.next;

}

}

?

public Link find(int key) {

?

Link current = first;

while (current.iData != key) {

if (current.next == null)

return null;

else

current = current.next;

}

?

return current;

}

?

public Link delete(int key) {

Link previous = first;

Link current = first;

while (current.iData != key) {

if (current.next == null)

return null;

else {

previous = current;

current = current.next;

}

}

if (current == first)

first = first.next;

else

previous.next = current.next;

return current;

}

public static void main(String[] args) {

LinkList llist = new LinkList();

llist.insertFirst(10, 30);

llist.insertFirst(30, 60);

llist.insertFirst(60, 140);

llist.insertFirst(20, 20);

llist.insertFirst(70, 210);

llist.displayLinkList();

//while(! llist.isEmpty()){

//System.out.println("this linklist is null !");

//Link link = llist.deleteFirst() ;

//link.displayLink() ;

//}

Link l = llist.find(60) ;

if(l != null)

System.out.println("found link width key"+l.iData);

else

System.out.println("can't find link");

Link d = llist.delete(60);

if(d != null)

System.out.println("deleted link key "+ d.iData);

llist.displayLinkList();

}

}


?

?

linkList ?有点源自于内部元素管理上。由于每个link 都有一个自身对象的引用地址。

的插入 删除 ,仅是真对这个引用的增加 ,删除,不会直接对对象本身操作。

?

读书人网 >编程

热点推荐