简体   繁体   中英

Queue implementation with Generics in Java

trying to make an immutable queue and performing standard queue operations on it,using generics.the problem is i dont know much about generics :( please bear with me about the flow and structure of the code

so far i have

import java.lang.*;
import.java.util.*;
import java.util.NoSuchElementException;

public class ImmutableQueue<E>
{
  private final List<E> elmnts;

   public ImmutableQueue(List<E> elmnts) 
    {
     this.elmnts = elmnts;
     return elmnts.newInstance(); 
    }

  public ImmutableQueue() 
  {
    elmnts = new LinkedList<E>();
  }

 public ImmutableQueue<E> enqueue(E e)
 {
    if(e.equals("null"))
      throws IllegalArgumentException;

    List<E> copy = new LinkedList<E>(elmnts);
    copy.add(elmnts);
    copy.addlast(e);
    Iterator iterator = copy.iterator();
    while(iterator.hasNext())
     {
       Object prnt = itr.next();
       System.out.println(prnt+" ");
     }
   }

  public ImmutableQueue<E> dequeue()
  {
     if (elmnts.size() == 0) 
     {
      throw new NoSuchElementException();
     }

    List<E> copy = new LinkedList<E>(elmnts);
    copy.add(elmnts);
    copy.remove(0);

    Iterator iterator = copy.iterator();
     while(iterator.hasNext())
      {
       Object prnt = itr.next();
       System.out.println(prnt+" ");
      }
  } 

  public E peek()
  {
    String fsel=elmnts.get(0);
    System.out.println("First Element in the Queue is "+fsel);
    return null;
   }

  public int size()
  {
    int size=0;
    Iterator iterator = elmnts.iterator();
     while(iterator.hasNext())
     {
      size++; 
     }
    return size;
  }


     public static void main(String [] arg)
     {
        int a;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in)) ;

        ImmutableQueue<E> imqu=new LinkedList<E>();

        System.out.println("Create Queue/Add Elements to Queue ? ('N' or 'n' to stop)");
       <E> e=br.readLine();

       while(e!='n' || e!='N')
       {  
        imqu.insert(e);
       }

      System.out.println(":: Queue Operations ::");
      System.out.println("\n\n\n21.Enqueue\n2.Dequeue\n3.Peek\n4.size");

      a=Integer.parseInt(br.readLine());
      System.out.println("Choice"+a); 

    switch(a)
    {
     case 1: System.out.println("Element to be Enqueued : ");
             <E> el=br.readLine();
             imqu.enqueue(E el);
             break;

     case 2: imqu.dequeue();
             break;

     case 3: imqu.peek();
             break;

     case 4: int sz=imqu.size();
             System.out.println("Size of the queue is"+sz);
             break;

     default: System.out.println("Bad Choice");

    }
 }
}

thanks

I have done some change in the code that you have posted. This is how an immutable queue should look like. Immutable objects won't do any changes on it rather it will take a copy of the object, manipulates it and returns the same. String class in java is one such example.

import java.util.LinkedList;
import java.util.List;

public class ImmutableQueue<T> {

    private List<T> immuatableQ = null;

    public ImmutableQueue() {
        this.immuatableQ = new LinkedList<T>();
    }

    public ImmutableQueue(List<T> immutableQ) {
        this();
        if (immutableQ != null) {
            this.immuatableQ.addAll(immutableQ);
        }
    }

    public ImmutableQueue<T> enqueue(T newItem) {
        List<T> copyQ = new LinkedList<T>(this.immuatableQ);
        copyQ.add(newItem);

        return new ImmutableQueue<T>(copyQ);
    }

    public ImmutableQueue<T> dequeue() {
        List<T> copyQ = new LinkedList<T>(this.immuatableQ);
        copyQ.remove(0);

        return new ImmutableQueue<T>(copyQ);
    }

    public T peek() {
        return this.immuatableQ.get(0);
    }

    public int size() {
        return this.immuatableQ.size();
    }

    public boolean isEmpty() {
        return this.immuatableQ.size() == 0;
    }

}

Look at this example implementation of Queue using Generis in Java. java queue implemenation You can read about Java Generics here Java Generics

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM