2016-11-10 11 views
1

みなさん、こんにちは皆さんは、2つのポインタ(頭、尾)を持つjavaでfifoキューの実装を書いています。頭のポインタだけを使用するFIFOキューを作成する方法があるかどうかを知りたいと思います。1つのポインタを持つFIFOキュー

私のコードは次のとおりです:

import java.io.PrintStream; 
import java.util.* 

public class StringQueueImpl<T> implements StringQueue { 

private int total;   // number of elements on queue 
private Node head; // beginning of queue 
private Node tail;  // end of queue 

private class Node { 
    T ele; 
    Node next; 
    Node(T ele) { 
    this.ele = ele; 
    next = null; } 
} 
/** 
* Creates an empty queue. 
*/ 
public StringQueueImpl() { 
    first = null; 
    last = null; 
    total = 0; 
} 

boolean isEmpty() { 
    return (head == null); 
} 

public <T> void put(T ele) { 
    Node t = tail; 
    tail = new Node(ele); 
    if (isEmpty()) head = tail; 
    else t.next = tail; 
    total++; 
} 

public T get() { 
    if (isEmpty()) throw new NoSuchElementException(); 
    T v = head.ele; 
    Node t = head.next; 
    head = t; 
    return v; 
    total--; 
} 


public T peek() { 
    if (isEmpty()) throw new NoSuchElementException(); 
    return head.ele; 
} 

Node node = head; 
public void printQueue(PrintStream stream){ 
    while(node != null){ 
     stream.println(node.ele); 
     stream.flush(); 
     node = node.next; 
    } 
} 

public int size(){ 
    return total; 
} 

} 

答えて

0

私はすべてをテストしていないが、いくつかの機能が働いています。

import java.util.Collection; 
import java.util.Iterator; 
import java.util.NoSuchElementException; 
import java.util.Queue; 


public class CircularQueue<T> implements Queue<T> { 

    private Node head = null; 
    private Integer size = 0; 

    private class Node{ 
     private Node next; 
     private T data; 

     public Node() { 
     } 

     public Node(T data,Node next) { 
      this.data = data; 
      this.next = next; 
     } 

     public T getData() { 
      return data; 
     } 

     public void setData(T data) { 
      this.data = data; 
     } 

     public Node getNext() { 
      return next; 
     } 

     public void setNext(Node next) { 
      this.next = next; 
     } 
    } 


    @Override 
    public int size() { 
     return this.size; 
    } 

    @Override 
    public boolean isEmpty() { 
     if(null == head) 
      return true; 
     else 
      return false; 
    } 

    @Override 
    public boolean contains(Object o) { 
     Node temp = head; 
     int counter = 0 ; 
     while(counter < size){ 
      if(temp.data.equals(o)){ 
       return true; 
      } 
      temp = temp.next; 
      counter++; 
     } 
     return false; 
    } 

    @Override 
    public Iterator<T> iterator() { 
     throw new UnsupportedOperationException(); 
    } 

    @Override 
    public Object[] toArray() { 
     if(isEmpty()) 
      return null; 
     Object[] arr = new Object[size]; 
     Node temp = head; 
     int counter = 0 ; 
     while(counter < size){ 
      arr[counter] = temp.data; 
      counter++; 
      temp = temp.next; 
     } 
     return arr; 
    } 

    @Override 
    public <T> T[] toArray(T[] a) { 
     Node temp = head; 
     int counter = 0 ; 
     while(counter < size){ 
      a[counter] = (T) temp.data; 
      counter++; 
      temp = temp.next; 
     } 
     return a; 
    } 

    @Override 
    public boolean remove(Object o) { 
     if(isEmpty()) 
      return false; 

     if(size() == 1){ 
      size--; 
      if(head.data.equals(o)) 
       head = head.next; 
      return true; 
     } 


     Node temp = head; 
     int counter = 1; 
     while(counter < size){ 
      if(o.equals(temp.next.data)){ 
       temp = temp.next.next; 
       size--; 
       return true; 
      } 
      temp = temp.next; 
      counter ++; 
     } 

     return false; 
    } 

    @Override 
    public boolean containsAll(Collection<?> c) { 
     if(isEmpty()) 
      return false; 
     Iterator<?> it = c.iterator(); 
     while(it.hasNext()){ 
      if(!contains(it.next())){ 
       return false; 
      } 
     } 
     return false; 
    } 

    @Override 
    public boolean addAll(Collection<? extends T> c) { 
     Iterator<? extends T> it = c.iterator(); 
     while(it.hasNext()){    
      add(it.next()); 
     } 
     return false; 
    } 

    @Override 
    public boolean removeAll(Collection<?> c) { 
     Iterator<?> it = c.iterator(); 
     while(it.hasNext()){    
      remove(it.next()); 
     } 
     return false; 
    } 

    @Override 
    public boolean retainAll(Collection<?> c) { 
     throw new UnsupportedOperationException(); 
    } 

    @Override 
    public void clear() { 
     head = null; 
    } 

    @Override 
    public boolean add(T e) { 

     if(isEmpty()){ 
      head = new Node(); 
      head.next = head; 
      head.data = e; 
      size++; 
      return true; 
     } 

     if(size() == 1){ 
      Node temp = new Node(); 
      temp.data = e; 
      temp.next = head; 
      head.next = temp; 
      size++; 
      return true; 
     } 

     Node temp = head; 
     int counter = 0; 
     while(counter < size - 1){ 
      counter++; 
      temp = temp.next; 
     } 

     temp.next = new Node(e,head); 
     size++; 
     return true; 
    } 

    @Override 
    public boolean offer(T e) { 
     throw new UnsupportedOperationException(); 
    } 

    @Override 
    public T remove() { 
     if(isEmpty()) 
      return null; 

     Node temp = head; 
     int counter = 0; 
     while(counter < size){ 
      temp = temp.next; 
      counter++; 
     } 
     head = head.next; 
     temp.next = head; 

     return null; 
    } 

    @Override 
    public T poll() { 
     if(isEmpty()) 
      return null; 

     if(size() == 1){ 
      T data = head.data; 
      head = null; 
      size--; 
      return data; 
     } 
     T data = head.data; 

     Node temp = head; 
     int counter = 0; 
     while(counter < size - 1){ 
      counter++; 
      temp = temp.next; 
     } 
     head = head.next; 
     temp.next = head; 
     size--; 
     return data; 
    } 

    @Override 
    public T element() { 
     if(isEmpty()) 
      throw new NoSuchElementException("Queue is Empty!!!"); 
     T data = head.data; 
     head = head.next; 
     return data; 
    } 

    @Override 
    public T peek() { 
     return head.data; 
    } 

    public void print(){ 
     Node temp = head; 
     int counter = 0; 
     while(counter < size){ 
      counter++; 
      System.out.println(temp.data); 
      temp = temp.next; 
     } 

    } 

    public static void main(String[] args) { 
     CircularQueue<String> circularQueue = new CircularQueue<>(); 

     if(circularQueue.isEmpty()){ 
      circularQueue.add("Gurkan"); 
      circularQueue.print(); 
      circularQueue.add("Illeez"); 
      circularQueue.print(); 
      circularQueue.add("Test1"); 
      circularQueue.print(); 
      circularQueue.add("Test2"); 
      circularQueue.print(); 
      while(!circularQueue.isEmpty()) 
      { 
       System.out.println(circularQueue.poll()); 
      } 
     } 

    } 

} 
関連する問題