请教链表删除类! 希望高手能写个链表里删除某个元素的类方法!!谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blany12.javaeye.com/blog/160279 Link current;Link previous;设置一个当前指针,一个前一个指针。找到后,把前一个指针连上当前指针的next。即找到元素后 previous.next = current.next; 回答的不全面package link;/** 链表(双端) */public class LinkedList<T, H> {// 链表头部private Link<T, H> first;// 链表尾部private Link<T, H> last;/** 构造器 初始化链表前尾部 */public LinkedList() { first = null; last = null;}/*** 添加元素到头部* * @param t* 键* @param h* 值* */public void insertFirst(T t, H h) { Link<T, H> newLink = new Link<T, H>(t, h); if (isEmpty()) { last = newLink; } newLink.next = first; first = newLink;}/*** 添加元素到尾部* * @param t* 键* @param h* 值* */public void insertLast(T t, H h) { Link<T, H> newLink = new Link<T, H>(t, h); if (isEmpty()) { first = newLink; } last.next = newLink; last = newLink;}/*** 删除头部元素* * @return 元素* */public Link<T, H> deleteFirst() { Link<T, H> temp = first; first = first.next; return temp;}/*** 删除指定元素(只删除查找到的第一个元素)* * @param t* 键* * @return 删除的元素* */public Link<T, H> delete(T t) { if (first != null) { Link<T, H> current = first; Link<T, H> previous = first; while (current.getT() != t) { 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; } else { return null; }}/*** 删除指定元素(删除查找到的所有元素)* * @param t* 键* * @return 删除的元素* */public LinkedList<T, H> deleteList(T t) { if (first != null) { Link<T, H> current = first; Link<T, H> previous = first; LinkedList<T, H> list = new LinkedList<T, H>(); while (current != null) { if (current.getT() == t) { if (current == first) { first = first.next; } else { previous.next = current.next; } list.insertFirst(current.getT(), current.getH()); } else { previous = current; } current = current.next; } return list; } else { return null; }}/*** 查找元素* * @param t* 键* * @return 元素* */public Link<T, H> find(T t) { if (first != null) { Link<T, H> current = first; while (current.getT() != t) { if (current.next == null) { return null; } else { current = current.next; } } return current; } else { return null; }}/*** 判断链表是否为空* * @return 结果* */public boolean isEmpty() { return (first == null);}/** 遍历链表 */public void displayList() { System.out.print("List (first-->last):"); Link<T, H> current = first; while (current != null) { current.displayLink(); current = current.next; } System.out.println();}// public static void main(String[] args) {// LinkList<Integer, String> list = new LinkList<Integer, String>();// list.insertFirst(1, "这是第1条");// list.insertFirst(2, "这是第2条");// list.insertFirst(3, "这是第3条");// list.insertFirst(4, "这是第4条");// list.insertFirst(5, "这是第5条");// list.insertFirst(6, "这是第6条");// list.insertFirst(7, "这是第7条");// list.insertFirst(7, "这是第AAAA条");// list.insertFirst(7, "这是第BBBB条");// list.insertFirst(7, "这是第CCCC条");// list.insertFirst(7, "这是第DDDD条");// list.insertFirst(7, "这是第EEEE条");// list.insertFirst(8, "这是第8条");// list.insertFirst(9, "这是第9条");// list.displayList();// list.find(1).displayLink();// System.out.println();// list.deleteList(7).displayList();// System.out.println();// list.displayList();// }}package link;/** 链表对象 */public class Link<T, H> {// 键private T t;// 值private H h;// 前链结点public Link<T, H> previous = null;// 后链结点public Link<T, H> next = null;/** 构造器 初始化'键','值' */public Link(T t, H h) { this.t = t; this.h = h;}/** 输出该对象 */public void displayLink() { System.out.print("{" + t + " , " + h + "}");}public T getT() { return t;}public void setT(T t) { this.t = t;}public H getH() { return h;}public void setH(H h) { this.h = h;}}很长时间以前写的了 从BLOG弄下来 不知道正确不正确 LZ先运行下 删除链表需要做的操作还是需要些代码的 尤其是双向链表 偷偷的告诉你:书上的例子 我只是让他更像是JDK1.5的代码而已.. 用LinkedList 不就完了? LinkedList l = new LinkedList(); l.add(1); l.add(3); l.add(2); System.out.println(l); l.remove(0); System.out.println(l); 更改默认的打印机后,为何不起作用 构造函数和普通函数有什么不同 急!!用java监听串口,但不占用串口 初级问题-在线 急问UML类图的问题!!!!! 初学者问(问题很简单,这分很容易得) ████████什么是集群,集群又有什么好处?什么情况下在需要配置集群 jsdk1.4下的问题,简单问题。 请问Oracle8.1.7有专用的JDBC3.0驱动吗? 上海的朋友进来看看!!(400分) 请教有关ant的lib路径 急!!!FileOutputStream,OutputStreamWriter, BufferedWriter为什么连用?
Link previous;设置一个当前指针,一个前一个指针。
找到后,把前一个指针连上当前指针的next。
即找到元素后
previous.next = current.next;
回答的不全面
package link;/** 链表(双端) */public class LinkedList<T, H> {// 链表头部
private Link<T, H> first;// 链表尾部
private Link<T, H> last;/** 构造器 初始化链表前尾部 */
public LinkedList() {
first = null;
last = null;
}/**
* 添加元素到头部
*
* @param t
* 键
* @param h
* 值
* */
public void insertFirst(T t, H h) {
Link<T, H> newLink = new Link<T, H>(t, h);
if (isEmpty()) {
last = newLink;
}
newLink.next = first;
first = newLink;
}/**
* 添加元素到尾部
*
* @param t
* 键
* @param h
* 值
* */
public void insertLast(T t, H h) {
Link<T, H> newLink = new Link<T, H>(t, h);
if (isEmpty()) {
first = newLink;
}
last.next = newLink;
last = newLink;
}/**
* 删除头部元素
*
* @return 元素
* */
public Link<T, H> deleteFirst() {
Link<T, H> temp = first;
first = first.next;
return temp;
}/**
* 删除指定元素(只删除查找到的第一个元素)
*
* @param t
* 键
*
* @return 删除的元素
* */
public Link<T, H> delete(T t) {
if (first != null) {
Link<T, H> current = first;
Link<T, H> previous = first;
while (current.getT() != t) {
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;
} else {
return null;
}
}/**
* 删除指定元素(删除查找到的所有元素)
*
* @param t
* 键
*
* @return 删除的元素
* */
public LinkedList<T, H> deleteList(T t) {
if (first != null) {
Link<T, H> current = first;
Link<T, H> previous = first;
LinkedList<T, H> list = new LinkedList<T, H>();
while (current != null) {
if (current.getT() == t) {
if (current == first) {
first = first.next;
} else {
previous.next = current.next;
}
list.insertFirst(current.getT(), current.getH());
} else {
previous = current;
}
current = current.next;
}
return list;
} else {
return null;
}
}/**
* 查找元素
*
* @param t
* 键
*
* @return 元素
* */
public Link<T, H> find(T t) {
if (first != null) {
Link<T, H> current = first;
while (current.getT() != t) {
if (current.next == null) {
return null;
} else {
current = current.next;
}
}
return current;
} else {
return null;
}
}/**
* 判断链表是否为空
*
* @return 结果
* */
public boolean isEmpty() {
return (first == null);
}/** 遍历链表 */
public void displayList() {
System.out.print("List (first-->last):");
Link<T, H> current = first;
while (current != null) {
current.displayLink();
current = current.next;
}
System.out.println();
}// public static void main(String[] args) {
// LinkList<Integer, String> list = new LinkList<Integer, String>();
// list.insertFirst(1, "这是第1条");
// list.insertFirst(2, "这是第2条");
// list.insertFirst(3, "这是第3条");
// list.insertFirst(4, "这是第4条");
// list.insertFirst(5, "这是第5条");
// list.insertFirst(6, "这是第6条");
// list.insertFirst(7, "这是第7条");
// list.insertFirst(7, "这是第AAAA条");
// list.insertFirst(7, "这是第BBBB条");
// list.insertFirst(7, "这是第CCCC条");
// list.insertFirst(7, "这是第DDDD条");
// list.insertFirst(7, "这是第EEEE条");
// list.insertFirst(8, "这是第8条");
// list.insertFirst(9, "这是第9条");
// list.displayList();
// list.find(1).displayLink();
// System.out.println();
// list.deleteList(7).displayList();
// System.out.println();
// list.displayList();
// }
}
package link;/** 链表对象 */
public class Link<T, H> {// 键
private T t;// 值
private H h;// 前链结点
public Link<T, H> previous = null;// 后链结点
public Link<T, H> next = null;/** 构造器 初始化'键','值' */
public Link(T t, H h) {
this.t = t;
this.h = h;
}/** 输出该对象 */
public void displayLink() {
System.out.print("{" + t + " , " + h + "}");
}public T getT() {
return t;
}public void setT(T t) {
this.t = t;
}public H getH() {
return h;
}public void setH(H h) {
this.h = h;
}
}
很长时间以前写的了 从BLOG弄下来 不知道正确不正确 LZ先运行下 删除链表需要做的操作还是需要些代码的 尤其是双向链表
偷偷的告诉你:书上的例子 我只是让他更像是JDK1.5的代码而已..
LinkedList l = new LinkedList();
l.add(1);
l.add(3);
l.add(2);
System.out.println(l);
l.remove(0);
System.out.println(l);