Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package list;
- import list.List;
- import list.Node;
- /** An implementation of List USING REFERENCES */
- public class LinkedList<E> implements List<E> {
- int size = 0; //LinkedLists general starting size
- Node<E> head;
- Node<E> tail;
- private Node<E> ref;
- private void setRef(int index)
- {
- ref = head.next;
- for(int i=0;i<index;i++)
- ref = ref.next;
- }
- public LinkedList()
- {
- head = new Node<E>(null, null, null);
- tail = new Node<E>(null, null, head); //new stuff
- head.next = tail;
- }
- public void add(E value)
- {
- Node<E> temp = new Node<E>(value, tail, tail.prev); //creating new Node (needs value and next fields)
- tail.prev.next = temp; //putting reference to new Node in temp
- tail.prev = temp;
- size++; //added element successfully, increment size
- }
- public void add(int index, E value)
- {
- //Move to position (index-1)
- setRef(index);
- Node<E> temp = new Node<E>(value, ref, ref.prev); //new node new params
- ref.prev.next = temp;
- ref.prev = temp;
- size++;
- }
- public E get(int index) //getting value in List at positon index
- {
- setRef(index);
- return ref.value; //in this node, we want the field (value)
- } //ref variable is gone, gets cleared
- public E set(int index, E value)
- {
- setRef(index);
- E result = ref.value;
- ref.value = value; //asign parameter value from new value by elminating old one
- return result;
- }
- public E remove(int index) //gets you to this position
- {
- setRef(index);
- ref.prev.next = ref.next; //asigned from ref.next
- ref.next.prev = ref.prev;
- size--;
- return ref.value;
- }
- public int size()
- {
- return this.size;
- }
- public boolean isEmpty()
- {
- for (int i=0;i<size;)
- if ((head.next == tail))
- {
- return false;
- }
- else
- {
- return false;
- }
- return true;
- }
- public void clear()
- {
- for(int i=0;i<size;i++)
- remove(i);
- size = 0;
- }
- public indexOf(Object)
- {
- //
- }
- public contains(Object)
- {
- }
- public toString()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement