2017-08-05 70 views
0
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类中还是在主类中使用此函数。是的,我只想交换队列中的名字就像那样简单。如何更改java中单个链表队列的顺序?

+4

请不要随便做一个代码转储。向我们展示代码中的逻辑部分失败并出现错误 –

+2

您的目标是什么?当然,你不换换换的缘故。 –

+0

你可以看http://www.geeksforgeeks.org/rearrange-a-given-linked-list-in-place/在这里你可以得到更好的主意。 – Akash

回答

0

修订ANSWER

我修改了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) 

希望它能帮助,

+0

你可以考虑我的技能水平在Java作为一个初学者,我需要一个简单的代码,我可以运行和理解。谢谢无论如何把你的答案。 –

+0

mySinglyLinkedList(i).getNext()。setNext(mySinglyLinkedList(i));我不明白这行代码 –

+0

这是一段非常好的代码,它也很简单,但它会解决一部分难题。我仍然需要将这个nodelinkedlist链接到一个队列接口,并且执行你在这个main中做的,但是使用队列函数{enqueue和dequeue} –