java中链表删除元素_从链表java中删除元素

HI :)我有一个关于链接列表的程序,我们应该能够删除两个数字,如果它们是相同的…我知道如何从一开始就这样做但如何删除两个数字如果他们在链表的中间??所有3个一起运行

继承我的数字计划

import java.util.Scanner;

public class Numbers {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner reader = new Scanner (System.in);

LinkedList link=new LinkedList();

LinkedList link2= new LinkedList();

System.out.println("Enter in 5 numbers to put in your list");

int num1, num2, num3, num4, num5;

num1 = reader.nextInt();

link.addToStart(num1);

num2 = reader.nextInt();

link.addToStart(num2);

num3 = reader.nextInt();

link.addToStart(num3);

num4 = reader.nextInt();

link.addToStart(num4);

num5 = reader.nextInt();

link.addToStart(num5);

link2.addToStart(num5);

link2.addToStart(num4);

link2.addToStart(num3);

link2.addToStart(num2);

link2.addToStart(num1);

System.out.println("The size of the linked list is " + link.size());

System.out.print("Here is the list ");

link2.outputList();

System.out.println();

System.out.print("Here is the list in reverse order ");

link.outputList( );

System.out.println();

if (num1==num2){

link2.deleteHeadNode(num1);

link2.deleteHeadNode(num2);

System.out.println("Here is the list with the removed numbers");

link2.outputList();

System.out.println();

System.out.println("Here is its size");

System.out.println(link2.size());

}

else if (num2==num3){

link2.deleteHeadNode(num2);

link2.deleteHeadNode(num3);

System.out.println("Here is the list with the removed numbers");

link2.outputList();

System.out.println();

System.out.println("Here is its size");

System.out.println(link2.size());

}

}

}

这是节点程序

public class Node1

{

private Object item;

private int count;

private Node1 link;

public Node1( )

{

link = null;

item = null;

count = 0;

}

public Node1(int num, int newCount, Node1 linkValue)

{

setData(num, newCount);

link = linkValue;

}

public void setData(int num, int newCount)

{

item = num;

count = newCount;

}

public void setLink(Node1 newLink)

{

link = newLink;

}

public Object getItem( )

{

return item;

}

public int getCount( )

{

return count;

}

public Node1 getLink( )

{

return link;

}

}

这里是链接列表程序

public class LinkedList

{

private Node1 head;

public LinkedList( )

{

head = null;

}

/**

Adds a node at the start of the list with the specified data.

The added node will be the first node in the list.

*/

public void addToStart(int num)

{

head = new Node1(num, num, head);

}

/**

Removes the head node and returns true if the list contains at least

one node. Returns false if the list is empty.

* @param num1

*/

public boolean deleteHeadNode(int num1 )

{

if (head != null)

{

head = head.getLink( );

return true;

}

else

return false;

}

/**

Returns the number of nodes in the list.

*/

public int size( )

{

int count = 0;

Node1 position = head;

while (position != null)

{

count++;

position = position.getLink( );

}

return count;

}

public boolean contains(String item)

{

return (find(item) != null);

}

/**

Finds the first node containing the target item, and returns a

reference to that node. If target is not in the list, null is returned.

*/

private Node1 find(String target)

{

Node1 position = head;

Object itemAtPosition;

while (position != null)

{

itemAtPosition = position.getItem( );

if (itemAtPosition.equals(target))

return position;

position = position.getLink( );

}

return null; //target was not found

}

public void outputList( )

{

Node1 position = head;

while (position != null)

{

System.out.print(position.getItem( ) + " ");

position = position.getLink( );

}

}

public boolean isEmpty( )

{

return (head == null);

}

public void clear( )

{

head = null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值