desislava_topuzakova

Workshop - DoublyLinkedList

Jun 10th, 2021
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. import java.util.function.Consumer;
  2.  
  3. public class DoublyLinkedList {
  4.     private class Node { //всеки елемент от моя свързан списък
  5.         //текущ елемент, следващия елемент, предишния елемент
  6.         private int element;
  7.         private Node next;
  8.         private Node prev;
  9.  
  10.         private Node(int element) {
  11.             this.element = element;
  12.             //next = null;
  13.             //prev = null;
  14.         }
  15.     }
  16.     //глава, опашка, размер
  17.     private Node head;
  18.     private Node tail;
  19.     private int size;
  20.  
  21.  
  22.     //методи
  23.     public void addFirst(int element) {
  24.         //добавям в началото (първи елемент) => глава
  25.         Node newNode = new Node(element);
  26.         if(this.size == 0) {
  27.             this.head = this.tail = newNode;
  28.         } else {
  29.             newNode.next = this.head;
  30.             this.head.prev = newNode;
  31.             this.head = newNode;
  32.         }
  33.         this.size++;
  34.     }
  35.  
  36.     public void addLast(int element){
  37.         //добавя елемент в края (последен елемент) => опашка
  38.         Node newNode = new Node(element);
  39.         if(this.size == 0) {
  40.             this.head = this.tail = newNode;
  41.         } else {
  42.             this.tail.next = newNode;
  43.             newNode.prev = this.tail;
  44.             this.tail = newNode;
  45.         }
  46.         this.size++;
  47.     }
  48.  
  49.     public int removeFirst() throws Exception {
  50.         //премахва първия елемент (глава)
  51.         if(this.size == 0) {
  52.             throw new Exception("Cannot remove element from collection with size 0.");
  53.         } else if (this.size == 1) {
  54.             Node firstElement = this.head;
  55.             this.head = null;
  56.             this.tail = null;
  57.             this.size = 0;
  58.             return firstElement.element;
  59.         } else {
  60.             Node firstElement = this.head;
  61.             this.head = this.head.next;
  62.             this.head.prev = null;
  63.             this.size--;
  64.             return firstElement.element;
  65.         }
  66.     }
  67.  
  68.     public int removeLast() throws Exception {
  69.         if(this.size == 0) {
  70.             throw new Exception("Cannot remove element from collection with size 0.");
  71.         } else if (this.size == 1) {
  72.             Node removedElement = this.head;
  73.             this.head = null;
  74.             this.tail = null;
  75.             this.size = 0;
  76.             return removedElement.element;
  77.         } else {
  78.             Node removedElement = this.tail;
  79.             this.tail = this.tail.prev;
  80.             this.tail.next = null;
  81.             this.size--;
  82.             return removedElement.element;
  83.         }
  84.     }
  85.  
  86.     public void forEach(Consumer<Integer> consumer) {
  87.         Node currentNode = this.head;
  88.         while(currentNode != null) {
  89.             consumer.accept(currentNode.element);
  90.             currentNode = currentNode.next;
  91.         }
  92.     }
  93.  
  94.     public int[] toArray() {
  95.         int[] array = new int[this.size];
  96.         Node currentNode = this.head;
  97.         int index = 0;
  98.         while(currentNode != null) {
  99.             array[index] = currentNode.element;
  100.             index++;
  101.             currentNode = currentNode.next;
  102.         }
  103.         return array;
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment