2012-10-16 146 views
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); 
} 
} 
+0

如果(以前==前)前= current.next否则previous.next = current.next –

+0

命名类'node'是非常令人困惑的Java程序员。约定是:类型名称(基本体除外)以大写字母开头。所以你的班级应该被命名为'Node'。 – amit

回答

1

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

以下是你需要做的:

  • 暴露frontLinkedList的公共成员是违反封装。使front是一个私有变量。
  • 从采取它的所有方法中删除参数front;改用私人会员front
  • 添加支票deleteNode以查看要删除的点是否为front。如果是,执行一项特殊操作,将front赋值为一个新值,然后退出;否则,请执行您已有的while循环。
+0

非常感谢!你的建议让我的代码现在完全工作。 – user1751234

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本身每次都被忽略。

+0

几乎 - 但是设置'front = null'并返回没有任何影响。你应该设置'this.front = this.front.next'或类似的东西。 – amit

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


} 

?> 
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() 
}