473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

need help

28
I need the definitions of the method copyQueue, the default constructor, and the copy constructor folr the class LinkedQueueClass.
This is what i get so far


Expand|Select|Wrap|Line Numbers
  1. public abstract class DataElement
  2. {
  3.     public abstract boolean equals(DataElement otherElement);
  4.       //Method to determine whether two objects contain the
  5.       //same data.
  6.       //Postcondition: Returns true if this object contains the
  7.       //               same data as the object otherElement;
  8.       //               otherwise, it returns false.
  9.  
  10.     public abstract int compareTo(DataElement otherElement);
  11.       //Method to compare two objects.
  12.       //Postcondition: Returns a value < 0 if this object is
  13.       //                    less than the object otherElement;
  14.       //               Returns 0 if this object is the same as
  15.       //                    the object otherElement.
  16.       //               Returns a value > 0 if this object is
  17.       //                  greater than the object otherElement.
  18.  
  19.     public abstract void makeCopy(DataElement otherElement);
  20.       //Method to copy otherElement into this object.
  21.       //Postcondition: The data of otherElement is copied into
  22.       //               this object.
  23.  
  24.     public abstract DataElement getCopy();
  25.       //Method to return a copy of this object.
  26.       //Postcondition: A copy of this object is created and
  27.       //               a reference of the copy is returned.
  28. }
  29.  
  30.  
  31.  
  32. public class IntElement extends DataElement
  33. {
  34.     protected int num;
  35.  
  36.       //default constructor
  37.     public IntElement()
  38.     {
  39.         num = 0;
  40.     }
  41.  
  42.       //constructor with a parameter
  43.     public IntElement(int x)
  44.     {
  45.         num = x;
  46.     }
  47.       //copy constructor
  48.     public IntElement(IntElement otherElement)
  49.     {
  50.         num = otherElement.num;
  51.     }
  52.  
  53.       //Method to set the value of the instance variable num.
  54.       //Postcondition: num = x;
  55.     public void setNum(int x)
  56.     {
  57.         num = x;
  58.     }
  59.  
  60.       //Method to return the value of the instance variable num.
  61.       //Postcondition: The value of num is returned.
  62.     public int getNum()
  63.     {
  64.         return num;
  65.     }
  66.  
  67.     public boolean equals(DataElement otherElement)
  68.     {
  69.         IntElement temp = (IntElement) otherElement;
  70.         return (num == temp.num);
  71.     }
  72.  
  73.     public int compareTo(DataElement otherElement)
  74.     {
  75.         IntElement temp = (IntElement) otherElement;
  76.         return (num - temp.num);
  77.     }
  78.  
  79.     public void makeCopy(DataElement otherElement)
  80.     {
  81.         IntElement temp = (IntElement) otherElement;
  82.         num = temp.num;
  83.     }
  84.  
  85.     public DataElement getCopy()
  86.     {
  87.         IntElement temp = new IntElement(num);
  88.         return temp;
  89.     }
  90.  
  91.     public String toString()
  92.     {
  93.         return String.valueOf(num);
  94.     }
  95. }
  96.  
  97.  
  98.  
  99. public class QueueException extends RuntimeException
  100. {
  101.     public QueueException()
  102.     {
  103.     }
  104.  
  105.     public QueueException(String msg)
  106.     {
  107.         super(msg);
  108.     }
  109. }
  110.  
  111.  
  112.  
  113. public class QueueOverflowException extends QueueException
  114. {
  115.     public QueueOverflowException()
  116.     {
  117.          super("Queue Overflow");
  118.     }
  119.  
  120.     public QueueOverflowException(String msg)
  121.     {
  122.         super(msg);
  123.     }
  124. }
  125.  
  126.  
  127.  
  128. public class QueueUnderflowException extends QueueException
  129. {
  130.     public QueueUnderflowException()
  131.     {
  132.          super("Queue Underflow");
  133.     }
  134.  
  135.     public QueueUnderflowException(String msg)
  136.     {
  137.         super(msg);
  138.     }
  139. }
  140.  
  141.  
  142.  
  143. public class LinkedQueueClass
  144. {
  145.           //Definition of the node
  146.     protected class QueueNode
  147.     {
  148.         DataElement info;
  149.         QueueNode link;
  150.     }
  151.  
  152.     private QueueNode queueFront; //reference variable to the
  153.                                   //first element of the queue
  154.     private QueueNode queueRear;  //reference variable to the
  155.                                   //last element of the queue
  156.         //default constructor
  157.     public LinkedQueueClass()
  158.     {
  159.         queueFront = null;
  160.         queueRear  = null;
  161.     }
  162.  
  163.  
  164.         //copy constructor
  165.     public LinkedQueueClass(LinkedQueueClass otherQueue)
  166.     {
  167.         queueFront = otherQueue.queueFront;
  168.         queueRear  = otherQueue.queueRear;
  169.     }//end copy constructor
  170.  
  171.         //Method to initialize the queue to an empty state.
  172.         //Postcondition: queueFront = null; queueRear = null
  173.      public void initializeQueue()
  174.      {
  175.           queueFront = null;
  176.           queueRear = null;
  177.      }
  178.  
  179.         //Method to determine whether the queue is empty.
  180.         //Postcondition: Returns true if the queue is empty;
  181.         //               otherwise, returns false.
  182.      public boolean isEmptyQueue()
  183.      {
  184.           return (queueFront == null);
  185.      }
  186.  
  187.  
  188.         //Method to determine whether the queue is full.
  189.         //Postcondition: Returns true if the queue is full;
  190.         //               otherwise, returns false.
  191.      public boolean isFullQueue()
  192.      {
  193.           return false;
  194.      }
  195.  
  196.         //Method to return the first element of the queue.
  197.         //Precondition: The queue exists and is not empty.
  198.         //Postcondition: If the queue is empty, the method throws
  199.         //               QueueUnderflowException; otherwise, a
  200.         //               reference to a copy of the first element
  201.         //               of the queue is returned.
  202.      public DataElement front() throws QueueUnderflowException
  203.      {
  204.           if(isEmptyQueue())
  205.              throw new QueueUnderflowException();
  206.  
  207.           DataElement temp = queueFront.info.getCopy();
  208.           return temp;
  209.      }
  210.  
  211.         //Method to return the last element of the queue.
  212.         //Precondition: The queue exists and is not empty.
  213.         //Postcondition: If the queue is empty, the method throws
  214.         //               QueueUnderflowException; otherwise, a
  215.         //               reference to a copy of the last element
  216.         //               of the queue is returned.
  217.      public DataElement back() throws QueueUnderflowException
  218.      {
  219.           if(isEmptyQueue())
  220.              throw new QueueUnderflowException();
  221.  
  222.           DataElement temp = queueRear.info.getCopy();
  223.           return temp;
  224.      }
  225.  
  226.  
  227.         //Method to add queueElement to the queue.
  228.         //Precondition: The queue exists.
  229.         //Postcondition: The queue is changed and queueElement
  230.         //               is added to the queue.
  231.      public void addQueue(DataElement newElement)
  232.      {
  233.           QueueNode newNode;
  234.  
  235.           newNode = new QueueNode();  //create the node
  236.  
  237.           newNode.info = newElement.getCopy();  //store the info
  238.           newNode.link = null;   //initialize the link field to null
  239.  
  240.           if(queueFront == null) //if initially the queue is empty
  241.           {
  242.              queueFront = newNode;
  243.              queueRear = newNode;
  244.           }
  245.           else   //add newNode at the end
  246.           {
  247.              queueRear.link = newNode;
  248.              queueRear = queueRear.link;
  249.           }
  250.      }//end addQueue
  251.  
  252.  
  253.         //Method to remove the first element of the queue.
  254.         //Precondition: The queue exists and is not empty.
  255.         //Postcondition: The queue is changed and the first
  256.         //               element is removed from the queue.
  257.      public void deleteQueue() throws QueueUnderflowException
  258.      {
  259.           if(isEmptyQueue())
  260.              throw new QueueUnderflowException();
  261.  
  262.           queueFront = queueFront.link; //advance queueFront
  263.  
  264.           if(queueFront == null)  //if after deletion the queue is
  265.              queueRear = null;  //empty, set queueRear to null
  266.      } //end deleteQueue
  267.  
  268.  
  269.         //Method to make a copy of otherQueue.
  270.         //Postcondition: A copy of otherQueue is created and
  271.         //               assigned to this queue.
  272.     public void copyQueue(LinkedQueueClass otherQueue)
  273.     {
  274.       if (this != otherQueue)  //avoid self-copy
  275.           copyQueue(otherQueue);
  276.     }
  277. }
  278.  
  279.  
  280.  
  281. public class QueueProgram
  282. {
  283.     public static void main(String[] args)
  284.     {
  285.         LinkedQueueClass intQueue1 = new LinkedQueueClass();
  286.         LinkedQueueClass intQueue2 = new LinkedQueueClass();
  287.         LinkedQueueClass tempintQueue1 = new LinkedQueueClass();
  288.         LinkedQueueClass temmintQueue2 = new LinkedQueueClass();
  289.  
  290.  
  291.         intQueue1.addQueue(new IntElement(23));
  292.         intQueue1.addQueue(new IntElement(45));
  293.         intQueue1.addQueue(new IntElement(38));
  294.  
  295.         intQueue2.addQueue(new IntElement(32));
  296.         intQueue2.addQueue(new IntElement(54));
  297.         intQueue2.addQueue(new IntElement(83));
  298.  
  299.  
  300.         System.out.print("intQueue1 elements: ");
  301.  
  302.         while(!intQueue1.isEmptyQueue())
  303.         {
  304.             System.out.print(intQueue1.front() + " ");
  305.             intQueue1.deleteQueue();
  306.         }
  307.  
  308.         System.out.println();
  309.  
  310.         System.out.print("intQueue2 elements: ");
  311.  
  312.         while(!intQueue2.isEmptyQueue())
  313.         {
  314.             System.out.print(intQueue2.front() + " ");
  315.             intQueue2.deleteQueue();
  316.         }
  317.  
  318.         System.out.println();
  319.  
  320.         intQueue1.copyQueue(intQueue2);
  321.         System.out.print("After copying intQueue1 elements into intQueue2, intQueue2 elements are: ");
  322.  
  323.  
  324.  
  325.     }
  326. }
And i'm getting this error in my output:
----jGRASP exec: java QueueProgram

intQueue1 elements: 23 45 38
intQueue2 elements: 32 54 83
java.lang.StackOverflowError
Exception in thread "main"
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Nov 14 '06 #1
10 2058
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1.  
  2. abstract class DataElement
  3. {
  4.     public abstract boolean equals(DataElement otherElement);
  5.       //Method to determine whether two objects contain the
  6.       //same data.
  7.       //Postcondition: Returns true if this object contains the
  8.       //               same data as the object otherElement;
  9.       //               otherwise, it returns false.
  10.     public abstract int compareTo(DataElement otherElement);
  11.       //Method to compare two objects.
  12.       //Postcondition: Returns a value < 0 if this object is
  13.       //                    less than the object otherElement;
  14.       //               Returns 0 if this object is the same as
  15.       //                    the object otherElement.
  16.       //               Returns a value > 0 if this object is
  17.       //                  greater than the object otherElement.
  18.     public abstract void makeCopy(DataElement otherElement);
  19.       //Method to copy otherElement into this object.
  20.       //Postcondition: The data of otherElement is copied into
  21.       //               this object.
  22.     public abstract DataElement getCopy();
  23.       //Method to return a copy of this object.
  24.       //Postcondition: A copy of this object is created and
  25.       //               a reference of the copy is returned.
  26. }
  27.  
  28. class IntElement extends DataElement
  29. {
  30.     protected int num;
  31.       //default constructor
  32.     public IntElement()
  33.     {
  34.         num = 0;
  35.     }
  36.       //constructor with a parameter
  37.     public IntElement(int x)
  38.     {
  39.         num = x;
  40.     }
  41.       //copy constructor
  42.     public IntElement(IntElement otherElement)
  43.     {
  44.         num = otherElement.num;
  45.     }
  46.       //Method to set the value of the instance variable num.
  47.       //Postcondition: num = x;
  48.     public void setNum(int x)
  49.     {
  50.         num = x;
  51.     }
  52.       //Method to return the value of the instance variable num.
  53.       //Postcondition: The value of num is returned.
  54.     public int getNum()
  55.     {
  56.         return num;
  57.     }
  58.     public boolean equals(DataElement otherElement)
  59.     {
  60.         IntElement temp = (IntElement) otherElement;
  61.         return (num == temp.num);
  62.     }
  63.     public int compareTo(DataElement otherElement)
  64.     {
  65.         IntElement temp = (IntElement) otherElement;
  66.         return (num - temp.num);
  67.     }
  68.     public void makeCopy(DataElement otherElement)
  69.     {
  70.         IntElement temp = (IntElement) otherElement;
  71.         num = temp.num;
  72.     }
  73.     public DataElement getCopy()
  74.     {
  75.         IntElement temp = new IntElement(num);
  76.         return temp;
  77.     }
  78.     public String toString()
  79.     {
  80.         return String.valueOf(num);
  81.     }
  82. }
  83.  
  84. class QueueException extends RuntimeException
  85. {
  86.     public QueueException()
  87.     {
  88.     }
  89.     public QueueException(String msg)
  90.     {
  91.         super(msg);
  92.     }
  93. }
  94.  
  95. class QueueOverflowException extends QueueException
  96. {
  97.     public QueueOverflowException()
  98.     {
  99.          super("Queue Overflow");
  100.     }
  101.     public QueueOverflowException(String msg)
  102.     {
  103.         super(msg);
  104.     }
  105. }
  106.  
  107. class QueueUnderflowException extends QueueException
  108. {
  109.     public QueueUnderflowException()
  110.     {
  111.          super("Queue Underflow");
  112.     }
  113.     public QueueUnderflowException(String msg)
  114.     {
  115.         super(msg);
  116.     }
  117. }
  118.  
  119. class LinkedQueueClass
  120. {
  121.           //Definition of the node
  122.     protected class QueueNode
  123.     {
  124.         DataElement info;
  125.         QueueNode link;
  126.     }
  127.     private QueueNode queueFront; //reference variable to the
  128.                                   //first element of the queue
  129.     private QueueNode queueRear;  //reference variable to the
  130.                                   //last element of the queue
  131.         //default constructor
  132.     public LinkedQueueClass()
  133.     {
  134.         queueFront = null;
  135.         queueRear  = null;
  136.     }
  137.  
  138.         //copy constructor
  139.     public LinkedQueueClass(LinkedQueueClass otherQueue)
  140.     {
  141.         queueFront = otherQueue.queueFront;
  142.         queueRear  = otherQueue.queueRear;
  143.     }//end copy constructor
  144.         //Method to initialize the queue to an empty state.
  145.         //Postcondition: queueFront = null; queueRear = null
  146.      public void initializeQueue()
  147.      {
  148.           queueFront = null;
  149.           queueRear = null;
  150.      }
  151.         //Method to determine whether the queue is empty.
  152.         //Postcondition: Returns true if the queue is empty;
  153.         //               otherwise, returns false.
  154.      public boolean isEmptyQueue()
  155.      {
  156.           return (queueFront == null);
  157.      }
  158.  
  159.         //Method to determine whether the queue is full.
  160.         //Postcondition: Returns true if the queue is full;
  161.         //               otherwise, returns false.
  162.      public boolean isFullQueue()
  163.      {
  164.           return false;
  165.      }
  166.         //Method to return the first element of the queue.
  167.         //Precondition: The queue exists and is not empty.
  168.         //Postcondition: If the queue is empty, the method throws
  169.         //               QueueUnderflowException; otherwise, a
  170.         //               reference to a copy of the first element
  171.         //               of the queue is returned.
  172.      public DataElement front() throws QueueUnderflowException
  173.      {
  174.           if(isEmptyQueue())
  175.              throw new QueueUnderflowException();
  176.           DataElement temp = queueFront.info.getCopy();
  177.           return temp;
  178.      }
  179.         //Method to return the last element of the queue.
  180.         //Precondition: The queue exists and is not empty.
  181.         //Postcondition: If the queue is empty, the method throws
  182.         //               QueueUnderflowException; otherwise, a
  183.         //               reference to a copy of the last element
  184.         //               of the queue is returned.
  185.      public DataElement back() throws QueueUnderflowException
  186.      {
  187.           if(isEmptyQueue())
  188.              throw new QueueUnderflowException();
  189.           DataElement temp = queueRear.info.getCopy();
  190.           return temp;
  191.      }
  192.  
  193.         //Method to add queueElement to the queue.
  194.         //Precondition: The queue exists.
  195.         //Postcondition: The queue is changed and queueElement
  196.         //               is added to the queue.
  197.      public void addQueue(DataElement newElement)
  198.      {
  199.           QueueNode newNode;
  200.           newNode = new QueueNode();  //create the node
  201.           newNode.info = newElement.getCopy();  //store the info
  202.           newNode.link = null;   //initialize the link field to null
  203.           if(queueFront == null) //if initially the queue is empty
  204.           {
  205.              queueFront = newNode;
  206.              queueRear = newNode;
  207.           }
  208.           else   //add newNode at the end
  209.           {
  210.              queueRear.link = newNode;
  211.              queueRear = queueRear.link;
  212.           }
  213.      }//end addQueue
  214.  
  215.         //Method to remove the first element of the queue.
  216.         //Precondition: The queue exists and is not empty.
  217.         //Postcondition: The queue is changed and the first
  218.         //               element is removed from the queue.
  219.      public void deleteQueue() throws QueueUnderflowException
  220.      {
  221.           if(isEmptyQueue())
  222.              throw new QueueUnderflowException();
  223.           queueFront = queueFront.link; //advance queueFront
  224.           if(queueFront == null)  //if after deletion the queue is
  225.              queueRear = null;  //empty, set queueRear to null
  226.      } //end deleteQueue
  227.  
  228.         //Method to make a copy of otherQueue.
  229.         //Postcondition: A copy of otherQueue is created and
  230.         //               assigned to this queue.
  231.  
  232.     public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
  233.     {
  234.         LinkedQueueClass intQueue1 = new LinkedQueueClass();
  235.        while(!otherQueue.isEmptyQueue())
  236.   {
  237.       intQueue1.addQueue(otherQueue.front());
  238.       otherQueue.deleteQueue();
  239.         }
  240.     return intQueue1;
  241.  
  242.     }
  243. }
  244.  
  245. public class QueueProgram
  246. {
  247.     public static void main(String[] args)
  248.     {
  249.         LinkedQueueClass intQueue1 = new LinkedQueueClass();
  250.         LinkedQueueClass intQueue2 = new LinkedQueueClass();
  251.         LinkedQueueClass tempintQueue1 = new LinkedQueueClass();
  252.         LinkedQueueClass temmintQueue2 = new LinkedQueueClass();
  253.  
  254.         intQueue1.addQueue(new IntElement(23));
  255.         intQueue1.addQueue(new IntElement(45));
  256.         intQueue1.addQueue(new IntElement(38));
  257.         intQueue2.addQueue(new IntElement(32));
  258.         intQueue2.addQueue(new IntElement(54));
  259.         intQueue2.addQueue(new IntElement(83));
  260.  
  261.         System.out.print("intQueue1 elements: ");
  262.         while(!intQueue1.isEmptyQueue())
  263.         {
  264.             System.out.print(intQueue1.front() + " ");
  265.             intQueue1.deleteQueue();
  266.         }
  267.         System.out.println("Here");
  268.         System.out.print("intQueue2 elements: ");
  269.         while(!intQueue2.isEmptyQueue())
  270.         {
  271.             System.out.print(intQueue2.front() + " ");
  272.             intQueue2.deleteQueue();
  273.         }
  274.  
  275.   //intQueue2 is now empty
  276.   intQueue2.addQueue(new IntElement(32));
  277.   intQueue2.addQueue(new IntElement(54));
  278.         intQueue2.addQueue(new IntElement(83));
  279.  
  280.         intQueue1 = intQueue1.copyQueue(intQueue2);
  281.         System.out.print("After copying intQueue2 elements into  intQueue1, intQueue1 elements are: ");//other way round
  282.  
  283.   while(!intQueue1.isEmptyQueue())
  284.   {
  285.        System.out.print(intQueue1.front() + " ");
  286.        intQueue1.deleteQueue();
  287.         }
  288.  
  289.     }
  290. }
  291.  
  292.  
  293.  
Do the same for the copy constructor
Nov 14 '06 #2
satan
28
Expand|Select|Wrap|Line Numbers
  1.  
  2. abstract class DataElement
  3. {
  4.     public abstract boolean equals(DataElement otherElement);
  5.       //Method to determine whether two objects contain the
  6.       //same data.
  7.       //Postcondition: Returns true if this object contains the
  8.       //               same data as the object otherElement;
  9.       //               otherwise, it returns false.
  10.     public abstract int compareTo(DataElement otherElement);
  11.       //Method to compare two objects.
  12.       //Postcondition: Returns a value < 0 if this object is
  13.       //                    less than the object otherElement;
  14.       //               Returns 0 if this object is the same as
  15.       //                    the object otherElement.
  16.       //               Returns a value > 0 if this object is
  17.       //                  greater than the object otherElement.
  18.     public abstract void makeCopy(DataElement otherElement);
  19.       //Method to copy otherElement into this object.
  20.       //Postcondition: The data of otherElement is copied into
  21.       //               this object.
  22.     public abstract DataElement getCopy();
  23.       //Method to return a copy of this object.
  24.       //Postcondition: A copy of this object is created and
  25.       //               a reference of the copy is returned.
  26. }
  27.  
  28. class IntElement extends DataElement
  29. {
  30.     protected int num;
  31.       //default constructor
  32.     public IntElement()
  33.     {
  34.         num = 0;
  35.     }
  36.       //constructor with a parameter
  37.     public IntElement(int x)
  38.     {
  39.         num = x;
  40.     }
  41.       //copy constructor
  42.     public IntElement(IntElement otherElement)
  43.     {
  44.         num = otherElement.num;
  45.     }
  46.       //Method to set the value of the instance variable num.
  47.       //Postcondition: num = x;
  48.     public void setNum(int x)
  49.     {
  50.         num = x;
  51.     }
  52.       //Method to return the value of the instance variable num.
  53.       //Postcondition: The value of num is returned.
  54.     public int getNum()
  55.     {
  56.         return num;
  57.     }
  58.     public boolean equals(DataElement otherElement)
  59.     {
  60.         IntElement temp = (IntElement) otherElement;
  61.         return (num == temp.num);
  62.     }
  63.     public int compareTo(DataElement otherElement)
  64.     {
  65.         IntElement temp = (IntElement) otherElement;
  66.         return (num - temp.num);
  67.     }
  68.     public void makeCopy(DataElement otherElement)
  69.     {
  70.         IntElement temp = (IntElement) otherElement;
  71.         num = temp.num;
  72.     }
  73.     public DataElement getCopy()
  74.     {
  75.         IntElement temp = new IntElement(num);
  76.         return temp;
  77.     }
  78.     public String toString()
  79.     {
  80.         return String.valueOf(num);
  81.     }
  82. }
  83.  
  84. class QueueException extends RuntimeException
  85. {
  86.     public QueueException()
  87.     {
  88.     }
  89.     public QueueException(String msg)
  90.     {
  91.         super(msg);
  92.     }
  93. }
  94.  
  95. class QueueOverflowException extends QueueException
  96. {
  97.     public QueueOverflowException()
  98.     {
  99.          super("Queue Overflow");
  100.     }
  101.     public QueueOverflowException(String msg)
  102.     {
  103.         super(msg);
  104.     }
  105. }
  106.  
  107. class QueueUnderflowException extends QueueException
  108. {
  109.     public QueueUnderflowException()
  110.     {
  111.          super("Queue Underflow");
  112.     }
  113.     public QueueUnderflowException(String msg)
  114.     {
  115.         super(msg);
  116.     }
  117. }
  118.  
  119. class LinkedQueueClass
  120. {
  121.           //Definition of the node
  122.     protected class QueueNode
  123.     {
  124.         DataElement info;
  125.         QueueNode link;
  126.     }
  127.     private QueueNode queueFront; //reference variable to the
  128.                                   //first element of the queue
  129.     private QueueNode queueRear;  //reference variable to the
  130.                                   //last element of the queue
  131.         //default constructor
  132.     public LinkedQueueClass()
  133.     {
  134.         queueFront = null;
  135.         queueRear  = null;
  136.     }
  137.  
  138.         //copy constructor
  139.     public LinkedQueueClass(LinkedQueueClass otherQueue)
  140.     {
  141.         queueFront = otherQueue.queueFront;
  142.         queueRear  = otherQueue.queueRear;
  143.     }//end copy constructor
  144.         //Method to initialize the queue to an empty state.
  145.         //Postcondition: queueFront = null; queueRear = null
  146.      public void initializeQueue()
  147.      {
  148.           queueFront = null;
  149.           queueRear = null;
  150.      }
  151.         //Method to determine whether the queue is empty.
  152.         //Postcondition: Returns true if the queue is empty;
  153.         //               otherwise, returns false.
  154.      public boolean isEmptyQueue()
  155.      {
  156.           return (queueFront == null);
  157.      }
  158.  
  159.         //Method to determine whether the queue is full.
  160.         //Postcondition: Returns true if the queue is full;
  161.         //               otherwise, returns false.
  162.      public boolean isFullQueue()
  163.      {
  164.           return false;
  165.      }
  166.         //Method to return the first element of the queue.
  167.         //Precondition: The queue exists and is not empty.
  168.         //Postcondition: If the queue is empty, the method throws
  169.         //               QueueUnderflowException; otherwise, a
  170.         //               reference to a copy of the first element
  171.         //               of the queue is returned.
  172.      public DataElement front() throws QueueUnderflowException
  173.      {
  174.           if(isEmptyQueue())
  175.              throw new QueueUnderflowException();
  176.           DataElement temp = queueFront.info.getCopy();
  177.           return temp;
  178.      }
  179.         //Method to return the last element of the queue.
  180.         //Precondition: The queue exists and is not empty.
  181.         //Postcondition: If the queue is empty, the method throws
  182.         //               QueueUnderflowException; otherwise, a
  183.         //               reference to a copy of the last element
  184.         //               of the queue is returned.
  185.      public DataElement back() throws QueueUnderflowException
  186.      {
  187.           if(isEmptyQueue())
  188.              throw new QueueUnderflowException();
  189.           DataElement temp = queueRear.info.getCopy();
  190.           return temp;
  191.      }
  192.  
  193.         //Method to add queueElement to the queue.
  194.         //Precondition: The queue exists.
  195.         //Postcondition: The queue is changed and queueElement
  196.         //               is added to the queue.
  197.      public void addQueue(DataElement newElement)
  198.      {
  199.           QueueNode newNode;
  200.           newNode = new QueueNode();  //create the node
  201.           newNode.info = newElement.getCopy();  //store the info
  202.           newNode.link = null;   //initialize the link field to null
  203.           if(queueFront == null) //if initially the queue is empty
  204.           {
  205.              queueFront = newNode;
  206.              queueRear = newNode;
  207.           }
  208.           else   //add newNode at the end
  209.           {
  210.              queueRear.link = newNode;
  211.              queueRear = queueRear.link;
  212.           }
  213.      }//end addQueue
  214.  
  215.         //Method to remove the first element of the queue.
  216.         //Precondition: The queue exists and is not empty.
  217.         //Postcondition: The queue is changed and the first
  218.         //               element is removed from the queue.
  219.      public void deleteQueue() throws QueueUnderflowException
  220.      {
  221.           if(isEmptyQueue())
  222.              throw new QueueUnderflowException();
  223.           queueFront = queueFront.link; //advance queueFront
  224.           if(queueFront == null)  //if after deletion the queue is
  225.              queueRear = null;  //empty, set queueRear to null
  226.      } //end deleteQueue
  227.  
  228.         //Method to make a copy of otherQueue.
  229.         //Postcondition: A copy of otherQueue is created and
  230.         //               assigned to this queue.
  231.  
  232.     public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
  233.     {
  234.         LinkedQueueClass intQueue1 = new LinkedQueueClass();
  235.        while(!otherQueue.isEmptyQueue())
  236.   {
  237.       intQueue1.addQueue(otherQueue.front());
  238.       otherQueue.deleteQueue();
  239.         }
  240.     return intQueue1;
  241.  
  242.     }
  243. }
  244.  
  245. public class QueueProgram
  246. {
  247.     public static void main(String[] args)
  248.     {
  249.         LinkedQueueClass intQueue1 = new LinkedQueueClass();
  250.         LinkedQueueClass intQueue2 = new LinkedQueueClass();
  251.         LinkedQueueClass tempintQueue1 = new LinkedQueueClass();
  252.         LinkedQueueClass temmintQueue2 = new LinkedQueueClass();
  253.  
  254.         intQueue1.addQueue(new IntElement(23));
  255.         intQueue1.addQueue(new IntElement(45));
  256.         intQueue1.addQueue(new IntElement(38));
  257.         intQueue2.addQueue(new IntElement(32));
  258.         intQueue2.addQueue(new IntElement(54));
  259.         intQueue2.addQueue(new IntElement(83));
  260.  
  261.         System.out.print("intQueue1 elements: ");
  262.         while(!intQueue1.isEmptyQueue())
  263.         {
  264.             System.out.print(intQueue1.front() + " ");
  265.             intQueue1.deleteQueue();
  266.         }
  267.         System.out.println("Here");
  268.         System.out.print("intQueue2 elements: ");
  269.         while(!intQueue2.isEmptyQueue())
  270.         {
  271.             System.out.print(intQueue2.front() + " ");
  272.             intQueue2.deleteQueue();
  273.         }
  274.  
  275.   //intQueue2 is now empty
  276.   intQueue2.addQueue(new IntElement(32));
  277.   intQueue2.addQueue(new IntElement(54));
  278.         intQueue2.addQueue(new IntElement(83));
  279.  
  280.         intQueue1 = intQueue1.copyQueue(intQueue2);
  281.         System.out.print("After copying intQueue2 elements into  intQueue1, intQueue1 elements are: ");//other way round
  282.  
  283.   while(!intQueue1.isEmptyQueue())
  284.   {
  285.        System.out.print(intQueue1.front() + " ");
  286.        intQueue1.deleteQueue();
  287.         }
  288.  
  289.     }
  290. }
  291.  
  292.  
  293.  
Do the same for the copy constructor

Please, the same what?
Nov 15 '06 #3
r035198x
13,262 8TB
Please, the same what?
Sorry, I meant to adapt the copyQueue method in writing the copy constructor.
Nov 15 '06 #4
satan
28
Sorry, I meant to adapt the copyQueue method in writing the copy constructor.
U meant like this! if so i've done like that but it's not working.


Expand|Select|Wrap|Line Numbers
  1. //copy constructor
  2.     public LinkedQueueClass(LinkedQueueClass otherQueue)
  3.     {
  4.         queueFront = otherQueue.queueFront;
  5.         queueRear  = otherQueue.queueRear;
  6.     }//end copy constructor


Expand|Select|Wrap|Line Numbers
  1. Method to make a copy of otherQueue.
  2.         //Postcondition: A copy of otherQueue is created and
  3.         //               assigned to this queue.
  4.     public void copyQueue(LinkedQueueClass otherQueue)
  5.     {
  6.            queueFront = otherQueue.queueFront;
  7.             queueRear = otherQueue.queueRear;
  8.           }
  9. }
Nov 15 '06 #5
r035198x
13,262 8TB
U meant like this! if so i've done like that but it's not working.


Expand|Select|Wrap|Line Numbers
  1. //copy constructor
  2.     public LinkedQueueClass(LinkedQueueClass otherQueue)
  3.     {
  4.         queueFront = otherQueue.queueFront;
  5.         queueRear = otherQueue.queueRear;
  6.     }//end copy constructor


Expand|Select|Wrap|Line Numbers
  1. Method to make a copy of otherQueue.
  2. //Postcondition: A copy of otherQueue is created and
  3. // assigned to this queue.
  4. public void copyQueue(LinkedQueueClass otherQueue)
  5. {
  6. queueFront = otherQueue.queueFront;
  7. queueRear = otherQueue.queueRear;
  8. }
  9. }
No. To adapt this

Expand|Select|Wrap|Line Numbers
  1. public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
  2. {
  3.   LinkedQueueClass intQueue1 = new LinkedQueueClass();
  4.    while(!otherQueue.isEmptyQueue())
  5.   {
  6.       intQueue1.addQueue(otherQueue.front());
  7.       otherQueue.deleteQueue();
  8.   }
  9.   return intQueue1;
  10.  }
  11.  
Is this not working as well?
Nov 15 '06 #6
satan
28
No. To adapt this

Expand|Select|Wrap|Line Numbers
  1. public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
  2. {
  3.   LinkedQueueClass intQueue1 = new LinkedQueueClass();
  4.    while(!otherQueue.isEmptyQueue())
  5.   {
  6.       intQueue1.addQueue(otherQueue.front());
  7.       otherQueue.deleteQueue();
  8.   }
  9.   return intQueue1;
  10.  }
  11.  
Is this not working as well?
It's not working either
Nov 15 '06 #7
r035198x
13,262 8TB
It's not working either
What are you saying is not working?

Expand|Select|Wrap|Line Numbers
  1. abstract class DataElement
  2. {
  3. public abstract boolean equals(DataElement otherElement);
  4.   //Method to determine whether two objects contain the
  5.   //same data.
  6.   //Postcondition: Returns true if this object contains the
  7.   //    same data as the object otherElement;
  8.   //    otherwise, it returns false.
  9. public abstract int compareTo(DataElement otherElement);
  10.   //Method to compare two objects.
  11.   //Postcondition: Returns a value < 0 if this object is
  12.   // less than the object otherElement;
  13.   //    Returns 0 if this object is the same as
  14.   // the object otherElement.
  15.   //    Returns a value > 0 if this object is
  16.   //   greater than the object otherElement.
  17. public abstract void makeCopy(DataElement otherElement);
  18.   //Method to copy otherElement into this object.
  19.   //Postcondition: The data of otherElement is copied into
  20.   //    this object.
  21. public abstract DataElement getCopy();
  22.   //Method to return a copy of this object.
  23.   //Postcondition: A copy of this object is created and
  24.   //    a reference of the copy is returned.
  25. }
  26.  
  27. class IntElement extends DataElement
  28. {
  29. protected int num;
  30.   //default constructor
  31. public IntElement()
  32. {
  33. num = 0;
  34. }
  35.   //constructor with a parameter
  36. public IntElement(int x)
  37. {
  38. num = x;
  39. }
  40.   //copy constructor
  41. public IntElement(IntElement otherElement)
  42. {
  43. num = otherElement.num;
  44. }
  45.   //Method to set the value of the instance variable num.
  46.   //Postcondition: num = x;
  47. public void setNum(int x)
  48. {
  49. num = x;
  50. }
  51.   //Method to return the value of the instance variable num.
  52.   //Postcondition: The value of num is returned.
  53. public int getNum()
  54. {
  55. return num;
  56. }
  57. public boolean equals(DataElement otherElement)
  58. {
  59. IntElement temp = (IntElement) otherElement;
  60. return (num == temp.num);
  61. }
  62. public int compareTo(DataElement otherElement)
  63. {
  64. IntElement temp = (IntElement) otherElement;
  65. return (num - temp.num);
  66. }
  67. public void makeCopy(DataElement otherElement)
  68. {
  69. IntElement temp = (IntElement) otherElement;
  70. num = temp.num;
  71. }
  72. public DataElement getCopy()
  73. {
  74. IntElement temp = new IntElement(num);
  75. return temp;
  76. }
  77. public String toString()
  78. {
  79. return String.valueOf(num);
  80. }
  81. }
  82.  
  83. class QueueException extends RuntimeException
  84. {
  85. public QueueException()
  86. {
  87. }
  88. public QueueException(String msg)
  89. {
  90. super(msg);
  91. }
  92. }
  93.  
  94. class QueueOverflowException extends QueueException
  95. {
  96. public QueueOverflowException()
  97. {
  98. super("Queue Overflow");
  99. }
  100. public QueueOverflowException(String msg)
  101. {
  102. super(msg);
  103. }
  104. }
  105.  
  106. class QueueUnderflowException extends QueueException
  107. {
  108. public QueueUnderflowException()
  109. {
  110. super("Queue Underflow");
  111. }
  112. public QueueUnderflowException(String msg)
  113. {
  114. super(msg);
  115. }
  116. }
  117.  
  118. class LinkedQueueClass
  119. {
  120.   //Definition of the node
  121. protected class QueueNode
  122. {
  123. DataElement info;
  124. QueueNode link;
  125. }
  126. private QueueNode queueFront; //reference variable to the
  127.   //first element of the queue
  128. private QueueNode queueRear;  //reference variable to the
  129.   //last element of the queue
  130. //default constructor
  131. public LinkedQueueClass()
  132. {
  133. queueFront = null;
  134. queueRear  = null;
  135. }
  136.  
  137. //copy constructor
  138. public LinkedQueueClass(LinkedQueueClass otherQueue)
  139. {
  140. queueFront = otherQueue.queueFront;
  141. queueRear  = otherQueue.queueRear;
  142. }//end copy constructor
  143. //Method to initialize the queue to an empty state.
  144. //Postcondition: queueFront = null; queueRear = null
  145. public void initializeQueue()
  146. {
  147.   queueFront = null;
  148.   queueRear = null;
  149. }
  150. //Method to determine whether the queue is empty.
  151. //Postcondition: Returns true if the queue is empty;
  152. //    otherwise, returns false.
  153. public boolean isEmptyQueue()
  154. {
  155.   return (queueFront == null);
  156. }
  157.  
  158. //Method to determine whether the queue is full.
  159. //Postcondition: Returns true if the queue is full;
  160. //    otherwise, returns false.
  161. public boolean isFullQueue()
  162. {
  163.   return false;
  164. }
  165. //Method to return the first element of the queue.
  166. //Precondition: The queue exists and is not empty.
  167. //Postcondition: If the queue is empty, the method throws
  168. //    QueueUnderflowException; otherwise, a
  169. //    reference to a copy of the first element
  170. //    of the queue is returned.
  171. public DataElement front() throws QueueUnderflowException
  172. {
  173.   if(isEmptyQueue())
  174. throw new QueueUnderflowException();
  175.   DataElement temp = queueFront.info.getCopy();
  176.   return temp;
  177. }
  178. //Method to return the last element of the queue.
  179. //Precondition: The queue exists and is not empty.
  180. //Postcondition: If the queue is empty, the method throws
  181. //    QueueUnderflowException; otherwise, a
  182. //    reference to a copy of the last element
  183. //    of the queue is returned.
  184. public DataElement back() throws QueueUnderflowException
  185. {
  186.   if(isEmptyQueue())
  187. throw new QueueUnderflowException();
  188.   DataElement temp = queueRear.info.getCopy();
  189.   return temp;
  190. }
  191.  
  192. //Method to add queueElement to the queue.
  193. //Precondition: The queue exists.
  194. //Postcondition: The queue is changed and queueElement
  195. //    is added to the queue.
  196. public void addQueue(DataElement newElement)
  197. {
  198.   QueueNode newNode;
  199.   newNode = new QueueNode();  //create the node
  200.   newNode.info = newElement.getCopy();  //store the info
  201.   newNode.link = null;   //initialize the link field to null
  202.   if(queueFront == null) //if initially the queue is empty
  203.   {
  204. queueFront = newNode;
  205. queueRear = newNode;
  206.   }
  207.   else   //add newNode at the end
  208.   {
  209. queueRear.link = newNode;
  210. queueRear = queueRear.link;
  211.   }
  212. }//end addQueue
  213.  
  214. //Method to remove the first element of the queue.
  215. //Precondition: The queue exists and is not empty.
  216. //Postcondition: The queue is changed and the first
  217. //    element is removed from the queue.
  218. public void deleteQueue() throws QueueUnderflowException
  219. {
  220.   if(isEmptyQueue())
  221. throw new QueueUnderflowException();
  222.   queueFront = queueFront.link; //advance queueFront
  223.   if(queueFront == null)  //if after deletion the queue is
  224. queueRear = null;  //empty, set queueRear to null
  225. } //end deleteQueue
  226.  
  227. //Method to make a copy of otherQueue.
  228. //Postcondition: A copy of otherQueue is created and
  229. //    assigned to this queue.
  230.  
  231. public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
  232. {
  233. LinkedQueueClass intQueue1 = new LinkedQueueClass();
  234.    while(!otherQueue.isEmptyQueue())
  235.   {
  236.   intQueue1.addQueue(otherQueue.front());
  237.   otherQueue.deleteQueue();
  238. }
  239. return intQueue1;
  240.  
  241. }
  242. }
  243.  
  244. public class QueueProgram
  245. {
  246. public static void main(String[] args)
  247. {
  248. LinkedQueueClass intQueue1 = new LinkedQueueClass();
  249. LinkedQueueClass intQueue2 = new LinkedQueueClass();
  250. LinkedQueueClass tempintQueue1 = new LinkedQueueClass();
  251. LinkedQueueClass temmintQueue2 = new LinkedQueueClass();
  252.  
  253. intQueue1.addQueue(new IntElement(23));
  254. intQueue1.addQueue(new IntElement(45));
  255. intQueue1.addQueue(new IntElement(38));
  256. intQueue2.addQueue(new IntElement(32));
  257. intQueue2.addQueue(new IntElement(54));
  258. intQueue2.addQueue(new IntElement(83));
  259.  
  260. System.out.print("intQueue1 elements: ");
  261. while(!intQueue1.isEmptyQueue())
  262. {
  263. System.out.print(intQueue1.front() + " ");
  264. intQueue1.deleteQueue();
  265. }
  266. System.out.println("Here");
  267. System.out.print("intQueue2 elements: ");
  268. while(!intQueue2.isEmptyQueue())
  269. {
  270. System.out.print(intQueue2.front() + " ");
  271. intQueue2.deleteQueue();
  272. }
  273.  
  274.   //intQueue2 is now empty
  275.   intQueue2.addQueue(new IntElement(32));
  276.   intQueue2.addQueue(new IntElement(54));
  277. intQueue2.addQueue(new IntElement(83));
  278.  
  279. intQueue1 = intQueue1.copyQueue(intQueue2);
  280. System.out.print("After copying intQueue2 elements into  intQueue1, intQueue1 elements are: ");//other way round
  281.  
  282.   while(!intQueue1.isEmptyQueue())
  283.   {
  284.    System.out.print(intQueue1.front() + " ");
  285.    intQueue1.deleteQueue();
  286. }
  287.  
  288. }
  289. }
  290.  
when I run this it works properly.
Nov 15 '06 #8
satan
28
What are you saying is not working?

Expand|Select|Wrap|Line Numbers
  1. abstract class DataElement
  2. {
  3. public abstract boolean equals(DataElement otherElement);
  4.   //Method to determine whether two objects contain the
  5.   //same data.
  6.   //Postcondition: Returns true if this object contains the
  7.   //    same data as the object otherElement;
  8.   //    otherwise, it returns false.
  9. public abstract int compareTo(DataElement otherElement);
  10.   //Method to compare two objects.
  11.   //Postcondition: Returns a value < 0 if this object is
  12.   // less than the object otherElement;
  13.   //    Returns 0 if this object is the same as
  14.   // the object otherElement.
  15.   //    Returns a value > 0 if this object is
  16.   //   greater than the object otherElement.
  17. public abstract void makeCopy(DataElement otherElement);
  18.   //Method to copy otherElement into this object.
  19.   //Postcondition: The data of otherElement is copied into
  20.   //    this object.
  21. public abstract DataElement getCopy();
  22.   //Method to return a copy of this object.
  23.   //Postcondition: A copy of this object is created and
  24.   //    a reference of the copy is returned.
  25. }
  26.  
  27. class IntElement extends DataElement
  28. {
  29. protected int num;
  30.   //default constructor
  31. public IntElement()
  32. {
  33. num = 0;
  34. }
  35.   //constructor with a parameter
  36. public IntElement(int x)
  37. {
  38. num = x;
  39. }
  40.   //copy constructor
  41. public IntElement(IntElement otherElement)
  42. {
  43. num = otherElement.num;
  44. }
  45.   //Method to set the value of the instance variable num.
  46.   //Postcondition: num = x;
  47. public void setNum(int x)
  48. {
  49. num = x;
  50. }
  51.   //Method to return the value of the instance variable num.
  52.   //Postcondition: The value of num is returned.
  53. public int getNum()
  54. {
  55. return num;
  56. }
  57. public boolean equals(DataElement otherElement)
  58. {
  59. IntElement temp = (IntElement) otherElement;
  60. return (num == temp.num);
  61. }
  62. public int compareTo(DataElement otherElement)
  63. {
  64. IntElement temp = (IntElement) otherElement;
  65. return (num - temp.num);
  66. }
  67. public void makeCopy(DataElement otherElement)
  68. {
  69. IntElement temp = (IntElement) otherElement;
  70. num = temp.num;
  71. }
  72. public DataElement getCopy()
  73. {
  74. IntElement temp = new IntElement(num);
  75. return temp;
  76. }
  77. public String toString()
  78. {
  79. return String.valueOf(num);
  80. }
  81. }
  82.  
  83. class QueueException extends RuntimeException
  84. {
  85. public QueueException()
  86. {
  87. }
  88. public QueueException(String msg)
  89. {
  90. super(msg);
  91. }
  92. }
  93.  
  94. class QueueOverflowException extends QueueException
  95. {
  96. public QueueOverflowException()
  97. {
  98. super("Queue Overflow");
  99. }
  100. public QueueOverflowException(String msg)
  101. {
  102. super(msg);
  103. }
  104. }
  105.  
  106. class QueueUnderflowException extends QueueException
  107. {
  108. public QueueUnderflowException()
  109. {
  110. super("Queue Underflow");
  111. }
  112. public QueueUnderflowException(String msg)
  113. {
  114. super(msg);
  115. }
  116. }
  117.  
  118. class LinkedQueueClass
  119. {
  120.   //Definition of the node
  121. protected class QueueNode
  122. {
  123. DataElement info;
  124. QueueNode link;
  125. }
  126. private QueueNode queueFront; //reference variable to the
  127.   //first element of the queue
  128. private QueueNode queueRear;  //reference variable to the
  129.   //last element of the queue
  130. //default constructor
  131. public LinkedQueueClass()
  132. {
  133. queueFront = null;
  134. queueRear  = null;
  135. }
  136.  
  137. //copy constructor
  138. public LinkedQueueClass(LinkedQueueClass otherQueue)
  139. {
  140. queueFront = otherQueue.queueFront;
  141. queueRear  = otherQueue.queueRear;
  142. }//end copy constructor
  143. //Method to initialize the queue to an empty state.
  144. //Postcondition: queueFront = null; queueRear = null
  145. public void initializeQueue()
  146. {
  147.   queueFront = null;
  148.   queueRear = null;
  149. }
  150. //Method to determine whether the queue is empty.
  151. //Postcondition: Returns true if the queue is empty;
  152. //    otherwise, returns false.
  153. public boolean isEmptyQueue()
  154. {
  155.   return (queueFront == null);
  156. }
  157.  
  158. //Method to determine whether the queue is full.
  159. //Postcondition: Returns true if the queue is full;
  160. //    otherwise, returns false.
  161. public boolean isFullQueue()
  162. {
  163.   return false;
  164. }
  165. //Method to return the first element of the queue.
  166. //Precondition: The queue exists and is not empty.
  167. //Postcondition: If the queue is empty, the method throws
  168. //    QueueUnderflowException; otherwise, a
  169. //    reference to a copy of the first element
  170. //    of the queue is returned.
  171. public DataElement front() throws QueueUnderflowException
  172. {
  173.   if(isEmptyQueue())
  174. throw new QueueUnderflowException();
  175.   DataElement temp = queueFront.info.getCopy();
  176.   return temp;
  177. }
  178. //Method to return the last element of the queue.
  179. //Precondition: The queue exists and is not empty.
  180. //Postcondition: If the queue is empty, the method throws
  181. //    QueueUnderflowException; otherwise, a
  182. //    reference to a copy of the last element
  183. //    of the queue is returned.
  184. public DataElement back() throws QueueUnderflowException
  185. {
  186.   if(isEmptyQueue())
  187. throw new QueueUnderflowException();
  188.   DataElement temp = queueRear.info.getCopy();
  189.   return temp;
  190. }
  191.  
  192. //Method to add queueElement to the queue.
  193. //Precondition: The queue exists.
  194. //Postcondition: The queue is changed and queueElement
  195. //    is added to the queue.
  196. public void addQueue(DataElement newElement)
  197. {
  198.   QueueNode newNode;
  199.   newNode = new QueueNode();  //create the node
  200.   newNode.info = newElement.getCopy();  //store the info
  201.   newNode.link = null;   //initialize the link field to null
  202.   if(queueFront == null) //if initially the queue is empty
  203.   {
  204. queueFront = newNode;
  205. queueRear = newNode;
  206.   }
  207.   else   //add newNode at the end
  208.   {
  209. queueRear.link = newNode;
  210. queueRear = queueRear.link;
  211.   }
  212. }//end addQueue
  213.  
  214. //Method to remove the first element of the queue.
  215. //Precondition: The queue exists and is not empty.
  216. //Postcondition: The queue is changed and the first
  217. //    element is removed from the queue.
  218. public void deleteQueue() throws QueueUnderflowException
  219. {
  220.   if(isEmptyQueue())
  221. throw new QueueUnderflowException();
  222.   queueFront = queueFront.link; //advance queueFront
  223.   if(queueFront == null)  //if after deletion the queue is
  224. queueRear = null;  //empty, set queueRear to null
  225. } //end deleteQueue
  226.  
  227. //Method to make a copy of otherQueue.
  228. //Postcondition: A copy of otherQueue is created and
  229. //    assigned to this queue.
  230.  
  231. public LinkedQueueClass copyQueue(LinkedQueueClass otherQueue)
  232. {
  233. LinkedQueueClass intQueue1 = new LinkedQueueClass();
  234.    while(!otherQueue.isEmptyQueue())
  235.   {
  236.   intQueue1.addQueue(otherQueue.front());
  237.   otherQueue.deleteQueue();
  238. }
  239. return intQueue1;
  240.  
  241. }
  242. }
  243.  
  244. public class QueueProgram
  245. {
  246. public static void main(String[] args)
  247. {
  248. LinkedQueueClass intQueue1 = new LinkedQueueClass();
  249. LinkedQueueClass intQueue2 = new LinkedQueueClass();
  250. LinkedQueueClass tempintQueue1 = new LinkedQueueClass();
  251. LinkedQueueClass temmintQueue2 = new LinkedQueueClass();
  252.  
  253. intQueue1.addQueue(new IntElement(23));
  254. intQueue1.addQueue(new IntElement(45));
  255. intQueue1.addQueue(new IntElement(38));
  256. intQueue2.addQueue(new IntElement(32));
  257. intQueue2.addQueue(new IntElement(54));
  258. intQueue2.addQueue(new IntElement(83));
  259.  
  260. System.out.print("intQueue1 elements: ");
  261. while(!intQueue1.isEmptyQueue())
  262. {
  263. System.out.print(intQueue1.front() + " ");
  264. intQueue1.deleteQueue();
  265. }
  266. System.out.println("Here");
  267. System.out.print("intQueue2 elements: ");
  268. while(!intQueue2.isEmptyQueue())
  269. {
  270. System.out.print(intQueue2.front() + " ");
  271. intQueue2.deleteQueue();
  272. }
  273.  
  274.   //intQueue2 is now empty
  275.   intQueue2.addQueue(new IntElement(32));
  276.   intQueue2.addQueue(new IntElement(54));
  277. intQueue2.addQueue(new IntElement(83));
  278.  
  279. intQueue1 = intQueue1.copyQueue(intQueue2);
  280. System.out.print("After copying intQueue2 elements into  intQueue1, intQueue1 elements are: ");//other way round
  281.  
  282.   while(!intQueue1.isEmptyQueue())
  283.   {
  284.    System.out.print(intQueue1.front() + " ");
  285.    intQueue1.deleteQueue();
  286. }
  287.  
  288. }
  289. }
  290.  
when I run this it works properly.
Yeah! it's running properly, thanks for your help. But the way you wrote the method copyQueue is not the same thing the problem asks me to do
Nov 15 '06 #9
r035198x
13,262 8TB
Yeah! it's running properly, thanks for your help. But the way you wrote the method copyQueue is not the same thing the problem asks me to do
How does the problem want you to write the copyQueue method then?There are several ways of doing it.
Nov 16 '06 #10
satan
28
How does the problem want you to write the copyQueue method then?There are several ways of doing it.
This is the heading for the method copyQueue

Expand|Select|Wrap|Line Numbers
  1. /Method to make a copy of otherQueue.
  2.         //Postcondition: A copy of otherQueue is created and
  3.         //               assigned to this queue.
  4.     public void copyQueue(LinkedQueueClass otherQueue)
Nov 20 '06 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
0
by: xunling | last post by:
i have a question about answering ..... this topic is "need help" what do i have to write at te topic line, !after i have klicked the "answer message" button ive tried many possibilities,...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.