java - FIFO queue with one pointer -


so hello , wrote fifo queue implementation in java 2 pointers (head,tail). know if there way create fifo circular queue uses head pointer.any suggestion appreciated.

my code following :

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 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; }  } 

i didn't test functions working.

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<?> = c.iterator();         while(it.hasnext()){             if(!contains(it.next())){                 return false;             }         }         return false;     }      @override     public boolean addall(collection<? extends t> c) {         iterator<? extends t> = c.iterator();         while(it.hasnext()){                         add(it.next());         }         return false;     }      @override     public boolean removeall(collection<?> c) {         iterator<?> = 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 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());             }         }      }  } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -