Recent Posts
None | 0 sec ago
None | 42 sec ago
Bash | 1 min ago
None | 1 min ago
CSS | 2 min ago
Diff | 2 min ago
HTML | 3 min ago
None | 4 min ago
None | 4 min ago
None | 4 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By joey on the 9th of Feb 2010 10:16:22 PM Download | Raw | Embed | Report
  1. /**
  2. 190 * Make the current list a copy of another list.
  3. 191 * Performs a deep copy, that is it makes a new copies of each of
  4. 8
  5. Data Structures and Team Project Answers – Lists
  6. 192 * the DataElements within the other list.
  7. 193 *
  8. 194 * @param otherList the LinkedListClass to duplicate.
  9. 195 */
  10. 196
  11. 197 private void copy(LinkedListClassEx3 otherList) {
  12. 198 LinkedListNode newNode; // variable to create a node
  13. 199 LinkedListNode current; // variable to traverse the list
  14. 200
  15. 201 first = null; //make this list empty
  16. 202
  17. 203 if ( otherList.first == null ) {
  18. 204 // otherList is empty
  19. 205 first = null;
  20. 206 last = null;
  21. 207 count = 0;
  22. 208 } else {
  23. 209 // Copy other list into this list
  24. 210 // Start with the first element of the other list
  25. 211 count = otherList.count;
  26. 212 current = otherList.first;
  27. 213
  28. 214 // Copy the first element
  29. 215 first = new LinkedListNode();
  30. 216 first.data = current.data.getCopy();
  31. 217 first.link = null;
  32. 218
  33. 219 // We now have one node in out list
  34. 220 last = first;
  35. 221
  36. 222 // Start to traverse the other list
  37. 223 current = current.link;
  38. 224
  39. 225 // While there are nodes in the other list,
  40. 226 // copy them into this list
  41. 227 while ( current != null ) {
  42. 228 // Make copy of node in other list
  43. 229 newNode = new LinkedListNode();
  44. 230 newNode.data = current.data.getCopy();
  45. 231
  46. 232 // Add new node to end of this list
  47. 233 newNode.link = null;
  48. 234 last.link = newNode;
  49. 235 last = newNode;
  50. 236
  51. 237 // Move on to next node in other list
  52. 238 current = current.link;
  53. 239 }
  54. 240 }
  55. 241 }
  56. 242
  57. 243 /**
  58. 244 * Make this list a copy of another list.
  59. 245 * This is simply another name for the copy() method.
  60. 246 *
  61. 247 * @param otherList the other LinkedListClass to duplicate.
  62. 248 */
  63. 249
  64. 250 public void copyList(LinkedListClassEx3 otherList) {
  65. 251 if ( this != otherList ) {
  66. 252 //avoid self-copy
  67. 253 copy(otherList);
  68. 254 }
  69. 255 }
  70. 256
  71. 257 /**
  72. 9
  73. Data Structures and Team Project Answers – Lists
  74. 258 * Copy constructor, makes a new list which is a direct copy
  75. 259 * of another list. This list is a deep copy of the other
  76. 260 * list.
  77. 261 *
  78. 262 * @param otherList the other LinkedListClass to copy.
  79. 263 */
  80. 264
  81. 265 public LinkedListClassEx3(LinkedListClassEx3 otherList) {
  82. 266 copy(otherList);
  83. 267 }
  84. 268 }
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: