Last active
August 8, 2017 06:09
-
-
Save pureexe/f8550a3d7017b8d3619c29d806c65253 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @author Pakkapon Phongthawee (07580028) | |
| * @license GPLv3 | |
| */ | |
| public class MyList<Type> { | |
| private class Node{ | |
| public Type data; | |
| public Node prev,next; | |
| Node(Type d){ | |
| data = d; | |
| } | |
| } | |
| private Node front,rear; | |
| private int length; | |
| MyList(){ | |
| } | |
| /** | |
| * append data to list | |
| * @param data | |
| * @return MyListObject | |
| */ | |
| public MyList unshift(Type data) | |
| { | |
| length++; | |
| Node node = new Node(data); | |
| try{ | |
| front.prev = node; | |
| node.next = front; | |
| }catch(NullPointerException e){ | |
| rear = node; | |
| } | |
| front = node; | |
| return this; | |
| } | |
| /** | |
| * get data from front of list | |
| * @return dataAtFrontOfList | |
| */ | |
| public Type shift() | |
| { | |
| Type output; | |
| try{ | |
| output = front.data; | |
| }catch(NullPointerException e){ | |
| return null; | |
| } | |
| length--; | |
| try{ | |
| front = front.next; | |
| front.prev = null; | |
| }catch(NullPointerException e){ | |
| rear = null; | |
| } | |
| return null; | |
| } | |
| /** | |
| * append data to list | |
| * @param data | |
| * @return MyListObject | |
| */ | |
| public MyList push(Type data){ | |
| length++; | |
| Node node = new Node(data); | |
| try{ | |
| rear.next = node; | |
| node.prev = rear; | |
| }catch(NullPointerException e){ | |
| front = node; | |
| } | |
| rear = node; | |
| return this; | |
| } | |
| /** | |
| * get data from rear of list | |
| * @return dataAtFrontOfList | |
| */ | |
| public Type pop(){ | |
| Type output; | |
| try{ | |
| output = rear.data; | |
| }catch(NullPointerException e){ | |
| return null; | |
| } | |
| length--; | |
| try{ | |
| rear = rear.prev; | |
| rear.next = null; | |
| }catch(NullPointerException e){ | |
| front = null; | |
| } | |
| return output; | |
| } | |
| /** | |
| * get data at specify position of list | |
| * Note: first data start at 0 | |
| * @param position | |
| * @return | |
| */ | |
| public Type at(int position) | |
| { | |
| if(position < 0){ | |
| throw new IndexOutOfBoundsException(); | |
| } | |
| Node traveller = front; | |
| int i = 0; | |
| try{ | |
| while(i<position){ | |
| traveller = traveller.next; | |
| } | |
| }catch(NullPointerException e){ | |
| throw new IndexOutOfBoundsException(); | |
| } | |
| return traveller.data; | |
| } | |
| /** | |
| * get data at front of list | |
| * @return data | |
| */ | |
| public Type front() | |
| { | |
| return front.data; | |
| } | |
| /** | |
| * get data at rear of list | |
| * @return data | |
| */ | |
| public Type rear() | |
| { | |
| return rear.data; | |
| } | |
| /** | |
| * Get List length | |
| * @return ListLength | |
| **/ | |
| public int size() | |
| { | |
| return length; | |
| } | |
| /** | |
| * convert MyList to Java ordinary array | |
| * @return ArrayOfMyList | |
| */ | |
| public Object[] toArray() | |
| { | |
| Object[] arrays = new Object[length]; | |
| int i = 0; | |
| Node traveller = front; | |
| while(traveller != null){ | |
| arrays[i++] = traveller.data; | |
| traveller = traveller.next; | |
| } | |
| return arrays; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment