java - 从列表中删除节点

标签 java linked-list nodes

我的问题:我的删除节点方法可以很好地从用户创建的列表中删除除第一个元素之外的任何指定节点。我如何让这个方法能够删除列表的前面?

public void deleteNode(node spot, node front) {
    node current = spot, previous = front;

    while(previous.next != current) {
        previous = previous.next;
    }
    previous.next = current.next;
}

这是完整的程序代码。

import java.io.*;

public class LinkedList {
public int num;
public node front;

//set front to null
public void init() {
    front = null;
}

//make a new node
public node makeNode(int num) {
    node newNode = new node();
    newNode.data = num;
    newNode.next = null;
    return newNode;
}

//find the end of a list
public node findTail(node front) {
    node current = front;

    while(current.next != null) {
        current = current.next;
    }
    return current;
}

//find a specified node
public node findSpot(node front, int num) {
    node current = front;
    boolean searching = true, found = false;

    while((searching)&&(!found)) {
        if(current == null) {
            searching = false;
        }
        else if(current.data == num) {
            found = true;
        }
        else {
            current = current.next;
        }
    }
    return current;
}

//delete a specified node
public void deleteNode(node spot, node front) {
    node current = spot, previous = front;

    while(previous.next != current) {
        previous = previous.next;
    }
    previous.next = current.next;
}

//add nodes to the end of a list
public void add2Back(node front, int num) {
    node tail;

    if (front == null) {
        front = makeNode(num);
    }
    else {
        tail = findTail(front);
        tail.next = makeNode(num);
    }
}

//add nodes after a specified node
public void addAfter(int num, node spot) {
    node newNode;
    newNode = makeNode(num);
    newNode.next = spot.next;
    spot.next = newNode;
}

//print out a list
public void showList(node front) {
    node current = front;

    while(current != null){
        System.out.println(current.data);
        current = current.next;
    }
}

public static void main(String [] args) throws IOException{
    //make a new list and node
    LinkedList newList = new LinkedList();
    node newNode = new node();
    //add data to the nodes in the list
    for(int j = 1; j < 10; j++){
        newList.add2Back(newNode, j);
    }
    //print out the list of nodes
    System.out.println("Auto-generated node list");
    newList.showList(newNode);

    //ask the user how many nodes to make, make those nodes, and show them
    System.out.println("Please enter how many nodes you would like made.");
    BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)) ;
    String inputData = inputReader.readLine();
    int listLength = Integer.parseInt(inputData);
    LinkedList userList = new LinkedList();
    node userNode = new node();
    for(int j = 1; j < listLength; j++) {
        userList.add2Back(userNode, j);
    }
    userList.showList(userNode);

    //ask the user to add a new node to the list after a specified node
    System.out.println("Please enter a number for a node and then choose a spot from the list to add after.");
    BufferedReader inputReader2 = new BufferedReader(new InputStreamReader(System.in)) ;
    String inputData2 = inputReader2.readLine();
    BufferedReader inputReader3 = new BufferedReader(new InputStreamReader(System.in)) ;
    String inputData3 = inputReader3.readLine();
    int newNodeValue = Integer.parseInt(inputData2);
    int nodeInList = Integer.parseInt(inputData3);
    userList.addAfter(newNodeValue, userList.findSpot(userNode, nodeInList));
    userList.showList(userNode);

    //ask the user to delete a specified node
    System.out.println("Please enter a node to delete.");
    BufferedReader inputReader4 = new BufferedReader(new InputStreamReader(System.in)) ;
    String inputData4 = inputReader4.readLine();
    int nodeToDelete = Integer.parseInt(inputData4);
    userList.deleteNode(userList.findSpot(userNode, nodeToDelete), userNode);
    userList.showList(userNode);
}
}

最佳答案

问题是你的deleteNode没有修改你列表的front成员变量,因为deleteNode里面的front变量是方法参数,不是实例变量front

这是你需要做的:

  • front 公开为LinkedList 的公共(public)成员违反了封装。使 front 成为私有(private)变量。
  • 从接受它的所有方法中删除参数 front;使用私有(private)成员 front 代替。
  • deleteNode中添加一个检查,以查看要删除的点是否在前面。如果是,则执行一个特殊的操作,为front赋一个新值,然后退出;否则,执行您已有的 while 循环。

关于java - 从列表中删除节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12922906/

相关文章:

java - Swing 对话框布局 : run method or constructor?

c++ - 二叉树形成错误

java - Activity 对象中两列组合的唯一约束

java - 经过多少次迭代后,SecureRandom 将生成给定范围内的所有数字?

java - 如何以最有效的方式获得以下输出?

arrays - 链表与数组遍历效率

c - 在单链表的开头插入节点

c++ - 需要帮助从链表 c++ 中删除节点

C 链表和节点程序,为什么我会收到未处理的异常?

c - 找到节点后从链表中删除节点