Implementation of Deque using doubly linked list | Java implementation of Deque using doubly linked list ; Node of a doubly linked list ; Function to get a new node ; A structure to represent a deque ; Function to check whether deque is empty or not ; Function to return the number of elements in the deque ; Function to insert an element at the front end ; If true then new element cannot be added and it is an ' Overflow ' condition ; If deque is empty ; Inserts node at the front end ; Increments count of elements by 1 ; Function to insert an element at the rear end ; If true then new element cannot be added and it is an ' Overflow ' condition ; If deque is empty ; Inserts node at the rear end ; Function to delete the element from the front end ; If deque is empty then ' Underflow ' condition ; Deletes the node from the front end and makes the adjustment in the links ; If only one element was present ; Decrements count of elements by 1 ; Function to delete the element from the rear end ; If deque is empty then ' Underflow ' condition ; Deletes the node from the rear end and makes the adjustment in the links ; If only one element was present ; Decrements count of elements by 1 ; Function to return the element at the front end ; If deque is empty , then returns garbage value ; Function to return the element at the rear end ; If deque is empty , then returns garbage value ; Function to delete all the elements from Deque ; Driver program to test above
import java . util . * ; class GFG { static class Node { int data ; Node prev , next ; static Node getnode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . prev = newNode . next = null ; return newNode ; } } ; static class Deque { Node front ; Node rear ; int Size ; Deque ( ) { front = rear = null ; Size = 0 ; } boolean isEmpty ( ) { return ( front == null ) ; } int size ( ) { return Size ; } void insertFront ( int data ) { Node newNode = Node . getnode ( data ) ; if ( newNode == null ) System . out . print ( "OverFlowNEW_LINE"); else { if ( front == null ) rear = front = newNode ; else { newNode . next = front ; front . prev = newNode ; front = newNode ; } Size ++ ; } } void insertRear ( int data ) { Node newNode = Node . getnode ( data ) ; if ( newNode == null ) System . out . print ( "OverFlowNEW_LINE"); else { if ( rear == null ) front = rear = newNode ; else { newNode . prev = rear ; rear . next = newNode ; rear = newNode ; } Size ++ ; } } void deleteFront ( ) { if ( isEmpty ( ) ) System . out . print ( "UnderFlowNEW_LINE"); else { Node temp = front ; front = front . next ; if ( front == null ) rear = null ; else front . prev = null ; Size -- ; } } void deleteRear ( ) { if ( isEmpty ( ) ) System . out . print ( "UnderFlowNEW_LINE"); else { Node temp = rear ; rear = rear . prev ; if ( rear == null ) front = null ; else rear . next = null ; Size -- ; } } int getFront ( ) { if ( isEmpty ( ) ) return - 1 ; return front . data ; } int getRear ( ) { if ( isEmpty ( ) ) return - 1 ; return rear . data ; } void erase ( ) { rear = null ; while ( front != null ) { Node temp = front ; front = front . next ; } Size = 0 ; } } public static void main ( String [ ] args ) { Deque dq = new Deque ( ) ; System . out . print ( "Insert element '5' at rear endNEW_LINE"); dq . insertRear ( 5 ) ; System . out . print ( "Insert element '10' at rear endNEW_LINE"); dq . insertRear ( 10 ) ; System . out . print ( " Rear ▁ end ▁ element : ▁ " + dq . getRear ( ) + "NEW_LINE"); dq . deleteRear ( ) ; System . out . print ( " After ▁ deleting ▁ rear ▁ element ▁ new ▁ rear " + " ▁ is : ▁ " + dq . getRear ( ) + "NEW_LINE"); System . out . print ( "Inserting element '15' at front end NEW_LINE"); dq . insertFront ( 15 ) ; System . out . print ( " Front ▁ end ▁ element : ▁ " + dq . getFront ( ) + "NEW_LINE"); System . out . print ( " Number ▁ of ▁ elements ▁ in ▁ Deque : ▁ " + dq . size ( ) + "NEW_LINE"); dq . deleteFront ( ) ; System . out . print ( " After ▁ deleting ▁ front ▁ element ▁ new ▁ " + " front ▁ is : ▁ " + dq . getFront ( ) + "NEW_LINE"); } }