Java 如何从链表中删除随机选择的元素?
我试图为一个类做一个赋值,我使用stringbag类的remove方法返回链表的所有元素,一次一个,然后从列表中删除该元素。我有一个开始,但我不知道该怎么做。有人能帮忙吗Java 如何从链表中删除随机选择的元素?,java,linked-list,Java,Linked List,我试图为一个类做一个赋值,我使用stringbag类的remove方法返回链表的所有元素,一次一个,然后从列表中删除该元素。我有一个开始,但我不知道该怎么做。有人能帮忙吗 public String remove() { Random rand = new Random(); int randNum = rand.nextInt(numItems); //generate random number int count = 0;
public String remove()
{
Random rand = new Random();
int randNum = rand.nextInt(numItems);
//generate random number
int count = 0;
String get;
currNode = firstNode;
//temporary node to get String from
while(count < randNum)
{
currNode = currNode.getLink();
count++;
}
//randomly select node to get String from
get = currNode.getInfo();
numItems--;
if(numItems == 0)
{
firstNode = null;
}
//decrement the number of items in the bag and make the first node
//null when it reaches 0
return get;
}
如果要删除链表的所有元素,可以使用内置的clear()方法 如果不想使用该方法,可以将head节点设置为null。垃圾收集器将处理剩余的垃圾 如果你想要一个移除方法,一次移除一件东西,而你不在乎它移除了什么,我建议你只移除你找到的第一个元素。如果它位于链接列表中,则可以将临时节点指定给头部节点,将头部节点重新指定给下一个节点,然后返回临时节点 看看这个链接: 还有一个完整的示例:(创建自己的链接和列表) (下面的例子是一个有(链接)的链表,它的链接是一个点,例如a(50,3)。你可以把它转换成你想要的任何东西…) 链接
public class DoublePoint {
public double X;
public double Y;
public int LinkKey=0;
public DoublePoint nextLink; //keeps the nextLink
//Constructor
public DoublePoint(double Xpos,double Ypos,int key){
X=Xpos;
Y=Ypos;
LinkKey=key;
}
public void printLinkKey(){
System.out.println(LinkKey);
}
//Return Link key
public String returnLinkKey(){
return ""+LinkKey;
}
public void changeContent(double x,double y){
X=x;
Y=y;
}
public void ChangeLinkKey(int key){
LinkKey=key;
}
}
名单如下:
public class ListDoublePoints {
public DoublePoint first;
public int key;
public int totalLinks=0;
public ListDoublePoints(){
first=null;
key=0;
}
//Insert
public void insertLink(double x,double y){
DoublePoint newLink = new DoublePoint(x,y,key);
newLink.nextLink=first;
first=newLink;
key++;
totalLinks++;
}
//Find
public DoublePoint findLinkAt(int key){
DoublePoint current=first;
while(current.LinkKey!=key){
if(current.nextLink==null)
return null;
else
current=current.nextLink;
}
return current;
}
//Delete using Link key (similar with remove(int position) with ready java lists)
public String deleteLinkAt(int linkKey){
DoublePoint current =first;
DoublePoint previous=first;
while(current.LinkKey !=linkKey){
if(current.nextLink == null ){
return "boom";}
else
previous=current;
current=current.nextLink;
}
if(current==first)
first=first.nextLink;
else
previous.nextLink=current.nextLink;
--totalLinks;
return "ok";
}
//Return
public int LinksNumber(){
return totalLinks;
}
//Print
public void displayList(){
DoublePoint current=first;
while(current!=null){
current.displayLink();
current=current.nextLink;
}
}
public void displayTheNumberOfLinks(){
System.out.println(totalLinks);
}
}
*如果你想要像这样的东西,请告诉我
只需使用java就绪列表..*如果要按索引删除随机选择的元素,则它看起来如下所示:
public void removeRandomElement() {
int index = new Random().nextInt(size);
Node current = head;
Node prev = head;
for (int i = 0; i < index; i++) {
prev = current;
current = current.next;
}
prev.next = current.next;
current.next = null;
size--;
}
public void removeandomelement(){
int index=new Random().nextInt(大小);
节点电流=头;
节点prev=头部;
对于(int i=0;i
对于单链接列表,其中size
是列表的当前大小,head
-head节点
换句话说,您在所选元素上执行以下操作:
你是说像这样的事吗 代码
您需要按键删除链接(itme)还是什么?还有,你说的是简单链表或双链表这是接近我要找的。你能在“prev.next=current.next和current.next=null”行上进行删除吗?你的意思是我需要设置这些节点的链接吗?是的。我们做一些事情,比如(我不是一个好画家),我认为它需要一个特殊的情况,当随机指数是头?(我可能错了)
public void removeRandomElement() {
int index = new Random().nextInt(size);
Node current = head;
Node prev = head;
for (int i = 0; i < index; i++) {
prev = current;
current = current.next;
}
prev.next = current.next;
current.next = null;
size--;
}
private LinkedList<String> list = new LinkedList<>();
private void fillList() {
for (int i = 0; i < 10; i++) {
list.add("Hello " + i);
}
}
private void removeAllRandomly() {
Random random = new Random();
while (!list.isEmpty()) {
int randomPosition = random.nextInt(list.size());
String s = list.remove(randomPosition);
System.out.println(String.format("Item on position: %s (%s) was removed", randomPosition, s));
}
}
Item on position: 9 (Hello 9) was removed
Item on position: 1 (Hello 1) was removed
Item on position: 1 (Hello 2) was removed
Item on position: 2 (Hello 4) was removed
Item on position: 5 (Hello 8) was removed
Item on position: 0 (Hello 0) was removed
Item on position: 3 (Hello 7) was removed
Item on position: 1 (Hello 5) was removed
Item on position: 1 (Hello 6) was removed
Item on position: 0 (Hello 3) was removed