package com.tasks3.linkedlist;


class Node<T>
{
	private Node<T> next;
	private Node<T> prev;
	private T data;
	
	public Node(T data, Node<T> prev, Node<T> next)
	{
		this.data = data;
		this.prev = prev;
		this.next = next;
	}

	public Node<T> getPrev()
	{
		return this.prev;
	}
	
	public void setPrev(Node<T> prev)
	{
		this.prev = prev;
	}
	
	public Node<T> getNext()
	{
		return this.next;
	}
	
	public void setNext(Node<T> next)
	{
		this.next = next;
	}
	
	public T getData()
	{
		return this.data;
	}
	
	public void setData(T data)
	{
		this.data = data;
	}
}


public class LinkedList<T>
{
	private Node<T> first;
	private Node<T> last;
	private int size = 0;
	
	@SafeVarargs
	public LinkedList(T ... args)
	{
		if (args.length > 0)
		{
			for (T arg: args)
			{
				this.add(arg);
			}
		}
	}

	public LinkedList<T> add(T data)
	{
		Node<T> insert = new Node<T>(data, this.last, null);
		
		if (this.first == null && this.last == null)
		{
			this.first = insert;
			this.last = insert;
		}
		else
		{
			this.last.setNext(insert);
			this.last = insert;
		}
		
		this.size++;
		
		return this;
	}
 
	public T get(int index)
	{
		if (index < 0 || index >= this.size)
		{
			return null;
		}
		
		Node<T> elem;
		
		if (index > this.size / 2)
		{
			elem = this.last;
			
			for (int i = index + 1; i < this.size; i++)
			{
				elem = elem.getPrev();
			}
		}
		else
		{
			elem = this.first;
			
			for (int i = 0; i < index; i++)
			{
				elem = elem.getNext();
			}
		}
		
		return elem.getData();
	}

	public boolean delete(int index)
	{
		if (index < 0 || index >= this.size)
		{
			return false;
		}
		
		if (index == 0)
		{
			this.first.getNext().setPrev(null);
			this.first = this.first.getNext();
		}
		else if (index == this.size - 1)
		{
			this.last.getPrev().setNext(null);
			this.last = this.last.getPrev();
		}
		else
		{
			Node<T> elem, prevElem;
			
			if (index > this.size / 2)
			{
				elem = this.last;
				prevElem = this.last.getPrev();
				
				for (int i = index + 1; i < this.size; i++)
				{
					elem = prevElem;
					prevElem = prevElem.getPrev();
				}
			}
			else
			{
				elem = this.first;
				
				for (int i = 0; i < index; i++)
				{
					prevElem = elem;
					elem = elem.getNext();
				}
			}
			
			elem.getNext().setPrev(elem.getPrev());
			elem.getPrev().setNext(elem.getNext());
		}
		
		this.size--;
		
		return true;
	}

	public int size()
	{
		return this.size;
	}
	
	public int indexOf(T value)
	{
		int index = -1;
		Node<T> elem = this.first;
		
		for (int i = 0; i < this.size; i++)
		{
			if (elem.getData().equals(value))
			{
				index = i;
				break;
			}
			
			elem = elem.getNext();
		}
		
		return index;
	}
	
	@Override
	public String toString()
	{
		if (this.first != null)
		{
			StringBuilder str = new StringBuilder("[ " + this.first.getData());
			
			Node<T> elem = this.first.getNext();
			for (int i = 1; i < this.size; i++)
			{
				str.append(", " + elem.getData());
				elem = elem.getNext();
			}
			
			str.append(" ]");
			
			return str.toString();
		}
		
		return "[ ]";
	}
}

@@ -1520,15 +1520,9 @@
 lic 
-boolean
+T
  del
@@ -1596,18 +1596,39 @@
 urn 
-false;%0A%09%09%7D
+null;%0A%09%09%7D%0A%09%09%0A%09%09Node%3CT%3E removed;
 %0A%09%09%0A
@@ -1680,32 +1680,57 @@
 .setPrev(null);%0A
+%09%09%09removed = this.first;%0A
 %09%09%09this.first = 
@@ -1832,16 +1832,40 @@
 (null);%0A
+%09%09%09removed = this.last;%0A
 %09%09%09this.
@@ -2393,16 +2393,35 @@
 ext());%0A
+%09%09%09removed = elem;%0A
 %09%09%7D%0A%09%09%0A%09
@@ -2450,12 +2450,25 @@
 urn 
-true
+removed.getData()
 ;%0A%09%7D

Return value of 'delete' method was changed