搜索和删除循环链表中的值

时间:2016-11-18 18:00:28

标签: java

我正在尝试创建一个循环遍历循环链表的应用程序。在执行此操作时,它将使用另一个linked list索引值,并将使用这些值从循环链接列表中删除。

我已经设置了它应该通过linked list方法获取要从我的随机runRandomList()中删除的索引值。然后,它使用rotate()方法循环遍历循环链接列表并从中删除值。然后它会将删除的值添加到“deletedLinked list”。然后,控件应返回runRandomList()方法,它应该从rotate()方法中提供随机链接列表中的下一个值。 循环链接列表应该开始遍历它停止的位置。它应该跟踪它所在的计数和节点。当计数到达第一个节点时,它应该重置为0,因此它可以正确地跟踪它所在的索引。

不幸的是,这种情况并没有发生。最近几天我一直在尝试不同的事情,因为代码现在正好;它进入一个连续的循环。问题似乎出现在rotate method中。

这是rotate method代码。我的想法是计数器将推进,直到它与索引输入匹配。如果它到达第一个节点,计数器将重置为0,然后再次开始递增,直到达到索引值。

private void rotate(int x)       
{ 

while(counter <= x)
{
 if(p == names.first)
 {
 counter = 0;
 }
 p = p.next;
 counter++;
}

deleteList.add((String) p.value);
names.remove(x);

}

这是我的链表类:

public class List<T>{


/*
helper class, creates nodes
*/
public class Node {

T value;
Node next;

/*
Inner class constructors
*/

public Node(T value, Node next)
{
   this.value = value;
   this.next = next;

}

 private Node(T value)
{
   this.value = value;

}
}

 /*
Outer class constructor
*/

Node first;
Node last;

public int size()
{
    return size(first);
}

 private int size(Node list)
 {
    if(list == null)
        return 0;

    else if(list == last)
        return 1;

    else
    {
     int size = size(list.next) + 1;

     return size;
    }
 }


public void add(T value)
{
first = add(value, first);   
}


private Node add(T value, Node list)
{  
    if(list == null)
    {  
        last = new Node(value);
        return last;
    }

    else
        list.next = add(value, list.next);

        return list;
}

public void setCircularList()
{
    last.next = first;

}

public void show()
{
    Node e = first;
    while (e != null)
    {
    System.out.println(e.value);
    e = e.next;
    }
}

 @Override
public String toString()
{
  StringBuilder strBuilder = new StringBuilder();

  // Use p to walk down the linked list
  Node p = first;
  while (p != null)
  {
     strBuilder.append(p.value + "\n"); 
     p = p.next;
  }      
  return strBuilder.toString(); 
}

public boolean isEmpty()
{
    boolean result = isEmpty(first);
    return result;
}

private boolean isEmpty(Node first)
{
   return first == null;
}


public class RemovalResult
{
   Node node;     // The node removed from the list
   Node list;     // The list remaining after the removal
   RemovalResult(Node remNode, Node remList)
   {
     node = remNode;
     list = remList;
   }
}     

/**
   The remove method removes the element at an index.
   @param index The index of the element to remove. 
   @return The element removed.  
     @exception IndexOutOfBoundsException When index is 
              out of bounds.     
*/

public T remove(int index)
{    
   // Pass the job on to the recursive version
   RemovalResult  remRes = remove(index, first);     
   T element = remRes.node.value;  // Element to return
   first = remRes.list;                 // Remaining list
   return element;      
}  

/**
   The private remove method recursively removes 
   the node at the given index from a list.
   @param index The position of the node to remove.
   @param list The list from which to remove a node.
   @return The result of removing the node from the list.
   @exception IndexOutOfBoundsException When index is 
              out of bounds.  
  */
   private RemovalResult remove(int index, Node list)
   {
   if (index < 0 || index >= size())
   {  
       String message = String.valueOf(index);
       throw new IndexOutOfBoundsException(message);
   }

   if (index == 0)
   {
       // Remove the first node on list
       RemovalResult remRes;
       remRes = new RemovalResult(list, list.next);
       list.next = null;
       return remRes;           
   }  

   // Recursively remove the element at index-1 in the tail
   RemovalResult remRes;
   remRes = remove(index-1, list.next);

   // Replace the tail with the results and return
   // after modifying the list part of RemovalResult
   list.next = remRes.list;    
   remRes.list = list;
   return remRes;
}


}

这包含main()runRandomList()rotate()方法。

public class lottery {

private int suitors;
private List<String> names;
private List<Integer> random;
private List<String> deleteList = new List<>();
private int counter;
private Node p;




public lottery(int suitors, List<String> names, List<Integer> random)
{   
 this.suitors = suitors;
 this.names = names;
 this.random = random;
 p = names.first;
 }


 public void start()
 {  
 //Set names list to circular
  names.setCircularList();
  runRandomList(random);
}

public void runRandomList(List<Integer> random)
{
Node i = random.first;
while(i != null)
{
    rotate((int) i.value, counter, p);
    i = i.next; 
}

}

public List getDeleteList()
{
return deleteList;
}      

private void rotate(int x, int count, Node p)       
{ 
Node i = p; 
while(count <= x)
{
 if(i == names.first)
 {
    count = 0;
 }
    i = i.next;
    count++;
}

deleteList.add((String) i.value);
names.remove(x);
p = i;
counter = count;
}



public static void main(String[] args)
{
List<String> namesList = new List<>();
namesList.add("a");
namesList.add("b");
namesList.add("c");
namesList.add("d");
namesList.add("e");
namesList.add("f");

List<Integer> randomList = new List<>();
randomList.add(3);
randomList.add(1);
randomList.add(5);
randomList.add(4);
randomList.add(0);



lottery obj = new lottery(6, namesList, randomList);

obj.start();
System.out.println(obj.getDeleteList());




}
}

1 个答案:

答案 0 :(得分:0)

我怀疑这是旋转方法,这就是解决方案。

private void rotate(int x, int count)       
{ 
 while(count != x)
{
 p = p.next;
 count++;

 if(count == x)
 {
   deleteList.add((String)p.value);  
   counter = x;
 }
 if(count >= suitors)
 { 
     for (int j = 0; j < x ; j++)
     {
          p = p.next;
     }
        deleteList.add((String)p.value);
        counter = x; 
        count = x;   
 }                 
 }

 }