Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. public void print() {
  2. Node<Comparable> temp = head;
  3. while (temp != null) {
  4. System.out.print(temp + " ");
  5. temp = temp.next;
  6. }
  7. }
  8.  
  9.  
  10.  
  11.  
  12.  
  13. @Override
  14. public void add(int index, Comparable element) {
  15. if(head == null) return;
  16.  
  17. Node<Comparable> node = new Node(element);
  18. Node<Comparable> current = head;
  19. Node<Comparable> temp;
  20. for (int i = 1; i < index; i++) {
  21. if (current.next != null) {
  22. current = current.next;
  23. }
  24. if (i == index - 1) {
  25. temp = current.next;
  26. current.next = node;
  27. node.next = temp;
  28. }
  29. }
  30. }
  31.  
  32.  
  33.  
  34. public static void main(String[] args) {
  35. SortableLinkedList<String> list= new SortableLinkedList<>();
  36. list.add("Kristian");
  37. list.add("Avlesbug");
  38. list.add("er");
  39. list.add("kul!");
  40. //list.add(3, "ikke");
  41.  
  42. list.print();
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement