By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,569 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,569 IT Pros & Developers. It's quick & easy.

need assistance

P: 28
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList.

These are my classes:



Expand|Select|Wrap|Line Numbers
  1. public class UnorderedLinkedList extends LinkedListClass
  2. {
  3.  
  4. public UnorderedLinkedList()
  5. {
  6. super();
  7. }
  8.  
  9. public UnorderedLinkedList(UnorderedLinkedList otherList)
  10. {
  11. super(otherList);
  12. }
  13.  
  14. //Method to determine whether searchItem is in
  15. //the list.
  16. //Postcondition: Returns true if searchItem is found
  17. // in the list; false otherwise.
  18. public boolean search(DataElement searchItem)
  19. {
  20. LinkedListNode current; //variable to traverse the list
  21. boolean found;
  22.  
  23. current = first; //set current to point to the first
  24. //node in the list
  25.  
  26. found = false; //set found to false
  27.  
  28. while(current != null && !found) //search the list
  29. if(current.info.equals(searchItem)) //item is found
  30. found = true;
  31. else
  32. current = current.link; //make current point to
  33. //the next node
  34. return found;
  35. }
  36.  
  37.  
  38. //Method to delete deleteItem from the list.
  39. //Postcondition: If found, the node containing
  40. // deleteItem is deleted from the
  41. // list. Also first points to the first
  42. // node, last points to the last
  43. // node of the updated list, and count
  44. // is decremented by 1.
  45. public void deleteNode(DataElement deleteItem)
  46. {
  47. LinkedListNode current; //variable to traverse the list
  48. LinkedListNode trailCurrent; //variable just before current
  49. boolean found;
  50.  
  51. if(first == null) //Case 1; the list is empty
  52. System.err.println("Cannot delete from an empty list.");
  53. else
  54. {
  55. if(first.info.equals(deleteItem)) //Case 2
  56. {
  57. first = first.link;
  58.  
  59. if(first == null) //the list had only one node
  60. last = null;
  61. count--;
  62. }
  63. else //search the list for the node with the given info
  64. {
  65. found = false;
  66. trailCurrent = first; //set trailCurrent to point to
  67. //the first node
  68. current = first.link; //set current to point to the
  69. //second node
  70.  
  71. while(current != null && !found)
  72. {
  73. if(current.info.equals(deleteItem))
  74. found = true;
  75. else
  76. {
  77. trailCurrent = current;
  78. current = current.link;
  79. }
  80. }//end while
  81.  
  82. if(found) //Case 3; if found, delete the node
  83. {
  84. count--;
  85. trailCurrent.link = current.link;
  86.  
  87. if(last == current) //node to be deleted was
  88. //the last node
  89. last = trailCurrent; //update the value of last
  90. }
  91. else
  92. System.out.println("Item to be deleted is "
  93. + "not in the list.");
  94. }//end else
  95. }//end else
  96. }//end deleteNode
  97. }


Expand|Select|Wrap|Line Numbers
  1. public abstract class LinkedListClass
  2. {
  3. protected class LinkedListNode
  4. {
  5. DataElement info;
  6. LinkedListNode link;
  7. }
  8.  
  9. protected LinkedListNode first; //variable to store the
  10. //address of the first
  11. //node of the list
  12. protected LinkedListNode last; //variable to store the
  13. //address of the last
  14. //node of the list
  15. protected int count; //variable to store the number of nodes
  16. //in the list
  17.  
  18.  
  19. //default constructor
  20. //Initializes the list to an empty state.
  21. //Postcondition: first = null, last = null,
  22. // count = 0
  23. public LinkedListClass()
  24. {
  25. first = null;
  26. last = null;
  27. count = 0;
  28. }
  29.  
  30. //Method to determine whether the list is empty.
  31. //Postcondition: Returns true if the list is empty;
  32. // false otherwise.
  33. public boolean isEmptyList()
  34. {
  35. return (first == null);
  36. }
  37.  
  38. //Method to initialize the list to an empty state.
  39. //Postcondition: first = null, last = null,
  40. // count = 0
  41. public void initializeList()
  42. {
  43. first = null;
  44. last = null;
  45. count = 0;
  46. }
  47.  
  48. //Method to output the data contained in each node.
  49. public void print()
  50. {
  51. LinkedListNode current; //variable to traverse the list
  52.  
  53. current = first; //set current so that it points to
  54. //the first node
  55. while(current != null) //while more data to print
  56. {
  57. System.out.print(current.info + " ");
  58. current = current.link;
  59. }
  60. }//end print
  61.  
  62. //Method to return the number of nodes in the list.
  63. //Postcondition: The value of count is returned.
  64. public int length()
  65. {
  66. return count;
  67. }
  68.  
  69.  
  70. //Method to return a reference of the object containing
  71. //the data of the first node of the list.
  72. //Precondition: The list must exist and must not be empty.
  73. //Postcondition: The reference of the object that
  74. // contains the info of the first node
  75. // is returned.
  76. public DataElement front()
  77. {
  78. DataElement temp = first.info.getCopy();
  79. return temp;
  80. }
  81.  
  82. //Method to return a reference of object containing
  83. //the data of the last node of the list.
  84. //Precondition: The list must exist and must not be empty.
  85. //Postcondition: The reference of the object that
  86. // contains the info of the last node
  87. // is returned.
  88. public DataElement back()
  89. {
  90. DataElement temp = last.info.getCopy();
  91. return temp;
  92. }
  93.  
  94. //Method to determine whether searchItem is in the list.
  95. //Postcondition: Returns true if searchItem is found
  96. // in the list; false otherwise.
  97. public abstract boolean search(DataElement searchItem);
  98.  
  99.  
  100. //Method to insert newItem in the list.
  101. //Postcondition: first points to the new list
  102. // and newItem is inserted at the
  103. // beginning of the list. Also,
  104. // last points to the last node and
  105. // count is incremented by 1.
  106. public void insertFirst(DataElement newItem)
  107. {
  108. LinkedListNode newNode; //variable to create the
  109. //new node
  110.  
  111. newNode = new LinkedListNode(); //create the new node
  112. newNode.info = newItem.getCopy(); //assign a copy of
  113. //newItem to the node
  114. newNode.link = first; //insert newNode before first
  115. first = newNode; //make first point to the
  116. //actual first node
  117.  
  118. if(last == null) //if the list was empty, newNode is
  119. //also the last node in the list
  120. last = newNode;
  121.  
  122. count++;
  123. }
  124.  
  125. //Method to insert newItem at the end of the list.
  126. //Postcondition: first points to the new list and
  127. // newItem is inserted at the end
  128. // of the list. Also, last points to
  129. // the last node and
  130. // count is incremented by 1.
  131. public void insertLast(DataElement newItem)
  132. {
  133. LinkedListNode newNode; //variable to create the new node
  134.  
  135. newNode = new LinkedListNode(); //create the new node
  136. newNode.info = newItem.getCopy(); //assign a copy of
  137. //newItem to the node
  138. newNode.link = null; //set the link field of
  139. //newNode to null
  140.  
  141. if(first == null) //if the list is empty, newNode is
  142. //both the first and last node
  143. {
  144. first = newNode;
  145. last = newNode;
  146. }
  147. else //if the list is not empty, insert newNode after last
  148. {
  149. last.link = newNode; //insert newNode after last
  150. last = newNode; //set last to point to the actual last node
  151. }
  152.  
  153. count++;
  154. }//end insertLast
  155.  
  156.  
  157.  
  158. //Method to delete deleteItem from the list.
  159. //Postcondition: If found, the node containing
  160. // deleteItem is deleted from the
  161. // list. Also first points to the first
  162. // node, last points to the last
  163. // node of the updated list, and count
  164. // is decremented by 1.
  165. public abstract void deleteNode(DataElement deleteItem);
  166.  
  167. //Method to return a reference of the copy of otherList.
  168. private void copy(LinkedListClass otherList)
  169. {
  170. LinkedListNode newNode; //variable to create a node
  171. LinkedListNode current; //variable to traverse the list
  172.  
  173. first = null; //make this list empty
  174.  
  175. if(otherList.first == null) //otherList is empty
  176. {
  177. first = null;
  178. last = null;
  179. count = 0;
  180. }
  181. else
  182. {
  183. count = otherList.count;
  184. current = otherList.first; //current points to the
  185. //list to be copied
  186.  
  187. //copy the first element
  188. first = new LinkedListNode(); //create the node
  189. first.info = current.info.getCopy(); //copy the info
  190. first.link = null; //set the link field of
  191. //the node to null
  192. last = first; //make last point to the first node
  193. current = current.link; //make current point to the next
  194. //node of the list being copied
  195.  
  196. //copy the remaining list
  197. while(current != null)
  198. {
  199. newNode = new LinkedListNode();
  200. newNode.info = current.info.getCopy();
  201. newNode.link = null;
  202. last.link = newNode;
  203. last = newNode;
  204. current = current.link;
  205. }//end while
  206. }//end else
  207. }//end copy
  208.  
  209.  
  210. //Method to return a reference of the copy of otherList.
  211. public void copyList(LinkedListClass otherList)
  212. {
  213. if(this != otherList) //avoid self-copy
  214. copy(otherList);
  215. }
  216.  
  217. //copy constructor
  218. public LinkedListClass(LinkedListClass otherList)
  219. {
  220. copy(otherList);
  221. }//end copy constructor

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class TestProgUnorderedLinkedList
  5. {
  6. static BufferedReader keyboard = new
  7. BufferedReader(new InputStreamReader(System.in));
  8.  
  9. public static void main(String[] args) throws IOException
  10. {
  11. UnorderedLinkedList list
  12. = new UnorderedLinkedList();
  13. UnorderedLinkedList tempList =
  14. new UnorderedLinkedList();
  15. IntElement num = new IntElement();
  16.  
  17. StringTokenizer tokenizer;
  18.  
  19. int position;
  20.  
  21. System.out.print(" Enter 8 integers on the " + "same line: ");
  22. System.out.flush();
  23.  
  24. tokenizer = new
  25. StringTokenizer(keyboard.readLine());
  26.  
  27. num.setNum(Integer.parseInt(tokenizer.nextToken()) );
  28.  
  29. while (tokenizer.hasMoreTokens())
  30. {
  31. num.setNum(Integer.parseInt(tokenizer.nextToken()) );
  32. list.insertLast(num);
  33. }
  34.  
  35. System.out.println();
  36. System.out.print("The list you " + "entered is: ");
  37. list.print();
  38. System.out.println();
  39. System.out.println("Length of list: " + list.length());
  40.  
  41. tempList.copyList(list);
  42.  
  43. System.out.print("Enter the num to " + "be deleted: ");
  44. System.out.flush();
  45. num.setNum(Integer.parseInt(keyboard.readLine())); 
  46. System.out.println();
  47.  
  48. tempList.deleteNode(num);
  49. System.out.println("After deleting " + num + " the node, tempList:");
  50. tempList.print();
  51. System.out.println();
  52. System.out.println("Length of list: " + tempList.length());
  53.  
  54. System.out.print("Enter the search " + "item: ");
  55. System.out.flush();
  56.  
  57. num.setNum(Integer.parseInt(keyboard.readLine())); 
  58. System.out.println();
  59.  
  60. if(tempList.search(num) != -1)
  61. System.out.println("Item found in " + "the list");
  62. else
  63. System.out.println("Item not found");
  64.  
  65. System.out.print("The list temp: ");
  66. tempList.print();
  67. System.out.println();
  68. System.out.println("Length of list: " + tempList.length());
  69. }
  70. }
Oct 17 '06 #1
Share this Question
Share on Google+
7 Replies


10K+
P: 13,264
Please keep your posts in one thread.
Where in your classes do you need help?
Have you tried the advice that was given to you earlier?
Oct 17 '06 #2

P: 28
Please keep your posts in one thread.
Where in your classes do you need help?
Have you tried the advice that was given to you earlier?
In the main program
Oct 17 '06 #3

10K+
P: 13,264
In the main program
Where is the code for the other 2 classes DataElement and IntElement?
What makes us sure that help given now will not be ignored like all the help given before?
Oct 18 '06 #4

P: 28
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. }
Oct 19 '06 #5

10K+
P: 13,264
Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args) throws IOException
  2. {
  3. UnorderedLinkedList list
  4. = new UnorderedLinkedList();
  5. UnorderedLinkedList tempList =
  6. new UnorderedLinkedList();
  7. IntElement num = new IntElement();
  8.  
  9. StringTokenizer tokenizer;
  10.  
  11. int position;
  12.  
  13. System.out.print(" Enter 8 integers on the " + "same line: ");
  14. System.out.flush();
  15.  
  16. tokenizer = new
  17. StringTokenizer(keyboard.readLine());
  18.  
  19. //num.setNum(Integer.parseInt(tokenizer.nextToken())   ); ***commented out 
  20.  
  21. while (tokenizer.hasMoreTokens())
  22. {
  23. num.setNum(Integer.parseInt(tokenizer.nextToken())   );
  24. list.insertLast(num);
  25. }
  26.  
  27. System.out.println();
  28. System.out.print("The list you " + "entered is: ");
  29. list.print();
  30. System.out.println();
  31. System.out.println("Length of list: " + list.length());
  32.  
  33. tempList.copyList(list);
  34.  
  35. System.out.print("Enter the num to " + "be deleted: ");
  36. System.out.flush();
  37. num.setNum(Integer.parseInt(keyboard.readLine()));
  38. System.out.println();
  39.  
  40. tempList.deleteNode(num);
  41. System.out.println("After deleting " + num + " the node, tempList:");
  42. tempList.print();
  43. System.out.println();
  44. System.out.println("Length of list: " + tempList.length());
  45.  
  46. System.out.print("Enter the search " + "item: ");
  47. System.out.flush();
  48.  
  49. num.setNum(Integer.parseInt(keyboard.readLine()));
  50. System.out.println();
  51.  
  52. if(tempList.search(num))//****boolean please
  53. System.out.println("Item found in " + "the list");
  54. else
  55. System.out.println("Item not found");
  56.  
  57. System.out.print("The list temp: ");
  58. tempList.print();
  59. System.out.println();
  60. System.out.println("Length of list: " + tempList.length());
  61. }
I could only guess what you wanted to do, so I made those two changes
Oct 19 '06 #6

P: 28
Thank you. i appreciate it.
Oct 23 '06 #7

10K+
P: 13,264
Thank you. i appreciate it.
I don't really see how I've helped you here, but hopefully now you will forgive my irrational attack at your username. I hope you understand ofcourse given the circumstances...
Oct 23 '06 #8

Post your reply

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