SHARE
TWEET

ql3

Shamel Oct 21st, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Chp. 30 Lab 3  
  2. // Lab 3 involves manipulating a queue data structure
  3.  
  4. // no data structures may be used except for stacks and queues
  5.  
  6. // Queue methods that you are allowed to use are listed below:
  7. // public boolean add (E x),  public E remove(),  public E peek(),  public boolean isEmpty()
  8.        
  9. // Stack methods that you are allowed to use are listed below:
  10. // public boolean push (E x),  public E pop(),  public E peek(),  public boolean isEmpty()
  11.  
  12.  
  13. import java.util.*;
  14.  
  15. public class Chp30Lab3
  16. {
  17.     public static void main(String args[])
  18.     {
  19.         System.out.println("Java3016.java\n\n");
  20.         QueueManipulate <Integer> ourQueue = new QueueManipulate <Integer> ();
  21.        
  22.            
  23.         QueueManipulate <Integer> theirQueue = new QueueManipulate <Integer> (ourQueue);
  24.        
  25.     //  etc...
  26.        
  27.        
  28.         System.out.println("\n\n");
  29.     }
  30. }
  31.    
  32. class QueueManipulate<E>
  33. {
  34.    
  35.     private Queue <E> myQueue;
  36.      
  37.     // default constructor - instantiates myQueue, myQueue should be empty after this constructor call
  38.     public QueueManipulate()
  39.     {
  40.         myQueue=new LinkedList<E>();
  41.     }
  42.    
  43.    
  44.    
  45.     // overloaded constructor - recieves parameter queue(a queue of type E), instantiates myQueue, and
  46.     // copies all data items from parameter queue(front to back) into myQueue(front to back)
  47.     public QueueManipulate(Queue <E> queue)
  48.     {
  49.         Queue<E> temp=new LinkedList<E>();
  50.         while(!queue.isEmpty())
  51.         {
  52.             myQueue.add(queue.peek());
  53.             temp.add(queue.remove());
  54.         }
  55.         while(!temp.isEmpty())
  56.         {
  57.             queue.add(temp.remove());
  58.         }
  59.        
  60.     }
  61.      
  62.    
  63.    
  64.     // copy constructor - recieves parameter queueManipObj(a QueueManipulate object), instantiates myQueue(of the curent oject in scope), and
  65.     // copies all data items from myQueue(of parameter queueManipObj, front to back) into myQueue(of the curent oject in scope, front to back)  
  66.     // all data items in myQueue(of the parameter queueManipObj) should not be affected and remain in the same order
  67.     public QueueManipulate(QueueManipulate queueManipObj)
  68.     {
  69.         myQueue=new LinkedList<E>();
  70.         queueManipObj.clone(this);
  71.     }
  72.    
  73.    
  74.    
  75.     // displays all data items in myQueue, front to back (each item on a seperate line) while preserving the order of data items in myQueue
  76.     public void display()  
  77.     {
  78.             Queue<E> temp=new LinkedList<E>();
  79.    
  80.           while(!myQueue.isEmpty())
  81.           {
  82.             E temp2=myQueue.remove();
  83.             temp.add(temp2);
  84.           }
  85.          
  86.           while(!temp.isEmpty())
  87.           {
  88.             E temp2=temp.remove();
  89.             System.out.println(temp2);
  90.             myQueue.add(temp2);
  91.           }
  92.     }
  93.    
  94.    
  95.    
  96.     // receives parameter target(of type E), removes the first occurrence of target from myQueue and leaves the remaining data items in myQueue intact 
  97.     // returns true if target found, returns false if target not found
  98.     public boolean removeFirst(E target)
  99.     {
  100.         Queue<E> temp=new LinkedList<E>();
  101.         int k=0;
  102.         while(!myQueue.isEmpty())
  103.           {
  104.             if(target.equals(myQueue.peek()) && k==0)
  105.             {
  106.             k=1;
  107.             myQueue.remove();
  108.             }
  109.             else
  110.                 temp.add(myQueue.remove());
  111.           }
  112.         while(!temp.isEmpty())
  113.         {
  114.             myQueue.add(temp.remove());
  115.         }
  116.          
  117.         return k==1;
  118.     }
  119.    
  120.    
  121.    
  122.     // receives parameter target(of type E), removes all occurrences of target from myQueue and leaves the remaining data items in myQueue intact  
  123.     // returns true if target(s) found, returns false if target(s) not found
  124.     public boolean removeAll(E target)
  125.     {
  126.         Queue<E> temp=new LinkedList<E>();
  127.         int k=0;
  128.         while(!myQueue.isEmpty())
  129.           {
  130.             if(target.equals(myQueue.peek()) )
  131.             {
  132.             k=1;
  133.             myQueue.remove();
  134.             }
  135.             else
  136.                 temp.add(myQueue.remove());
  137.           }
  138.         while(!temp.isEmpty())
  139.         {
  140.             myQueue.add(temp.remove());
  141.         }
  142.          
  143.         return k==1;
  144.     }
  145.    
  146.    
  147.    
  148.     // reverses the the data items in myQueue (ie.  1 2 3 4   shold now be   4 3 2 1)  
  149.     public void reverse()
  150.     {
  151.    
  152.     }
  153.    
  154.        
  155.        
  156.     // recieves parameter queueManipObj(a QueueManipulate object), copies all data items from myQueue(of the curent oject in scope, front to back)
  157.     // into myQueue(of parameter queueManipObj, front to back)
  158.     // this is a clone method not an append method  ***  myQueue of parameter queueManipObj should be empty before you copy data items
  159.     // all data items in myQueue(of the curent oject in scope) should not be affected and remain in the same order
  160.     public void clone(QueueManipulate queueManipObj)
  161.     {
  162.    
  163.     }
  164.    
  165.    
  166.    
  167.     // recieves parameter queueManipObj(a QueueManipulate object), copies all data items from myQueue(of parameter queueManipObj, front to back)
  168.     // to the back of myQueue(of the curent oject in scope)
  169.     // this is an append method not a copy constructor or clone method  ***  do not remove any data items in myQueue of (of the curent oject in scope)  
  170.     // all data items in myQueue(of the parameter queueManipObj) should not be affected and remain in the same order
  171.     public void append(QueueManipulate queueManipObj)
  172.     {
  173.    
  174.     }
  175.    
  176.    
  177.    
  178.     // receives parameters target and newValue(both of type E), replaces ALL occurrences of target, contained in myQueue, with parameter newValue
  179.     // all other data items in myQueue should not be affected and remain in the same order
  180.     // return the amount of data items that were replaced
  181.     public int replace(E target, E newValue )
  182.     {
  183.        
  184.         return -999;
  185.     }
  186.    
  187.    
  188.    
  189.     // receives parameter target(of type E), searches myQueue for target, if target is found, return true and,
  190.     // move ALL occurrences of target to the front of myQueue (there may be one, more than one, or no occurrence of target)
  191.     // if target not found return false
  192.     // all other data items in myQueue should not be affected and remain in the same order         
  193.     public boolean moveToFront(E target)
  194.     {        
  195.        
  196.         return true;  
  197.     }
  198.        
  199.        
  200.        
  201.     // receives parameter target(of type E), searches myQueue for target, if target is found, return true and,
  202.     // move ALL occurrences of target to the back of myQueue (there may be one, more than one, or no occurrence of target)
  203.     // if target not found return false
  204.     // all other data items in myQueue should not be affected and remain in the same order         
  205.     public boolean moveToBack(E target)
  206.     {        
  207.        
  208.         return true;  
  209.     }  
  210.        
  211.          
  212.        
  213.     // receives parameters target and newValue(both of type E), searches myQueue for target, if target is found, return true and                                               
  214.     // insert the parameter newValue immediately BEFORE ALL occurrences of target(there may be one, more than one, or no occurrence of target)
  215.     // if target not found return false
  216.     // all other data items in myQueue should not be affected and remain in the same order         
  217.     public boolean insertItem(E target, E newValue)
  218.     {
  219.        
  220.         return true;  
  221.     }  
  222. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top