Advertisement
Guest User

Untitled

a guest
Mar 10th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. // A class's description to be provided by student
  2. // Fill in code in ALL commented areas
  3.  
  4. public class LinkedList {
  5. // Defined Node class
  6. private class Node {
  7. private Object Data = null;
  8. private Node Next = null;
  9. public Node() {
  10. Data = null;
  11. Next = null;
  12. }
  13. public Node(Object element) {
  14. Data = element;
  15. }
  16. public Node(Object o, Node n) {
  17. Data = o;
  18. Next = n;
  19. }
  20. public void setNext(Node n) {
  21. Next = n;
  22. }
  23. public Node getNext() {
  24. return Next;
  25. }
  26. public Object getElement() {
  27. return Data;
  28. }
  29. public void setElement(Object element) {
  30. Data = element;
  31. }
  32. }
  33.  
  34. // Internal data for LinkedList
  35. private Node head = null;
  36. private Node current = null;
  37. private int size = 0;
  38.  
  39. // LinkedList constructor
  40. public LinkedList() {
  41. head = null;
  42. current = head;
  43. }
  44.  
  45. // Move the current position forward one position
  46. public void forward() {
  47.  
  48. }
  49.  
  50. // Move the current position backward one position
  51. public void backward() {
  52.  
  53. }
  54.  
  55. // Get current object's data element
  56. public Object currentData() {
  57.  
  58. }
  59.  
  60.  
  61. // Add object to the first of the list
  62. public void addFirst(Object o) {
  63.  
  64. }
  65.  
  66.  
  67. // resetCurrent at the first position
  68. public void resetCurrent() {
  69.  
  70. }
  71.  
  72. // Add object to the last of the list
  73. public void addLast(Object o) {
  74.  
  75. }
  76.  
  77. // Add an object o before the current position
  78. public void insertBefore(Object o) {
  79.  
  80. }
  81.  
  82. // Add an object o after the current position
  83. public void insertAfter(Object o) {
  84.  
  85. }
  86.  
  87. // Get first object
  88. public Object getFirst() {
  89.  
  90. }
  91.  
  92. // Get last object
  93. public Object getLast() {
  94.  
  95. }
  96.  
  97. // Remove the first object
  98. public Object removeFirst(){
  99.  
  100. }
  101.  
  102. // Remove the last object
  103. public Object removeLast() {
  104.  
  105. }
  106.  
  107. // Remove object o from the list
  108. public void remove(Object o) {
  109.  
  110. }
  111.  
  112. // Returns the number of elements on the list
  113. public int size() {
  114. return size;
  115. }
  116.  
  117. // Is the list emptied?
  118. public boolean isEmpty() {
  119.  
  120. }
  121.  
  122. // Display a content of a list
  123. public String toString() {
  124. String r = "( HEAD -> ";
  125. // Node l = head.getNext();
  126. Node l = head;
  127. while (l != null) {
  128. r = r + l.getElement() + " -> " ;
  129. l = l.getNext();
  130. }
  131. return r + " )";
  132. }
  133.  
  134. public static void main(String args[]) {
  135. LinkedList lst = new LinkedList();
  136. // creat instances for testing
  137. CsusStudent instance1 = new CsusStudent("John Doe 1", 1, "1 Somewhere", "916-555-1211", "johndoe1@somewhere.com");
  138. CsusStudent instance2 = new CsusStudent("John Doe 2", 2, "2 Somewhere", "916-555-1212", "johndoe2@somewhere.com");
  139. CsusStudent instance3 = new CsusStudent("John Doe 3", 3, "3 Somewhere", "916-555-1213", "johndoe3@somewhere.com");
  140. CsusStudent instance4 = new CsusStudent("John Doe 4", 4, "4 Somewhere", "916-555-1214", "johndoe4@somewhere.com");
  141. CsusStudent instance5 = new CsusStudent("John Doe 5", 5, "5 Somewhere", "916-555-1215", "johndoe5@somewhere.com");
  142. CsusStudent instance6 = new CsusStudent("John Doe 6", 6, "6 Somewhere", "916-555-1216", "johndoe6@somewhere.com");
  143. CsusStudent instance7 = new CsusStudent("John Doe 7", 7, "7 Somewhere", "916-555-1217", "johndoe7@somewhere.com");
  144. CsusStudent instance8 = new CsusStudent("John Doe 8", 8, "8 Somewhere", "916-555-1218", "johndoe8@somewhere.com");
  145. CsusStudent instance9 = new CsusStudent("John Doe 9", 9, "9 Somewhere", "916-555-1219", "johndoe9@somewhere.com");
  146.  
  147. // begin adding instance1 to the list
  148.  
  149.  
  150. // test forward and backward for single entry
  151.  
  152.  
  153. // now add instance2 and instance3 via addFirst and instance4, instance5, instance6 via addLast
  154.  
  155.  
  156. // move current forward a few times
  157.  
  158. // insert instance 9 after
  159.  
  160. // remove instance9
  161.  
  162.  
  163. // print out the first and last entries
  164. System.out.println("Show the first entry and last entry:");
  165.  
  166.  
  167. // print out the whole list
  168. System.out.println("Show the whole list:");
  169.  
  170.  
  171. // remove entries starting from the last entry
  172. System.out.println("Check for the content of the emptied list");
  173.  
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement