Monday, May 23, 2016

MyLinkedList : Implemented with Generics


MyLinkedList:


package com.lnn.ds;

import java.util.Objects;

public class MyLinkedList<T> {
      private static class MyLink<T> {
            private MyLink<T> prev;
            private MyLink<T> next;
            private T data;

            public MyLink(T data) {
                  this.data = data;
            }

            public T getData() {
                  return data;
            }

            public MyLink<T> getNext() {
                  return next;
            }

            public MyLink<T> getPrev() {
                  return prev;
            }

            public void setNext(MyLink<T> next) {
                  this.next = next;
            }

            public void setPrev(MyLink<T> prev) {
                  this.prev = prev;
            }
      }

      private MyLink<T> head;

      private MyLink<T> tail;

      public void addFirst(T data) {
            MyLink<T> prev = new MyLink<T>(data);
            prev.setNext(head);
            if (head != null) {
                  head.setPrev(prev);
            }
            head = prev;
            if (tail == null) {
                  tail = prev;
            }
      }

      public void addLast(T data) {
            MyLink<T> next = new MyLink<T>(data);
            next.setPrev(tail);
            if (tail != null) {
                  tail.setNext(next);
            }
            tail = next;
            if (head == null) {
                  head = next;
            }
      }

      public void display() {
            MyLink<T> tmp = head;
            System.out.print("[");
            while (tmp != null) {
                  System.out.print(tmp.getData());
                  tmp = tmp.getNext();
                  if (tmp != null) {
                        System.out.print(", ");
                  }
            }
            System.out.println("]");
      }

      public boolean exists(T data) {
            MyLink<T> tmp = head;
            while (tmp != null) {
                  if (Objects.equals(tmp.getData(), data)) {
                        return true;
                  }
                  tmp = tmp.getNext();
            }
            return false;
      }

      public boolean remove(T data) {
            MyLink<T> tmp = head;
            while (tmp != null) {
                  if (Objects.equals(tmp.getData(), data)) {
                        MyLink<T> prev = tmp.getPrev();
                        MyLink<T> next = tmp.getNext();
                        if (prev != null) {
                              prev.setNext(next);
                        } else {
                              head = next;
                        }
                        if (next != null) {
                              next.setPrev(prev);
                        } else {
                              tail = prev;
                        }
                        return true;
                  }
                  tmp = tmp.getNext();
            }
            return false;
      }

      public boolean removeFirst() {
            if (head != null) {
                  head = head.getNext();
                  if (head != null) {
                        head.setPrev(null);
                  } else {
                        tail = head;
                  }
                  return true;
            }
            return false;
      }

      public boolean removeLast() {
            if (tail != null) {
                  tail = tail.getPrev();
                  if (tail != null) {
                        tail.setNext(null);
                  } else {
                        head = tail;
                  }
                  return true;
            }
            return false;
      }
}

TestMyLinkedList:

package com.lnn.ds;

public class TestMyLinkedList {
      public static void main(String[] args) {
            MyLinkedList<Integer> list = new MyLinkedList<Integer>();
            for (int i = 1; i < 10; i++) {
                  list.addFirst(10 - i);
            }
            for (int i = 10; i < 20; i++) {
                  list.addLast(i);
            }
            list.display();
            list.remove(7);
            list.display();
            list.removeLast();
            list.display();
            list.removeFirst();
            list.display();
            System.out.println(list.exists(9));
      }
}

No comments:

Post a Comment