Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Chp. 30 Lab 3
- // Lab 3 involves manipulating a queue data structure
- // no data structures may be used except for stacks and queues
- // Queue methods that you are allowed to use are listed below:
- // public boolean add (E x), public E remove(), public E peek(), public boolean isEmpty()
- // Stack methods that you are allowed to use are listed below:
- // public boolean push (E x), public E pop(), public E peek(), public boolean isEmpty()
- import java.util.*;
- public class Chp30Lab3
- {
- public static void main(String args[])
- {
- System.out.println("Java3016.java\n\n");
- QueueManipulate <Integer> ourQueue = new QueueManipulate <Integer> ();
- QueueManipulate <Integer> theirQueue = new QueueManipulate <Integer> (ourQueue);
- // etc...
- System.out.println("\n\n");
- }
- }
- class QueueManipulate<E>
- {
- private Queue <E> myQueue;
- // default constructor - instantiates myQueue, myQueue should be empty after this constructor call
- public QueueManipulate()
- {
- myQueue=new LinkedList<E>();
- }
- // overloaded constructor - recieves parameter queue(a queue of type E), instantiates myQueue, and
- // copies all data items from parameter queue(front to back) into myQueue(front to back)
- public QueueManipulate(Queue <E> queue)
- {
- Queue<E> temp=new LinkedList<E>();
- while(!queue.isEmpty())
- {
- myQueue.add(queue.peek());
- temp.add(queue.remove());
- }
- while(!temp.isEmpty())
- {
- queue.add(temp.remove());
- }
- }
- // copy constructor - recieves parameter queueManipObj(a QueueManipulate object), instantiates myQueue(of the curent oject in scope), and
- // copies all data items from myQueue(of parameter queueManipObj, front to back) into myQueue(of the curent oject in scope, front to back)
- // all data items in myQueue(of the parameter queueManipObj) should not be affected and remain in the same order
- public QueueManipulate(QueueManipulate queueManipObj)
- {
- myQueue=new LinkedList<E>();
- queueManipObj.clone(this);
- }
- // displays all data items in myQueue, front to back (each item on a seperate line) while preserving the order of data items in myQueue
- public void display()
- {
- Queue<E> temp=new LinkedList<E>();
- while(!myQueue.isEmpty())
- {
- E temp2=myQueue.remove();
- temp.add(temp2);
- }
- while(!temp.isEmpty())
- {
- E temp2=temp.remove();
- System.out.println(temp2);
- myQueue.add(temp2);
- }
- }
- // receives parameter target(of type E), removes the first occurrence of target from myQueue and leaves the remaining data items in myQueue intact
- // returns true if target found, returns false if target not found
- public boolean removeFirst(E target)
- {
- Queue<E> temp=new LinkedList<E>();
- int k=0;
- while(!myQueue.isEmpty())
- {
- if(target.equals(myQueue.peek()) && k==0)
- {
- k=1;
- myQueue.remove();
- }
- else
- temp.add(myQueue.remove());
- }
- while(!temp.isEmpty())
- {
- myQueue.add(temp.remove());
- }
- return k==1;
- }
- // receives parameter target(of type E), removes all occurrences of target from myQueue and leaves the remaining data items in myQueue intact
- // returns true if target(s) found, returns false if target(s) not found
- public boolean removeAll(E target)
- {
- Queue<E> temp=new LinkedList<E>();
- int k=0;
- while(!myQueue.isEmpty())
- {
- if(target.equals(myQueue.peek()) )
- {
- k=1;
- myQueue.remove();
- }
- else
- temp.add(myQueue.remove());
- }
- while(!temp.isEmpty())
- {
- myQueue.add(temp.remove());
- }
- return k==1;
- }
- // reverses the the data items in myQueue (ie. 1 2 3 4 shold now be 4 3 2 1)
- public void reverse()
- {
- }
- // recieves parameter queueManipObj(a QueueManipulate object), copies all data items from myQueue(of the curent oject in scope, front to back)
- // into myQueue(of parameter queueManipObj, front to back)
- // this is a clone method not an append method *** myQueue of parameter queueManipObj should be empty before you copy data items
- // all data items in myQueue(of the curent oject in scope) should not be affected and remain in the same order
- public void clone(QueueManipulate queueManipObj)
- {
- }
- // recieves parameter queueManipObj(a QueueManipulate object), copies all data items from myQueue(of parameter queueManipObj, front to back)
- // to the back of myQueue(of the curent oject in scope)
- // 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)
- // all data items in myQueue(of the parameter queueManipObj) should not be affected and remain in the same order
- public void append(QueueManipulate queueManipObj)
- {
- }
- // receives parameters target and newValue(both of type E), replaces ALL occurrences of target, contained in myQueue, with parameter newValue
- // all other data items in myQueue should not be affected and remain in the same order
- // return the amount of data items that were replaced
- public int replace(E target, E newValue )
- {
- return -999;
- }
- // receives parameter target(of type E), searches myQueue for target, if target is found, return true and,
- // move ALL occurrences of target to the front of myQueue (there may be one, more than one, or no occurrence of target)
- // if target not found return false
- // all other data items in myQueue should not be affected and remain in the same order
- public boolean moveToFront(E target)
- {
- return true;
- }
- // receives parameter target(of type E), searches myQueue for target, if target is found, return true and,
- // move ALL occurrences of target to the back of myQueue (there may be one, more than one, or no occurrence of target)
- // if target not found return false
- // all other data items in myQueue should not be affected and remain in the same order
- public boolean moveToBack(E target)
- {
- return true;
- }
- // receives parameters target and newValue(both of type E), searches myQueue for target, if target is found, return true and
- // insert the parameter newValue immediately BEFORE ALL occurrences of target(there may be one, more than one, or no occurrence of target)
- // if target not found return false
- // all other data items in myQueue should not be affected and remain in the same order
- public boolean insertItem(E target, E newValue)
- {
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement