Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyList<E> implements SingleList<E> {
- private Node<E> head;
- private Node<E> tail;
- private int size;
- public MyList() {
- this.head = null;
- this.tail = null;
- this.size = 0;
- }
- private static class Node<E> {
- E data;
- Node<E> next;
- public Node(E data, Node<E> next) {
- this.data = data;
- this.next = next;
- }
- public void setNext(Node<E> next) {
- this.next = next;
- }
- }
- //ITERATOR
- private class iter implements Iterator<E> {
- Node<E> previousNode;
- Node<E> currentNode;
- Node<E> nextNode;
- public iter() {
- previousNode = null;
- currentNode = null;
- nextNode = head;
- }
- @Override
- public boolean hasNext() {
- return nextNode != null;
- }
- @Override
- public E next() {
- if (!hasNext()) {
- throw new NoSuchElementException("No more elements");
- }
- previousNode = currentNode;
- currentNode = nextNode;
- nextNode = nextNode.next;
- return currentNode.data;
- }
- @Override
- public void remove() {
- if (currentNode == null) {
- throw new IllegalStateException();
- }
- if (previousNode == null) {
- head = nextNode;
- } else {
- previousNode.next = nextNode;
- currentNode = previousNode;
- }
- size--;
- }
- }
- //KLAR
- @Override
- public Iterator<E> iterator() {
- return new iter();
- }
- @Override
- public void add(E element) {
- Node<E> newNode = new Node<E>(element, null);
- if (head == null) {
- head = newNode;
- tail = newNode;
- } else {
- tail.next = newNode;
- tail = newNode;
- }
- size++;
- }
- //KLAR
- @Override
- public void add(int index, E element) {
- if (index < 0 || index > size) {
- throw new IndexOutOfBoundsException(Integer.toString(index));
- }
- if (index == size) {
- add(element);
- return;
- }
- if (index == 0) {
- head = new Node<E>(element, head);
- } else {
- Node<E> current = head;
- for (int i = 0; i < index - 1 && current != null; i++) {
- current = current.next;
- }
- current.setNext(new Node<E>(element, current.next));
- }
- size++;
- }
- //REMOVE by index METHOD
- @Override
- public E remove(int index) {
- if (index < 0 || index >= size()) {
- throw new IndexOutOfBoundsException("Out of bounds");
- }
- Iterator<E> iterator = this.iterator();
- for (int j = 0; j < index; j++) {
- iterator.next();
- }
- E element = iterator.next();
- iterator.remove();
- return element;
- }
- //REMOVE element
- @Override
- public boolean remove(E element) {
- Iterator<E> iterator = this.iterator();
- while(iterator.hasNext()){
- E e = iterator.next();
- if(element==null ? e==null : element.equals(e)){
- iterator.remove();
- return true;
- }
- }
- return false;
- }
- @Before
- public void setUp() {
- list.add("First");
- list.add("Second");
- list.add("Third");
- list.add("Fourth");
- list.add("Fifth");
- }
- @Test
- public void testSimpleMethodsOnDefaultList() {
- assertEquals(5, list.size());
- assertEquals("First", list.get(0));
- assertEquals("Third", list.get(2));
- assertEquals("Fifth", list.get(4));
- assertEquals("[First, Second, Third, Fourth, Fifth]", list.toString());
- list.add("Second");
- assertEquals(6, list.size());
- assertEquals("Second", list.get(5));
- }
- @Test
- public void testRemoveWithIndex() {
- printList();
- assertEquals("Third", list.remove(2));
- assertEquals(4, list.size());
- assertEquals("Second", list.get(1));
- assertEquals("Fourth", list.get(2));
- assertEquals("First", list.remove(0));
- assertEquals(3, list.size());
- assertEquals("Second", list.get(0));
- System.out.println();
- for(String s : list){
- System.out.print(s + ", ");
- }
- System.out.println();
- System.out.println(list.get(0) + ", " + list.get(1) + ", " + list.get(2));
- assertEquals("Fifth", list.remove(2));
- assertEquals(2, list.size());
- System.out.println(list.get(0) + ", " + list.get(1) + ", " + list.size());
- assertEquals("Fourth", list.get(1));
- }
- Node<E> currentLink = head;
- Node<E> previousLink = head;
- for(int i = 0; i < index; i++){
- if(currentLink.next == null){
- return null;
- } else {
- previousLink = currentLink;
- currentLink = currentLink.next;
- }
- }
- if(currentLink == head){
- head = head.next;
- } else {
- previousLink.next = currentLink.next;
- }
- size--;
- return currentLink.data;
- public String toString() {
- if (head != null) {
- Iterator<E> iterator = this.iterator();
- String storeData = "[";
- while (iterator.hasNext()) {
- E e = iterator.next();
- if (iterator.hasNext() == false) {
- return storeData += e + "]";
- }
- storeData += e + ", ";
- }
- return storeData + "]";
- }
- return "[]";
- }
Add Comment
Please, Sign In to add comment