Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.NoSuchElementException;
- public class DoublyLinkedList<E>
- {
- public class DNode
- {
- E data;
- DNode forward;
- DNode back;
- }
- private DNode head;
- private DNode tail;
- private int n;
- public DoublyLinkedList()
- {
- head = null;
- tail = null;
- n = 0;
- }
- public boolean isEmpty()
- {
- return (head == null);
- }
- public int size()
- {
- return n;
- }
- public void insertAtFront(E insert)
- {
- DNode newHead = new DNode();
- newHead.data = insert;
- if (this.isEmpty())
- {
- newHead.forward = null;
- newHead.back = null;
- head = newHead;
- tail = newHead;
- }
- else
- {
- head.back = newHead;
- newHead.forward = head;
- newHead.back = null;
- head = newHead;
- }
- n++;
- }
- public void insertAtEnd(E insert)
- {
- DNode newTail = new DNode();
- newTail.data = insert;
- newTail.forward = null;
- if (this.isEmpty())
- {
- newTail.back = null;
- head = newTail;
- tail = newTail;
- }
- else
- {
- newTail.back = tail;
- tail.forward = newTail;
- tail = newTail;
- }
- n++;
- }
- public E removeFront()
- {
- if (this.isEmpty())
- throw new NoSuchElementException();
- E removedData = head.data;
- head = head.forward;
- if (head != null)
- {
- head.back = null;
- }
- n--;
- return removedData;
- }
- public E removeEnd()
- {
- if (this.isEmpty())
- throw new NoSuchElementException();
- E removedData = tail.data;
- tail = tail.back;
- if (tail != null)
- {
- tail.forward = null;
- }
- n--;
- return removedData;
- }
- public void insertBefore(E target, E insert)
- {
- DNode temp;
- for (temp = head; temp != null; temp = temp.forward)
- {
- if (temp.data.equals(target))
- {
- if (temp == head)
- {
- this.insertAtFront(insert);
- }
- else
- {
- DNode newNode = new DNode();
- newNode.data = insert;
- newNode.forward = temp;
- newNode.back = temp.back;
- temp.back.forward = newNode;
- temp.back = newNode;
- }
- break;
- }
- }
- }
- public void insertAfter(E target, E insert)
- {
- DNode temp;
- for (temp = head; temp != null; temp = temp.forward)
- {
- if (temp.data.equals(target))
- {
- if (temp == tail)
- {
- this.insertAtEnd(insert);
- }
- else
- {
- DNode newNode = new DNode();
- newNode.data = insert;
- newNode.forward = temp.forward;
- newNode.back = temp;
- temp.forward.back = newNode;
- temp.forward = newNode;
- }
- break;
- }
- }
- return;
- }
- public void remove(E target)
- {
- DNode temp;
- for (temp = head; temp.forward != null; temp = temp.forward)
- {
- if (temp.data.equals(target))
- {
- if (temp == head)
- {
- this.removeFront();
- }
- else if (temp == tail)
- {
- this.removeEnd();
- }
- else
- {
- temp.back.forward = temp.forward;
- temp.forward.back = temp.back;
- temp = null;
- break;
- }
- }
- }
- return;
- }
- }
- import java.util.NoSuchElementException;
- public class Deque<E>
- {
- private DoublyLinkedList<E> myDeque;
- public Deque()
- {
- myDeque = new DoublyLinkedList<>();
- }
- public boolean isEmpty()
- {
- return (myDeque.isEmpty());
- }
- public int size()
- {
- return (myDeque.size());
- }
- public void pushLeft(E insert)
- {
- myDeque.insertAtEnd(insert);
- }
- public void pushRight(E insert)
- {
- myDeque.insertAtFront(insert);
- }
- public E popLeft()
- {
- E removedData = myDeque.removeEnd();
- return removedData;
- }
- public E popRight()
- {
- E removedData = myDeque.removeFront();
- return removedData;
- }
- }
- public class DequeClient
- {
- public static void main(String[] args)
- {
- Deque<Integer> deque = new Deque<>();
- System.out.println("Is empty? " + deque.isEmpty() + "\t Size: " + deque.size());
- deque.pushLeft(8);
- deque.pushLeft(10);
- System.out.println("Size now: " + deque.size() + "\nFront element: " + deque.popRight() + "\t Size: " + deque.size());;
- deque.pushRight(12);
- deque.pushRight(13);
- System.out.println("Size now: " + deque.size() + "\nBack element: " + deque.popLeft() + "\t Size: " + deque.size());
- System.out.println("Size now: " + deque.size() + "\nFront element: " + deque.popRight() + "\t Size: " + deque.size());
- System.out.println("Is empty? " + deque.isEmpty());
- // NullPointerException in the following command!
- System.out.println("Front element: " + deque.popRight());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement