Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Iterator;
- public class LinkedList<T> implements Iterable<T>{
- //Construct List/////////////////////////////////////////////////
- private Node Head;
- public LinkedList(){
- Head = new Node(null,null,null);
- Head.next = Head;
- Head.prev = Head;
- }
- /////////////////////////////////////////////////////////////////
- //Append Method//////////////////////////////////////////////////
- public void append(T _add){
- Node cur = Head.prev;
- cur.next = new Node(Head,cur,_add);
- Head.prev = cur.next;
- }
- /////////////////////////////////////////////////////////////////
- //lenght/////////////////////////////////////////////////////////
- public int length(){
- int count = 0;
- Node cur = Head;
- while (cur.next != Head){
- cur = cur.next;
- count++;
- }
- return count;
- }
- /////////////////////////////////////////////////////////////////
- public int find(T _find){
- Node cur = Head.next;
- int i = 0;
- while (cur.data != null){
- if (cur.data.equals(_find)){
- return i;
- }
- i++;
- cur = cur.next;
- }
- return -1;
- }
- //index//////////////////////////////////////////////////////////
- public T index(int _index){
- Node cur = Head.next;
- int index = _index;
- while (index > 0){
- cur = cur.next;
- index--;
- }
- return cur.data;
- }
- /////////////////////////////////////////////////////////////////
- //toString///////////////////////////////////////////////////////
- public String toString(){
- return ":LinkedList:<["+format(Head.next)+"]>";
- }
- private String format(Node _cur){
- if (_cur.data == null){
- return "";
- }
- else{
- return _cur.data.toString()+","+format(_cur.next);
- }
- }
- /////////////////////////////////////////////////////////////////
- public Iterator<T> iterator(){
- return new Itr();
- }
- private class Itr implements Iterator<T> {
- Node cur = Head;
- public boolean hasNext(){
- return cur.next != Head;
- }
- public T next(){
- cur = cur.next;
- return cur.data;
- }
- }
- //Node Class/////////////////////////////////////////////////////
- class Node{
- Node next;
- Node prev;
- T data;
- Node(Node _next, Node _prev, T _data){
- next = _next;
- prev = _prev;
- data = _data;
- }
- }
- /////////////////////////////////////////////////////////////////
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement