如何从链表中删除随机选择的元素?
How can I remove a randomly chosen element from a linked list?
我正在尝试对 class 进行赋值,其中我使用 String Bag class 的 remove 方法来 return 链表的所有元素,一个位于一次,然后从列表中删除该元素。我有一个开始,但我不知道该怎么做。有人可以帮忙吗?
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
}
}
如果要删除链表的所有元素,可以使用内置的clear() 方法。
如果您不想使用该方法,您可以将头节点设置为空。垃圾收集器会处理剩下的事情。
如果您想要一个一次删除一个东西的删除方法并且您不关心它删除了什么,我建议只删除您找到的第一个元素。如果是在链表中,你可以只将一个临时节点分配给头节点,将头节点重新分配给下一个节点,并且 return 临时节点。
看看这个 link:
link
还有一个完整的示例:(制作您自己的 Link 和列表)
(下面的示例是一个 Linked 列表,其中有(links)它的 link 是一个点,例如 A(50,3) .你可以把它变成任何你想要的...)
Link
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--;
}
对于单链表,其中size
是链表的当前大小,head
——头节点。
换句话说,您正在对所选元素执行类似的操作:
你的意思是这样的吗???
代码
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
我正在尝试对 class 进行赋值,其中我使用 String Bag class 的 remove 方法来 return 链表的所有元素,一个位于一次,然后从列表中删除该元素。我有一个开始,但我不知道该怎么做。有人可以帮忙吗?
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
}
}
如果要删除链表的所有元素,可以使用内置的clear() 方法。
如果您不想使用该方法,您可以将头节点设置为空。垃圾收集器会处理剩下的事情。
如果您想要一个一次删除一个东西的删除方法并且您不关心它删除了什么,我建议只删除您找到的第一个元素。如果是在链表中,你可以只将一个临时节点分配给头节点,将头节点重新分配给下一个节点,并且 return 临时节点。
看看这个 link: link
还有一个完整的示例:(制作您自己的 Link 和列表)
(下面的示例是一个 Linked 列表,其中有(links)它的 link 是一个点,例如 A(50,3) .你可以把它变成任何你想要的...)
Link
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--;
}
对于单链表,其中size
是链表的当前大小,head
——头节点。
换句话说,您正在对所选元素执行类似的操作:
你的意思是这样的吗???
代码
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