簡體   English   中英

如何在Java中更改單個鏈表隊列的順序?

[英]How to change the order of a single linkedlist queue in 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();

    }}

如何更改隊列中這些名稱的順序? 我試圖將交換節點的函數放在單鏈表類中,但它不起作用.im混淆了我應該在鏈隊列類或單鏈表類還是在主類中使該函數在哪一層進行。 是的,我只想像這樣簡單地交換隊列中的名稱。

更新的答案

我以一種更易於理解的方式修改了您的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)

希望能幫助到你,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM