2

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

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);
}
}
4

4 回答 4

1

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

这是您需要做的:

  • front作为公共成员公开是LinkedList违反封装的。创建front一个私有变量。
  • front从所有采用它的方法中删除参数;改用私有成员front
  • 添加签入deleteNode以查看要删除的位置是否为front. 如果是,则执行分配front新值的特殊操作,然后退出;否则,请执行while您已有的循环。
于 2012-10-16T20:39:25.123 回答
0
public void deleteNode(node spot, node front) {
    node current = spot, previous = front;
    if(front == spot) {
        front = null;
        return;
    }
    while(previous.next != current) {
        previous = previous.next;
    }
    previous.next = current.next;
    current = null;
}

您开始从front.next. 所以front它本身每次都被忽略。

于 2012-10-16T20:35:53.050 回答
0
Delete a node from linklist in PHP by just passing that value to
linklist delete method....

<?php

class ListNode
{

    public $data;

    public $next;

    function __construct($data)
    {
        $this->data = $data;
        $this->next = NULL;
    }

    function readNode()
    {
        return $this->data;
    }
}


class LinkList
{

    private $firstNode;

    private $lastNode;

    private $count;


    function __construct()
    {
        $this->firstNode = NULL;
        $this->lastNode = NULL;
        $this->count = 0;
    }




    //deleting a node from linklist $key is the value you want to delete
    public function deleteNode($key)
    {
        $current = $this->firstNode;
        $previous = $this->firstNode;

        while($current->data != $key)
        {
            if($current->next == NULL)
                return NULL;
            else
            {
                $previous = $current;
                $current = $current->next;
            }
        }

        if($current == $this->firstNode)
         {
              if($this->count == 1)
               {
                  $this->lastNode = $this->firstNode;
               }
               $this->firstNode = $this->firstNode->next;
        }
        else
        {
            if($this->lastNode == $current)
            {
                 $this->lastNode = $previous;
             }
            $previous->next = $current->next;
        }
        $this->count--;  
    }



}




    $obj = new LinkList();

    $obj->deleteNode($value);


}

?>
于 2012-10-18T12:32:02.027 回答
0

链表删除方法....

<?php

class ListNode
{

    public $data;

    public $next;

    function __construct($data)
    {
        $this->data = $data;
        $this->next = NULL;
    }

    function readNode()
    {
        return $this->data;
    }
}


class LinkList
{

    private $firstNode;

    private $lastNode;

    private $count;


    function __construct()
}
于 2015-02-08T20:28:35.543 回答