黑马程序员技术交流社区
标题:
自己实现的LinkedList中怎么删除一个指定节点
[打印本页]
作者:
adamjy
时间:
2014-4-7 13:12
标题:
自己实现的LinkedList中怎么删除一个指定节点
本帖最后由 adamjy 于 2014-4-8 21:12 编辑
如下代码是自己实现的LinkedList,有添加、删除(为实现)、显示元素功能,请问注释掉的"delete()"方法该怎么实现?
class Node{
Node in;
String name;
public Node(){
in = null;
}
public Node(String n){
in = null;
name = n;
}
public void setIn(Node n){
in = n;
}
public Node getIn(){
return in;
}
public void setName(String n){
name = n;
}
public String getName(){
return name;
}
}
public class Main {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
LinkList bi = new LinkList();
while(true){
System.out.println("Choose!\n[a] Insert Name\n[b] Delete\n[c] Show\n[d] Exit");
char c = scan.next().charAt(0);
System.out.println();
if(c == 'a'){
System.out.print("Enter Name: ");
bi.insert(scan.next());
System.out.println();
}
else if(c == 'b'){
System.out.print("Enter Name to delete: ");
// bi.delete(scan.next());
System.out.println();
}
else if(c == 'c'){
bi.show();
System.out.println();
}
else if(c == 'd'){
System.exit(0);
}
}
}
}
class LinkList{
private Node root;
public LinkList(){
root = null;
}
public void insert(String n){
root = insert(root, n);
}
private Node insert(Node n, String r){
if(n == null){
n = new Node(r);
}
else{
n.in = insert(n.in, r);
}
return n;
}
// public void delete(String n){
// root = delete(root, n);
// }
//
// private Node delete(Node n, String r){
//
// }
public void show(){
show(root);
}
private Node show(Node n){
if(n == null){
System.out.println("Empy list!");
}
else{
while(n!=null){
System.out.println(n.getName());
n = n.getIn();
}
}
return n;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2