ahmed19981973

Untitled

Feb 3rd, 2019
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.45 KB | None | 0 0
  1. import datastructure.LinkedList.arrayedStack.Display;
  2. import lists.ListInter;
  3. public class DoubleLinkedList<T>
  4.         implements ListInter<T> {
  5.     protected   Node<T> current = null;
  6.     protected Node<T> first = null;
  7.     protected int currentPos = 0;
  8.     protected int size = 0;
  9.     protected class Node<T> {
  10.         public T entery;
  11.         public Node<T> next;
  12.         public Node<T> before;
  13.         public Node() {
  14.  
  15.         }
  16.  
  17.         public Node(T entery) {
  18.             this();
  19.             this.entery = entery;
  20.         }
  21.  
  22.         public Node(T entery, Node<T> next) {
  23.             this(entery);
  24.             this.next = next;
  25.  
  26.         }
  27.  
  28.         public Node(T entery, Node<T> next, Node<T> before) {
  29.             this(entery, next);
  30.             this.before = before;
  31.         }
  32.     }
  33.  
  34.     public DoubleLinkedList() {
  35.  
  36.     }
  37.  
  38.     /**
  39.      * p begin from 0 to 9 system
  40.      * reach before off place "p"
  41.      * @param p should be integer number from 1 to size-1
  42.      *          it mean it should be from second element to last element
  43.      *          take care p must be <= size -1 && >=1
  44.      *          it throws OutBoundException if "p" does not satisfy conditions above
  45.      *          you should not call it for reaching  to (p= size-1 ) as "reachLast" is more efficient for that
  46.      * @post:the pointer "current" move to "p-1" place and "currentPos" equal p-1(before p)
  47.      *          and current go ahead or go back so currentPos get increased or get decreased or remain if p=currentPos+1
  48.      */
  49.     protected void reachBefore (int p){
  50.         if (p<1||p>size-1)throw new  IndexOutOfBoundsException("index "+p+" is not true index should be at level [1,size-1]");
  51.         final int MAX_LOOP=--p-currentPos;// --p to reach p place now
  52.         int i = 0;
  53.         if (MAX_LOOP>0)
  54.             for (; i < MAX_LOOP; i++)
  55.                 current=current.next;
  56.             else
  57.             for (; i < Math.abs(MAX_LOOP); i++) {
  58.                 current = current.before;
  59.             }
  60.             currentPos = p;
  61.     }
  62.     protected void reachFirst(){
  63.         current=first;
  64.         currentPos=0;
  65.     }
  66.     protected void reachLast(){
  67.         current =first.before;
  68.         currentPos=size-1;
  69.     }
  70.  
  71.     public boolean listEmpty() {
  72.         return size == 0;
  73.     }
  74.  
  75.     public boolean listFull() {
  76.         return false;
  77.     }
  78.  
  79.     public int listSize() {
  80.         return size;
  81.     }
  82.  
  83.     @Override
  84.     public boolean destroyList() {
  85.         return false;
  86.     }
  87.  
  88.     @Override
  89.     public void insertList(T element, int p) {
  90.         if (p==0) {
  91.            push(element);
  92.            return;
  93.        }
  94.        else if (p==size) {
  95.             reachLast();
  96.         }
  97.        else
  98.            reachBefore(p);
  99.        connect(element);
  100.     }
  101.     protected void connect(T element){
  102.         Node<T> node=new Node<>(element);
  103.         node.before=current;
  104.         node.next=current.next;
  105.         current.next.before=node;
  106.         current.next=node;
  107.         size++;
  108.     }
  109.     public  void push(T element){
  110.         reachFirst();
  111.         Node<T> node=new Node<>(element);
  112.         first=node;
  113.         if (current==null) {
  114.             current = first;
  115.             current.next=node;
  116.         }
  117.         else {
  118.             node.next = current;
  119.             node.before = current.before;
  120.             current.before.next = node;
  121.         }
  122.             current.before = node;
  123.             size++;
  124.     }
  125.     public void append(T element){
  126.          reachLast();
  127.          connect(element);
  128.     }
  129.  
  130.     public void deleteList(int p) {
  131.         if (p==0) {
  132.             reachFirst();
  133.             destroyAndConnect(current);
  134.         }
  135.         else if(p==size-1) {
  136.             reachLast();
  137.             destroyAndConnect(current);
  138.         }
  139.         else {
  140.             reachBefore(p);
  141.             destroyAndConnect(current.next);
  142.         }
  143.     }
  144.     public T pop(){
  145.         reachLast();
  146.         T element=first.before.entery;
  147.         return element;
  148.     }
  149.     public  T serve(){
  150.        reachFirst();
  151.        T rt=first.entery;
  152.        return rt;
  153.     }
  154.  
  155.     @Override
  156.     public T retreiveList(int p) {
  157.         if (p==0)
  158.           return   serve();
  159.         else if (p==size-1)
  160.           return   pop();
  161.         else{
  162.             reachBefore(p);
  163.             return current.next.entery;
  164.         }
  165.     }
  166.  
  167.     /**
  168.      * destroy element the current point at
  169.      * this method make all the node content point to null to help GC
  170.      * current must not refer to null (it's empty)
  171.      * else it cause NullPointerException Error
  172.      */
  173.     private void destroyAndConnect(Node<T>pn){
  174.       current=pn.next;
  175.       ++currentPos;
  176.       current.before=pn.before;
  177.       pn.before.next=current;
  178.       if (pn==first) {
  179.           first = current;
  180.           currentPos--;
  181.       }
  182.       pn.next=null;
  183.       pn.entery=null;
  184.       pn.before=null;
  185.       size--;
  186.     }
  187.  
  188.     @Override
  189.     public boolean replaceList(T newValue, int p) {
  190.         if (p==0) {
  191.             reachFirst();
  192.             current.entery=newValue;
  193.         }
  194.         else if (p==size-1) {
  195.             reachLast();
  196.             current.entery=newValue;
  197.         }
  198.         else {
  199.             reachBefore(p);
  200.             current.next.entery=newValue;
  201.         }
  202.         return true ;
  203.     }
  204.     @Override
  205.     public void traverseList(Display<T> d) {
  206.         Node<T> pq=first;
  207.         for (int i=0;i<size;i++,pq=pq.next){
  208.             d.display(pq.entery);
  209.         }
  210.     }
  211.  
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment