Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. package linkedList;
  2.  
  3. public class SinglyLinkedList<T extends Comparable<T>> implements List<T> {
  4.  
  5. private Node<T> top;
  6. private int listSize;
  7.  
  8. //Creates a node and sets the size of the list
  9. public SinglyLinkedList(){
  10. top = new Node<T>(null);
  11. listSize = 0;
  12. }
  13.  
  14. //Inserts element at a specific position
  15. public void add(int index, T value) throws ListAccessError {
  16. Node<T> addNode = new Node<T>(value);
  17. Node<T> nextE = top;
  18. if(index > (listSize + 1)) throw new ListAccessError("You can't add an element to an index greater than the list size");
  19. //For each element less than index skip over then move each element one
  20. //more pointer
  21. for (int i = 1; i < index && nextE.next() != null; i++) {
  22. nextE = nextE.next();
  23. }
  24. //Set the pointer to the next node
  25. addNode.setNext(nextE.next());
  26. //Set this node to the old node pointer
  27. nextE.setNext(addNode);
  28. //Increment the size
  29. listSize++;
  30. }
  31.  
  32. //Gets the element at a specific index
  33. public T get(int index) throws ListAccessError{
  34. //Index needs to be more than 1 since index 0 is top
  35. if (index <= 0)
  36. throw new ListAccessError("This is the top index");
  37. //Starts any counting from the next element after the top
  38. Node <T> nextE = top.next();
  39. //Each element less than the index is ignored
  40. for (int i = 1; i < index; i++) {
  41. if (nextE.next() == null)
  42. throw new ListAccessError("There is no node to get");
  43. nextE = nextE.next();
  44. }
  45. //Returns the value of the next index from index - 1 i.e index
  46. return nextE.value();
  47. }
  48.  
  49. //Removes specific element
  50. public T remove(int index) throws ListAccessError{
  51. //If the index is the same as top or greater than the list size throw an error
  52. if (index < 1 || index > listSize)
  53. throw new ListAccessError ("Index not within specified list");
  54. //Each element less than the specific index is ignored
  55. Node<T> nextE = top;
  56. for (int i = 1; i < index; i++) {
  57. //If there isn't pointer to the next node throw error
  58. if (nextE.next() == null)
  59. throw new ListAccessError ("There is no node to remove");
  60. nextE = nextE.next();
  61. }
  62. //Assigns value of removed item to elementValue
  63. T elementValue = nextE.next().value();
  64. //Sets the current node pointer to the pointer after the removed node
  65. nextE.setNext(nextE.next().next());
  66. //Reduces list size by 1
  67. listSize--;
  68. return elementValue;
  69. }
  70.  
  71. //Return True or False depending on whether list is empty
  72. public boolean isEmpty(){
  73. return(top == null);
  74. }
  75.  
  76. //For outputing the test
  77. public String toString() {
  78. Node<T> indexE = top.next();
  79. String testOutput = "";
  80. while (indexE != null) {
  81. testOutput += "[" + indexE.value().toString() + "]";
  82. indexE = indexE.next();
  83. }
  84. return testOutput;
  85. }
  86.  
  87. //Main method
  88. public static void main(String[] args) throws ListAccessError {
  89. SinglyLinkedList<String> newList = new SinglyLinkedList<String>();
  90.  
  91. //Add elements to SinglyLinkedList
  92. newList.add(1,"1");
  93. newList.add(2,"2");
  94. newList.add(3,"3");
  95. newList.add(4,"4");
  96. newList.add(5,"5");
  97. newList.add(2, "6");
  98.  
  99. //Test that methods work
  100. try{
  101. System.out.println("List: " + newList);
  102. System.out.println("Get 2nd element: " + newList.get(2));
  103. System.out.println("Remove 2nd element: " + newList.remove(2));
  104. System.out.println("List size: " + newList.listSize);
  105. System.out.println("Update list: " + newList);
  106. } finally {
  107. System.out.println("Is list empty?: " + newList.isEmpty());
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement