Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void print() {
- Node<Comparable> temp = head;
- while (temp != null) {
- System.out.print(temp + " ");
- temp = temp.next;
- }
- }
- @Override
- public void add(int index, Comparable element) {
- if(head == null) return;
- Node<Comparable> node = new Node(element);
- Node<Comparable> current = head;
- Node<Comparable> temp;
- for (int i = 1; i < index; i++) {
- if (current.next != null) {
- current = current.next;
- }
- if (i == index - 1) {
- temp = current.next;
- current.next = node;
- node.next = temp;
- }
- }
- }
- public static void main(String[] args) {
- SortableLinkedList<String> list= new SortableLinkedList<>();
- list.add("Kristian");
- list.add("Avlesbug");
- list.add("er");
- list.add("kul!");
- //list.add(3, "ikke");
- list.print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement