如何在java中更改单个链表队列的顺序?

时间:2017-08-05 15:54:02

标签: java

public class Node<E> {

    private E element;
    public Node<E> next;
 int data;

    Node(int d)
    {
        data = d;
        next = null;
    }
    public Node(E element, Node<E> next) {
        this.element = element;
        this.next = next;
    }

    public E getElement() {
        return element;
    }

    public Node<E> getNext() {
        return next;
    }

    public void setElement(E element) {
        this.element=element;
    }

    public void setNext(Node<E> n) {
        next = n;
    }
    public void displayNode(){
            System.out.print(element+ " ");
        }

}


 public class SinglyLinkedList<E> {

    private Node<E> head;
    private Node<E> tail;
    private int size;

    public SinglyLinkedList() {
        head = tail = null;
        size = 0;
    }

    public SinglyLinkedList(Node<E> head, Node<E> tail) {
        this.head = head;
        this.tail = tail;
    }

    public Node<E> getHead() {
        return head;
    }

    public Node<E> getTail() {
        return tail;
    }

    public void setHead(Node<E> head) {
        this.head = head;
    }

    public void setTail(Node<E> tail) {
        this.tail = tail;
    }

    public boolean isEmpty() {
        if (head == null) {
            return true;
        }

        return false;

    }

    public E first() {
        return head.getElement();
    }

    public E last() {
        return tail.getElement();
    }

    public void addFirst(E e) {
        if (head == null) {
            head = tail = new Node(e, null);

        } else {
            Node<E> newest = new Node(e, head);
            head = newest;
        }
        size++;
    }

    public void addLast(E e) {
        if (tail == null) {
            head = tail = new Node(e, null);

        } else {
            Node<E> newest = new Node(e, null);
            tail.setNext(newest);
            tail = newest;
        }
        size++;
    }

    public E removeFirst() {
        E e = head.getElement();
        head = head.getNext();
        size--;
        return e;
    }

    @Override
    public String toString() {
        Node<E> tmp = head;
        String s = "";
        while (tmp != null) {
            s += tmp.getElement();
            tmp=tmp.getNext();

        }
        return s;
    }


    public void displayList() {
        Node current = head;
        while (current != null) {
            current.displayNode();
            current = current.next;
        }
    }


}

    public interface Queue<E> {
    int size();
    boolean isEmpty();
    void enqueue( );
    E first();
    E dequeue();


}

public class LinkedQueue<E> implements Queue<E> {

    private SinglyLinkedList<E> list = new SinglyLinkedList<>();

    public LinkedQueue() {
    }

    public int size() {
        return list.size();
    }

    public boolean isEmpty() {
        return list.isEmpty();
    }

    public void enqueue(E element) {
        list.addLast(element);
    }

    public E first() {
        return list.first();
    }

    public E dequeue() {
        return list.removeFirst();
    }

    @Override
    public void enqueue() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools |list.addLast(element);
    }

    public void displayQueue() {
        list.displayList();
        System.out.println();
    }

public class Main {



    public static void main(String[] args) {

        LinkedQueue list = new LinkedQueue();

        list.enqueue(sam);
        list.enqueue(adams);
        list.enqueue(john);
        list.enqueue(isac);
         list.enqueue(gad);
         System.out.print("\n Linked list before calling swapNodes() ");
         list.displayQueue();

    }}

如何更改队列中这些名称的顺序? 我已经尝试将交换节点的函数放在singlylinkedlist类中,但它不起作用。我迷惑在哪个层应该在linkedqueue类或singlylinkedlist类或主类中创建此函数。是的,我只想简单地交换队列中的名称。

1 个答案:

答案 0 :(得分:0)

更新的答案

我以更容易理解的方式修改了您的NodeNodeList类。我还为这些类保留了类似的私有值和类似的方法。

public class JavaApplication287 {

    public static class Node{
        private Node node;
        private Node nextNode;
        int data;

        Node(int d){
            data = d;
            nextNode = null;
        }

        public Node getNode(){return node;}
        public void setNode(Node someNode){node = someNode;}

        public Node getNextNode(){return nextNode;}
        public void setNextNode(Node someNextNode){nextNode = someNextNode;}

        public int getData(){return data;}
        public void setData(int d){data = d;}

        public void printNode(){System.out.println(data);} 
    }

    public static class NodeLinkedList{
        private Node head;
        private Node tail;
        private int size;

        NodeLinkedList(Node nodeHead, Node nodeTail, int s){
            this.head = nodeHead;
            this.tail = nodeTail;
            this.size = s;
        }

        public Node getHead(){return head;}
        public void setHead(Node n){head = n;}

        public Node getTail(){return tail;}
        public void setTail(Node n){tail = n;}

        public int getSize(){return size;}
        public void setSize(int n){size = n;}

        public void printNodeList(){
            System.out.println("Head: " + head.getData());
            Node current = head;
            while (current.nextNode != null){
                System.out.println(current.data);
                current = current.getNextNode();
            }
            System.out.println("Tail: " + tail.getData());
        }
    }

    public static void main(String[] args) {

        // create Sample Nodes
        Node zero = new Node(0);
        Node one = new Node(1);
        Node two = new Node(2);
        Node three = new Node(3);
        Node four = new Node(4);
        Node five = new Node(5);

        //Link Them 
        zero.setNextNode(one);
        one.setNextNode(two);
        two.setNextNode(three);
        three.setNextNode(four);
        four.setNextNode(five);

        //Create the Linked Node List with head = one & tail = five
        NodeLinkedList myNodeLinkedList = new NodeLinkedList(zero, five, 6);

        //Print Current LinkedNodes
        myNodeLinkedList.printNodeList();

        //Invert the NodeLinkedList
        Node position = myNodeLinkedList.getHead(); //Node we look at

        Node prev = null; // Store the prev Node
        Node node = null; // Temp Node of the next Node in the Linked List

        for (int i=0; i< myNodeLinkedList.getSize(); i++){                      
            node = position.getNextNode(); //Store the Next Node so we do not lose access to it

            position.setNextNode(prev); // Update current Node's NextNode value 
            prev = position; // Set previous Node as the Node we are currently looking at
            position = node; // Move our position to the next Node
        }

        //Invert Head and Tail
        Node temp = myNodeLinkedList.getHead();
        myNodeLinkedList.setHead(myNodeLinkedList.getTail());
        myNodeLinkedList.setTail(temp);

        //Print Current LinkedNodes
        myNodeLinkedList.printNodeList();
    }
}

这是工作代码,这是我得到的输出,

run:
Head: 0
0
1
2
3
4
Tail: 5
Head: 5
5
4
3
2
1
Tail: 0
BUILD SUCCESSFUL (total time: 0 seconds)

希望它有所帮助,