korobushk

LinkedListDeque

Apr 4th, 2021 (edited)
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. package ds.cs61b;
  2.  
  3. public class LinkedListDeque<T> {
  4.  
  5.     public class Node {
  6.         public Node prev;
  7.         public Node next;
  8.         public T item;
  9.  
  10.         public Node(T item) {
  11.             this.item = item;
  12.  
  13.         }
  14.  
  15.         public void displayNode() {
  16.             System.out.println("{ " + item + " } ");
  17.         }
  18.     }
  19.  
  20.  
  21.     private Node first;
  22.     private Node last;
  23.  
  24.     private int size;
  25.  
  26.     public LinkedListDeque(T x) {
  27.  
  28.         this.first = null;
  29.         this.last = null;
  30.  
  31.     }
  32.  
  33.     public boolean isEmpty() {
  34.         return first == null;
  35.     }
  36.  
  37.     /**
  38.      * Adds an item to the front of the list.
  39.      */
  40.     public void addFirst(T x) {
  41.  
  42.  
  43.         Node node = new Node(x);
  44.  
  45.         if (isEmpty()) {
  46.             last = node;
  47.         } else {
  48.             first.prev = node;
  49.         }
  50.         node.next = first;
  51.         this.first = node;
  52.         size += 1;
  53.  
  54.     }
  55.  
  56.     public void addLast(T x) {
  57.         Node node = new Node(x);
  58.         if (isEmpty()) {
  59.             first = node;
  60.         } else {
  61.             last.next = node;
  62.         }
  63.         node.prev = last;
  64.         this.last = node;
  65.         size += 1;
  66.  
  67.     }
  68.  
  69.     public T removeFirst() {
  70. //        Node temp = first;
  71.         if (isEmpty()) {
  72.             last = null;
  73.         } else {
  74.             first.next.prev = null;
  75.         }
  76.         first = first.next;
  77.  
  78.         return first.item;
  79.     }
  80.  
  81.     public T removeLast() {
  82. //        Node temp = last;
  83.         if (first.next == null) {
  84.             first = null;
  85.         } else {
  86.             last.prev.next = null;
  87.  
  88.         }
  89.         last = last.prev;
  90.  
  91.         return last.item;
  92.     }
  93.  
  94.     public T get(int index) {
  95.         Node n = first;
  96.         int count = 0;
  97.         while (n.next != null) {
  98.             n = n.next;
  99.             count++;
  100.             if (index == count) {
  101.                 return n.item;
  102.             }
  103.         }
  104.         return null;
  105.     }
  106.  
  107.  
  108.     public T getFirst() {
  109.         return first.item;
  110.     }
  111.  
  112.     public boolean insertAfter(T key, T d) {
  113.         Node current = first; // we start from the beginning of the list
  114.         while (current.item != key) { // as long as we have not found the key in a particular node
  115.             current = current.next;
  116.             if (current == null) {
  117.                 return false;
  118.             }
  119.         }
  120.         Node n = new Node(d);
  121.  
  122.         if (current == last) {
  123.             current.next = null;
  124.             last = n;
  125.         } else {
  126.             n.next = current.next;
  127.  
  128.             current.next.prev = n;
  129.         }
  130.         n.prev = current;
  131.         current.next=n;
  132.         return true;
  133.     }
  134.    
  135.     public int size() {
  136.         return size;
  137.  
  138.     }
  139.  
  140.  
  141.     public void printDeque() {
  142.         System.out.println("List (first --> last) ");
  143.         Node current = first;
  144.         while (current != null) {
  145.             current.displayNode();
  146.             current = current.next;
  147.         }
  148.         System.out.println();
  149.     }
  150. }
  151.  
Add Comment
Please, Sign In to add comment