删除链接列表中的偶数节点并反向打印

时间:2016-03-15 08:35:39

标签: java list reverse

这种情况应遵循的逻辑是什么。

我有一个线性链表。我想从倒数第二个节点开始以相反的顺序打印列表..但同时删除偶数个节点。 例如:  34(节点1-奇数),65(节点2 - 偶数),733(节点3 - 奇数),34(节点4 - 偶数), 56(节点5 - 奇数),33(节点6 - 偶数)..

现在,我想要这样的输出.. 56(第二个最后一个节点),733(节点4被删除为偶数,它被打印,因为它是奇数),34(再次被删除,奇数以反向形式打印)..等等。

如果我没有澄清我的问题,请告诉我。并帮助我逻辑。 谢谢

更新

// Task A
public class LinkedList {

    protected class NodeReference {
        int info;
        NodeReference next;
        public NodeReference pLoc;
    }

    NodeReference headRef,pLoc, Loc; 
    int totalElements;
    private NodeReference next; 

    public LinkedList() {
        totalElements = 0;
        headRef = null;
    }

    public void insertAtFront(int value) {
        NodeReference newNode = new NodeReference();
                newNode.info = value;
                newNode.next = headRef;
                headRef = newNode;
    }
// Task B
    public void printList(){
        NodeReference location = headRef;
        if(headRef!=null) {
             System.out.println("The list is being printed...."); 
             while(location != null) {
             System.out.println(location.info);
             location = location.next; 
             }

        } // if block ends
        else
            System.out.println("Sorry, List is Empty!");
    }   
// Task C
    public NodeReference reverseList(NodeReference headRef) {
        if(headRef==null || headRef.next == null) 
            return headRef;

        NodeReference pLoc1 = headRef;
        NodeReference pLoc2 = headRef.next;

        headRef.next = null;
        while(pLoc1!= null || pLoc2!= null){
            NodeReference tempRef = pLoc2.next;
            pLoc2.next = pLoc1;
            pLoc1 = pLoc2;
            if (tempRef != null){
                pLoc2 = tempRef;
            }else{
                break;
            }
        }

        return pLoc2;
    }
    public void reverse() {
        reverseList(headRef);
    }
// Task D 
    public void deleteEven() {
        Loc = headRef; pLoc = null;
        while (Loc != null) {
            Loc = (Loc.next).next;
            System.out.println(Loc.info); 

        }
    }
}

驱动程序

public class LinkedListDriver {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LinkedList l = new LinkedList();
        l.insertAtFront(4); l.insertAtFront(7); 
        l.insertAtFront(3); l.insertAtFront(2);
        l.insertAtFront(8); l.insertAtFront(4);
        l.insertAtFront(6);
        l.printList(); 
        System.out.println("Printing Just Odd Values While Deleting Evens");
        try { 
            l.deleteEven();
        } catch(Exception e) {
            System.out.println(" End of the List..");

        }



    }

}

1 个答案:

答案 0 :(得分:1)

deleteEven方法几乎不错,但实际上你跳过了第一个元素。

另外,您声明那些仅​​在deleteEven方法中使用的属性,因此应该是局部变量,而不是类属性:

NodeReference headRef // Useless -> ,pLoc, Loc;

我重写了这样的deleteEven:

public void deleteEven() {
  NodeReference loc = headRef;
  while(loc != null && loc.next != null) {
    loc.next = loc.next.next;
    loc = loc.next;
  }
}

并且还改变了主要方法:

public static void main(String[] args) {
    LinkedList l = new LinkedList();
    l.insertAtFront(4);
    l.insertAtFront(7);
    l.insertAtFront(3);
    l.insertAtFront(2);
    l.insertAtFront(8);
    l.insertAtFront(4);
    l.insertAtFront(6);
    l.printList(); 
    System.out.println("Deleting Evens");
    l.deleteEven();
    l.printList();
}

输出:

The list is being printed....
6
4
8
2
3
7
4
Deleting Evens
The list is being printed....
6
8
3
4

现在,您必须实现reverseList方法。

提示:列表可以双链接(每个节点保留对上一个和下一个节点的引用)。然后,更容易保持对列表的最后一个元素的引用并从中迭代。