[英]How can i randomly select and delete an element from an already existing linked list?
[英]How can I remove a randomly chosen element from a linked list?
我正在嘗試對一個類進行分配,在該類中,我使用String Bag類的remove方法一次返回一個鏈表的所有元素,然后從列表中刪除該元素。 我有一個開始,但我不知道該怎么做。 有人可以幫忙嗎?
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;
}
編輯:這是應用程序級別:
public class StringBagTest
{
public static void main(String[] args)
{
LLStringBag bag = new LLStringBag();
bag.insert("Hat");
bag.insert("Shirt");
bag.insert("Pants");
bag.insert("Shoes");
//insert 4 strings into the list
while(!bag.isEmpty())
{
System.out.println(bag.remove());
}
//randomly removes all contents of list
}
}
如果要按索引刪除隨機選擇的元素,則它看起來像這樣:
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--;
}
對於單鏈列表,其中size
是列表的當前大小,為head
-head節點。
如果要刪除鏈表的所有元素,則可以使用內置的clear()方法。
如果您不想使用該方法,則可以將頭節點設置為null。 垃圾收集器將負責其余的工作。
如果您想要一次一次刪除一件東西的remove方法,而不關心它刪除了什么,我建議您只刪除找到的第一個元素。 如果它在鏈接列表中,則只需將臨時節點分配給頭節點,將頭節點重新分配給下一個節點,然后返回臨時節點。
看一下這個鏈接: link
也是一個完整的例子:( 創建自己的鏈接和列表 )
(下面的示例是一個具有(鏈接)的鏈接列表,它的鏈接是一個點,例如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准備就緒列表一起工作.. *
你的意思是這樣的嗎???
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.