Created
March 18, 2026 18:11
-
-
Save adi928/73baa74a4fa2f33ad1eb7327b5ba03cf to your computer and use it in GitHub Desktop.
Implementing Deque with LL
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
| import java.util.Arrays; | |
| class ListNode { | |
| int val; | |
| ListNode left; | |
| ListNode right; | |
| ListNode(int element) { | |
| val = element; | |
| left = null; | |
| right = null; | |
| } | |
| } | |
| class ArrayDeque2 { | |
| ListNode head; | |
| ListNode tail; | |
| int size; | |
| ArrayDeque2() { | |
| head = new ListNode(Integer.MAX_VALUE); | |
| tail = new ListNode(Integer.MAX_VALUE); | |
| head.right = tail; | |
| tail.left = head; | |
| size = 0; | |
| } | |
| void addFirst(int element) { | |
| ListNode oldRight = head.right; | |
| ListNode newNode = new ListNode(element); | |
| // make connection towards right proper | |
| head.right = newNode; newNode.left = head; | |
| // make connection towards left proper | |
| newNode.right = oldRight; oldRight.left = newNode; | |
| size++; | |
| } | |
| void addLast(int element) { | |
| ListNode oldLeft = tail.left; | |
| ListNode newNode = new ListNode(element); | |
| // make connection towards right proper | |
| tail.left = newNode; newNode.right = tail; | |
| // make connection towards left proper | |
| newNode.left = oldLeft; oldLeft.right = newNode; | |
| size++; | |
| } | |
| boolean isEmpty() { | |
| return head.right == tail && tail.left == head; | |
| } | |
| int removeFirst(){ | |
| if (isEmpty()) throw new IllegalStateException(); | |
| size--; | |
| int ans = head.right.val; | |
| head.right = head.right.right; | |
| head.right.left = head; | |
| return ans; | |
| } | |
| int peekFirst() { | |
| return head.right.val; | |
| } | |
| int peekLast() { | |
| return tail.left.val; | |
| } | |
| int removeLast() { | |
| if (isEmpty()) throw new IllegalStateException(); | |
| size--; | |
| int ans = tail.left.val; | |
| tail.left = tail.left.left; | |
| tail.left.right = tail; | |
| return ans; | |
| } | |
| void print() { | |
| ListNode curr = head.right; | |
| int[] nums = new int[size]; | |
| for (int i=0;i<size;i++) { | |
| nums[i] = curr.val; | |
| curr = curr.right; | |
| } | |
| System.out.println(Arrays.toString(nums)); | |
| } | |
| int getSize() { | |
| return size; | |
| } | |
| } | |
| public class Main { | |
| public static void main(String[] args) { | |
| // Implementing ArrayDeque | |
| // 1. a size of deque | |
| // 2. pointers for first and last. | |
| // 3. | |
| ArrayDeque2 customDeque = new ArrayDeque2(); | |
| customDeque.addFirst(1); | |
| customDeque.addLast(9); | |
| System.out.println(customDeque.isEmpty()); | |
| customDeque.print(); | |
| System.out.println(customDeque.peekLast()); | |
| System.out.println(customDeque.peekFirst()); | |
| customDeque.addFirst(-1); | |
| customDeque.addLast(99); | |
| customDeque.print(); | |
| System.out.println(customDeque.removeLast()); | |
| System.out.println(customDeque.removeLast()); | |
| customDeque.print(); | |
| System.out.println(customDeque.getSize()); | |
| System.out.println(customDeque.removeLast()); | |
| System.out.println(customDeque.removeLast()); | |
| System.out.println(customDeque.isEmpty()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment