Java 从列表中删除节点
我的问题:我的delete node方法可以很好地从用户创建的列表中删除除第一个元素之外的任何指定节点。如何使此方法能够删除列表的前面Java 从列表中删除节点,java,linked-list,nodes,Java,Linked List,Nodes,我的问题:我的delete node方法可以很好地从用户创建的列表中删除除第一个元素之外的任何指定节点。如何使此方法能够删除列表的前面 public void deleteNode(node spot, node front) { node current = spot, previous = front; while(previous.next != current) { previous = previous.next; } previous
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);
}
}
import java.io.*;
公共类链接列表{
公共整数;
公共节点前端;
//将front设置为null
公共void init(){
front=null;
}
//创建一个新节点
公共节点makeNode(int num){
node newNode=新节点();
newNode.data=num;
newNode.next=null;
返回newNode;
}
//查找列表的结尾
公共节点findTail(节点前端){
节点电流=前端;
while(current.next!=null){
当前=当前。下一步;
}
回流;
}
//查找指定的节点
公共节点findSpot(节点前端,int num){
节点电流=前端;
布尔搜索=真,找到=假;
当((搜索)和(!查找)){
如果(当前==null){
搜索=假;
}
else if(current.data==num){
发现=真;
}
否则{
当前=当前。下一步;
}
}
回流;
}
//删除指定的节点
公共void deleteNode(节点点、节点前){
节点当前=点,上一个=前;
while(previous.next!=当前){
上一个=上一个。下一个;
}
previous.next=current.next;
}
//将节点添加到列表的末尾
公共void add2Back(节点前端,int num){
节尾;
if(front==null){
front=makeNode(num);
}
否则{
尾部=尾翼(前部);
tail.next=makeNode(num);
}
}
//在指定节点后添加节点
public void addAfter(int num,节点点){
节点newNode;
newNode=makeNode(num);
newNode.next=spot.next;
spot.next=newNode;
}
//打印一份清单
公共无效显示列表(节点前端){
节点电流=前端;
while(当前!=null){
系统输出打印项次(当前数据);
当前=当前。下一步;
}
}
公共静态void main(字符串[]args)引发IOException{
//创建一个新的列表和节点
LinkedList newList=新LinkedList();
node newNode=新节点();
//向列表中的节点添加数据
对于(int j=1;j<10;j++){
newList.add2Back(newNode,j);
}
//打印出节点列表
System.out.println(“自动生成的节点列表”);
newList.showList(newNode);
//询问用户要创建多少个节点,创建这些节点并显示它们
System.out.println(“请输入您希望创建的节点数。”);
BufferedReader inputReader=新的BufferedReader(新的InputStreamReader(System.in));
字符串inputData=inputReader.readLine();
int listLength=Integer.parseInt(inputData);
LinkedList userList=新建LinkedList();
node userNode=新节点();
对于(int j=1;j
您正开始从
前面进行检查。下一步
。因此,每次都会忽略front
本身。问题在于deleteNode
不会修改列表中的front
成员变量,因为deleteNode
中的front
变量是一个方法参数,而不是实例变量front
以下是您需要做的:
- 将
公开为front
的公共成员违反了封装。将LinkedList
设为私有变量front
- 从所有获取参数的方法中删除参数
;改为使用私人会员front
front
- 在
中添加一个签入,以查看要删除的位置是否在deleteNode
前面。如果是,执行一个特殊操作,为
分配一个新值,然后退出;否则,执行已有的front
循环while
只需将该值传递给
链接列表删除方法。。。。
链接列表删除方法
<?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()
}
if(previous==front)front=current.next-else-previous.next=current.nextn对类节点进行命名对于java程序员来说非常混乱。约定是:类型名称(原语除外)以大写字母开头。所以你的类应该被命名为Node
。几乎-但是设置front=null
和返回没有影响。您应该设置this.front=this.front.next
或类似的设置。非常感谢!您的建议使我的代码现在完全起作用了。
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);
}
?>
<?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()
}