Thread: Node List Implementation

Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31
  1. #1 Node List Implementation 
    Banned

    Join Date
    Jul 2008
    Age
    26
    Posts
    5,826
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    I made this to kinda help me understand more of how a LinkedList works. Didn't look at JDK source. Ran out of time so I didn't create Iterator generating methods. Half the shit probably doesn't work to par so yeah
    Code:
    package impl;
    
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.List;
    import java.util.ListIterator;
    
    class NodeList<E> implements List<E> {
    
        public NodeList() {
            reset();
        }
    
        @Override
        public boolean isEmpty() {
            return size == 0;
        }
    
        @Override
        public boolean containsAll(Collection<?> c) {
    
            Iterator i = c.iterator();
            while(i.hasNext()) {
                if(!contains(i.next())) {
                    return false;
                }
            }
            return true;
        }
    
        @Override
        public boolean contains(Object o) {
    
            Node<E> current = first;
            for(int i = 0; i < size; i++) {
                if(current.value.equals(o)) {
                    return true;
                }
                current = current.next;
            }
            return false;
        }
    
        @Override
        public boolean addAll(Collection<? extends E> c) {
            return addAll(0, c);
        }
    
        @Override
        public boolean addAll(int index, Collection<? extends E> c) {
            int curr = index;
            Iterator i = c.iterator();
            while(i.hasNext()) {
                add(curr++, (E)i.next());
            }
            return true;
        }
    
        @Override
        public boolean removeAll(Collection<?> c) {
    
            boolean changed = false;
            Iterator it = c.iterator();
            while (it.hasNext()) {
                if (remove((E) it.next())) {
                    changed = true;
                }
            }
            return changed;
        }
    
        @Override
        public boolean retainAll(Collection<?> c) {
            Object[] collection = c.toArray();
            Object[] test = toArray();
            Object[] kept = new Object[c.size()];
            int index = 0;
            for (Object o1 : test) {
                for (Object o2 : collection) {
                    if (o2.equals(o1)) {
                        kept[index++] = o1;
                    }
                }
            }
            clear();
            for (Object o : kept) {
                add((E) o);
            }
            return true;
        }
    
        @Override
        public Object[] toArray() {
            Object[] array = new Object[size];
            Node<E> current = first;
            for (int i = 0; i < size; i++) {
                array[i] = (Object) current.value;
                current = current.next;
            }
            return array;
        }
    
        @Override
        public <T> T[] toArray(T[] type) {
            T[] array = (T[]) new Object[size];
            Node<E> current = first;
            for (int i = 0; i < size; i++) {
                array[i] = (T) current.value;
                current = current.next;
            }
            return array;
        }
    
        private void reset() {
            first = new Node<E>();
            first.next = first;
        }
    
        @Override
        public void clear() {
            reset();
        }
    
        @Override
        public E get(int index) {
    
            Node<E> current = first;
            for (int i = 0; i < index; i++) {
                current = current.next;
            }
            return current.value;
        }
    
        @Override
        public boolean add(E value) {
            if (size >= Integer.MAX_VALUE) {
                return false;
            }
    
            Node<E> newFirst = new Node<E>();
            newFirst.value = value;
            newFirst.next = first;
            first = newFirst;
            ++size;
            return true;
        }
    
        @Override
        public E set(int index, E value) {
            if (index > size) {
                throw new IndexOutOfBoundsException("Index > size");
            }
    
            E old = null;
            Node<E> current = first;
            Node<E> newSlot = new Node<E>();
            for (int i = 0; i < index; i++) {
                if (i == index) {
                    newSlot.value = current.value;
                    current.value = value;
                    newSlot.next = current.next;
                    current.next = newSlot;
                    return old;
                }
                current = current.next;
            }
    
            return null;
        }
    
        @Override
        public void add(int index, E value) {
            set(index, value);
        }
    
        @Override
        public E remove(int index) {
            if (index < 0 || index > size) {
                throw new IndexOutOfBoundsException("Invalid index: " + index);
            }
    
            Node<E> current = first;
            for (int i = 0; i < size - 1; i++) {
                current = current.next;
            }
            E old = current.next.value;
            current.next = current.next.next;
            --size;
            return old;
        }
    
        @Override
        public boolean remove(Object value) {
            Node<E> current = first;
            for (int i = 0; i < size; i++) {
                if (current.value.equals(value)) {
                    current.value = current.next.value;
                    current.next = current.next.next;
                    return true;
                }
            }
            return false;
        }
    
        @Override
        public NodeList<E> subList(int begin, int end) {
            if (begin < 0 || end < 0 || begin > size || end > size || end < begin) {
                throw new IndexOutOfBoundsException("Invalid arguments");
            }
    
            NodeList<E> sub = new NodeList<E>();
            Node<E> current = first;
            for (int i = 0; i <= end; i++) {
    
                if (i >= begin) {
                    sub.add(current.value);
    
                }
    
                current = current.next;
            }
            return sub;
        }
    
        @Override
        public int size() {
            return size;
        }
    
        @Override
        public ListIterator<E> listIterator(int index) {
            return null;
        }
    
        @Override
        public ListIterator<E> listIterator() {
            return null;
        }
    
        @Override
        public Iterator iterator() {
            return null;
        }
    
        @Override
        public int lastIndexOf(Object o) {
    
            int lastIndex = -1;
            Node<E> current = first;
            for (int i = 0; i < size; i++) {
                if (current.value.equals(o)) {
                    lastIndex = i;
                }
                current = current.next;
            }
            return lastIndex;
        }
    
        @Override
        public int indexOf(Object o) {
    
            Node<E> current = first;
            for (int i = 0; i < size; i++) {
                if (current.value.equals(o)) {
                    return i;
                }
                current = current.next;
            }
            return -1;
        }
        private int size;
        private Node<E> first;
    
        private static class Node<E> {
    
            public Node<E> next;
            public E value;
        }
    }
     

  2. #2  
    Renown Programmer
    veer's Avatar
    Join Date
    Nov 2007
    Posts
    3,747
    Thanks given
    354
    Thanks received
    1,368
    Rep Power
    3032
    what the hell is the purpose of THIS?
     

  3. #3  
    Banned

    Join Date
    Jul 2008
    Age
    26
    Posts
    5,826
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    Quote Originally Posted by super_ View Post
    what the hell is the purpose of THIS?
    Some shit I was trying to do with inner classes and I screwed it over, forgot to remove it.
     

  4. #4  
    Renown Programmer
    veer's Avatar
    Join Date
    Nov 2007
    Posts
    3,747
    Thanks given
    354
    Thanks received
    1,368
    Rep Power
    3032
    Quote Originally Posted by Colby View Post
    Some shit I was trying to do with inner classes and I screwed it over, forgot to remove it.
    you realize you can just do Enclosing.this... right?
     

  5. #5  
    Banned

    Join Date
    Jul 2008
    Age
    26
    Posts
    5,826
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    Quote Originally Posted by super_ View Post
    you realize you can just do Enclosing.this... right?
    Yeah thats why I said I fucked it up.
     

  6. #6  
    Banned

    Join Date
    Jul 2008
    Age
    26
    Posts
    5,826
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    benchmarked it, 0.19x faster than LinkedList at add & get operations Too bad it sucks.
     

  7. #7  
    Registered Member Killer 99's Avatar
    Join Date
    Dec 2007
    Posts
    1,484
    Thanks given
    171
    Thanks received
    503
    Rep Power
    414
    so, you rewrote java's LinkedList? why?
     

  8. #8  
    Banned

    Join Date
    Jul 2008
    Age
    26
    Posts
    5,826
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    Quote Originally Posted by Killer 99 View Post
    so, you rewrote java's LinkedList? why?
    Quote Originally Posted by Colby
    I made this to kinda help me understand more of how a LinkedList works.
    mts.
     

  9. #9  
    Renown Programmer
    Method's Avatar
    Join Date
    Feb 2009
    Posts
    1,455
    Thanks given
    0
    Thanks received
    843
    Rep Power
    3019
    indexOf doesn't look like it will work and other methods may be the same way.
    :-)
     

  10. #10  
    Banned

    Join Date
    Jul 2008
    Age
    26
    Posts
    5,826
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    Quote Originally Posted by Method View Post
    indexOf doesn't look like it will work and other methods may be the same way.
    Why not?
     

Page 1 of 4 123 ... LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •