自定义列表类,如何循环使用hasNext方法?

时间:2017-06-29 10:30:41

标签: java eclipse list collections

大家好我是java的新手,所以我真的非常感谢任何帮助。 好的,这就是问题所在: 我有一个列表类和一个listNode类, 列表Class由名称,firstNode和lastNode表示。 firstNode和lastNode来自listNode类型, listNode由Object(例如data或Object o)和nextNode表示,nextNode指向列表中的下一个Node,它也来自listNode类型。

列表类:

public class List {

private ListNode firstNode;
private ListNode lastNode;
private String name;

public List() {
    this("list");
}

public List(String listName) {
    name = listName;
    firstNode = lastNode = null;
}

public void insertAtFront(Object insertItem) {
    if (isEmpty())
        firstNode = lastNode = new ListNode(insertItem);
    else
        firstNode = new ListNode(insertItem, firstNode);
}

public void insertAtBack(Object insertItem) {
    if (isEmpty())
        firstNode = lastNode = new ListNode(insertItem);
    else
        lastNode = lastNode.nextNode = new ListNode(insertItem);
}

public Object removeFromFront() throws EmptyListException {
    if (isEmpty())
        throw new EmptyListException(name);
    Object removedItem = firstNode.data;

    if (firstNode == lastNode)
        firstNode = lastNode = null;
    else
        firstNode = firstNode.nextNode;
    return removedItem;
}

public Object removeFromBack() throws EmptyListException {
    if (isEmpty())
        throw new EmptyListException(name);

    Object removedItem = lastNode.data;
    if (firstNode == lastNode)
        firstNode = lastNode = null;
    else {
        ListNode current = firstNode;

        while (current.nextNode != lastNode)
            current = current.nextNode;

        lastNode = current;
        current.nextNode = null;
    }
    return removedItem;
}

public boolean isEmpty() {
    return firstNode == null;
}

public void print() {
    if (isEmpty()) {
        System.out.printf("Empty %s\n", name);
        return;
    }
    System.out.printf("The %s is : ", name);
    ListNode current = firstNode;

    while (current != null) {
        System.out.printf("%s", current.data);
        current = current.nextNode;
    }
    System.out.println("\n");
}

@Override
public String toString() {
    String stk = "(";
    if(isEmpty())return "Empty List";
    ListNode checkNode = firstNode;
        while (checkNode != null) {
        stk += checkNode.data.toString()+ " , ";
        checkNode = checkNode.nextNode;
    }
    return stk+")";
}
public ListNode removeAt (int k){
    if(k<=0 || k>getLength())
        try{
            throw new IllegalValues();
        }catch(IllegalValues iv){
            iv.printStackTrace();
            return null;
        }
    ListNode newNode = firstNode;
    if(k==1){
        newNode = firstNode;
        firstNode = firstNode.nextNode;
        return  newNode;
    }
    if(k==2){
        newNode = firstNode.nextNode;
        firstNode.nextNode = firstNode.nextNode.nextNode;
        return newNode;
    }

    if(k==3){
        newNode = firstNode.nextNode;
        firstNode.nextNode.nextNode = firstNode.nextNode.nextNode.nextNode;
        return newNode;
    }

    if(k==4){
        newNode = firstNode.nextNode;
        firstNode.nextNode.nextNode.nextNode = firstNode.nextNode.nextNode.nextNode.nextNode;
        return newNode;
    }

    return newNode;
}
public int getLength(){
    ListNode checkNode = firstNode;
    int count =0;
    while (checkNode != null) {
    count++;
    checkNode = checkNode.nextNode;
}
    return count;
}

}

listNode class:

public class ListNode {

Object data;
ListNode nextNode;

public ListNode(Object o) {
    this(o, null);
}

public ListNode(Object o, ListNode node) {
    data = o;
    nextNode = node;
}

public Object getObject() {
    return data;
}

public ListNode getNext(){
    return nextNode;
}

}

所以这些是我正在使用的两个类。 我的问题在于removeAt()方法我不知道如何概括它并为所有代码做出一般的答案(比如for语句)我只能通过编写每个案例来使其工作在if语句中分开。我需要编写一个for循环,以某种方式循环抛出hasNext()方法。 有任何想法吗?? 提前谢谢

2 个答案:

答案 0 :(得分:0)

好吧,为了删除k第n个节点(当k> 1时),总会有一个节点应该执行:

someNode.nextNode = someNode.nextNode.nextNode;

这是您要删除的节点之前的节点,这使其成为k-1第3个节点。

您可以找到带有for循环的节点:

if (k==1) {
    ListNode removedNode = firstNode;
    firstNode = firstNode.nextNode;
    return removedNode;
}
ListNode someNode = firstNode;
for (int i = 1; i < k - 1; i++) {
    someNode = someNode.nextNode;
}
ListNode removedNode = someNode.nextNode;
someNode.nextNode = someNode.nextNode.nextNode;
return removedNode;

请注意,k == 1的情况是单独处理的。

答案 1 :(得分:0)

这是一个使用0作为第一个索引的变体:

public ListNode removeAt (int k)
{
  if (k < 0 || k >= getLength())
  {
    try
    {
        throw new IllegalValues();
    }
    catch(IllegalValues iv)
    {
        iv.printStackTrace();
        return null;
    }

    if (k == 0)
    {
      // first element should be removed
      removeFromFront();
    }
    else if (k == getLength() – 1)
    {
      // last element should be removed
      removeFromBack();
    }
    else
    {
      ListNode predecessor =  firstNode;
      for (int i = 0; i < k - 1; ++i)
      {
        predecessor = predecessor.nextNode;
      }

      ListNode removed = predecessor.nextNode;
      predecessor.nextNode = predecessor.nextNode.nextNode;
      return removed;
    }