Java Program to implement Array Deque

«
»
This is a Java Program to implement Array Deque. Deque is an abstract data type that generalizes a queue, for which elements can be added to or removed from either the front (head) or back (tail).

Here is the source code of the Java Program to implement Array Deque. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  **  Java Program to implement Array Deque
  3.  **/
  4.  
  5. import java.util.Scanner;
  6.  
  7. /** Class ArrayDeque **/
  8. class ArrayDeque
  9. {
  10.     private int[] a;
  11.     private int j, n;
  12.  
  13.     /** constructor **/
  14.     public ArrayDeque()
  15.     {
  16.         j = 0;
  17.         n = 0;
  18.         resize();        
  19.     }
  20.  
  21.     /** function to check if empty **/
  22.     public boolean isEmpty()
  23.     {
  24.         return n == 0;
  25.     }
  26.  
  27.     /** function to clear array deque **/
  28.     public void clear()
  29.     {
  30.         j = 0;
  31.         n = 0;
  32.         resize();
  33.     }
  34.  
  35.     /** function to get number of elements **/
  36.     public int getSize()
  37.     {
  38.         return n;
  39.     }
  40.  
  41.     /** function to resize array deque **/
  42.     private void resize() 
  43.     {
  44.         int[] temp = new int[Math.max(2 * n, 1)];
  45.         for (int k = 0; k < n; k++) 
  46.             temp[k] = a[(j + k) % a.length];
  47.         a = temp;
  48.         j = 0;
  49.     }
  50.  
  51.     /** function to get an element array deque **/
  52.     public int get(int i)
  53.     {
  54.         return a[(j + i) % a.length];
  55.     }
  56.  
  57.     /** function to set an element **/
  58.     public int set(int i, int x) 
  59.     {
  60.         int y = a[(j + i) % a.length];
  61.         a[(j + i) % a.length] = x;
  62.         return y;
  63.     }
  64.  
  65.     /** function to add an element **/
  66.     void add(int i, int x) 
  67.     {
  68.         if (n + 1 > a.length)  
  69.             resize();
  70.         if (i < n/2) 
  71.         { 
  72.               /** shift left one position **/
  73.               j = (j == 0) ? a.length - 1 : j - 1;
  74.  
  75.               for (int k = 0; k <= i - 1; k++)
  76.                   a[(j + k) % a.length] = a[(j + k + 1)%a.length];
  77.         } 
  78.         else 
  79.         { 
  80.             /** shift right one position **/
  81.             for (int k = n; k > i; k--)
  82.                 a[(j + k) % a.length] = a[(j + k - 1)%a.length];
  83.         }
  84.         a[(j + i) % a.length] = x;
  85.         n++;
  86.     }
  87.  
  88.     /** function to remove an element **/
  89.     public int remove(int i) 
  90.     {
  91.         int x = a[(j + i) % a.length];
  92.         if (i < n/2) 
  93.         { 
  94.             /** shift a[0],..,[i-1] right one position **/
  95.             for (int k = i; k > 0; k--)
  96.                 a[(j + k) % a.length] = a[(j + k - 1) % a.length];
  97.  
  98.             j = (j + 1) % a.length;
  99.         }
  100.         else 
  101.         { 
  102.             /** shift a[i+1],..,a[n-1] left one position **/
  103.             for (int k = i; k < n - 1; k++)
  104.                 a[(j + k) % a.length] = a[(j + k + 1) % a.length];
  105.         }
  106.         n--;
  107.         if (3 * n < a.length) 
  108.             resize();
  109.         return x;
  110.     }
  111.  
  112.     /** function display array deque **/
  113.     public void display()
  114.     {
  115.         System.out.print("\nArray Deque : ");
  116.         int p = j;
  117.         for (int i = 0; i < n; i++)
  118.         {
  119.             System.out.print(a[p % a.length] +" ");
  120.             p++;
  121.         }
  122.         System.out.println();
  123.     }    
  124. }
  125.  
  126. /** Class ArrayDequeTest **/
  127. public class ArrayDequeTest
  128. {
  129.     public static void main(String[] args) 
  130.     {
  131.         Scanner scan = new Scanner(System.in);
  132.         System.out.println("Array Deque Test\n");
  133.  
  134.         ArrayDeque ad = new ArrayDeque();
  135.  
  136.         char ch;
  137.         /*  Perform Array Deque operations */
  138.         do    
  139.         {
  140.             System.out.println("\nArray Deque Operations\n");
  141.             System.out.println("1. add");
  142.             System.out.println("2. get");
  143.             System.out.println("3. set");
  144.             System.out.println("4. remove");
  145.             System.out.println("5. check empty");
  146.             System.out.println("6. clear");
  147.             System.out.println("7. size");
  148.  
  149.             int choice = scan.nextInt();            
  150.             switch (choice) 
  151.             {
  152.             case 1 : 
  153.                 System.out.println("Enter index and element");
  154.                 ad.add(scan.nextInt(), scan.nextInt() );                     
  155.                 break;                          
  156.             case 2 : 
  157.                 System.out.println("Enter index");
  158.                 System.out.println("Result : "+ ad.get(scan.nextInt() ));
  159.                 break;        
  160.             case 3 : 
  161.                 System.out.println("Enter index and element");
  162.                 ad.set(scan.nextInt(), scan.nextInt() );  
  163.                 break; 
  164.             case 4 : 
  165.                 System.out.println("\nEnter index");
  166.                 ad.remove(scan.nextInt() );  
  167.                 break;        
  168.             case 5 : 
  169.                 System.out.println("\nEmpty Status : "+ ad.isEmpty());
  170.                 break;                                                  
  171.             case 6 : 
  172.                 System.out.println("\nArray Deque Cleared");
  173.                 ad.clear();
  174.                 break;    
  175.             case 7 : 
  176.                 System.out.println("\nSize = "+ ad.getSize() );
  177.                 break;         
  178.             default : 
  179.                 System.out.println("Wrong Entry \n ");
  180.                 break;   
  181.             }   
  182.             /** print array deque **/
  183.             ad.display();             
  184.  
  185.             System.out.println("\nDo you want to continue (Type y or n) \n");
  186.             ch = scan.next().charAt(0);                        
  187.         } while (ch == 'Y'|| ch == 'y');  
  188.     }
  189. }

advertisement
Array Deque Test
 
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
5
 
Empty Status : true
 
Array Deque :
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
1
Enter index and element
0 1
 
Array Deque : 1
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
1
Enter index and element
1 2
 
Array Deque : 1 2
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
1
Enter index and element
2 3
 
Array Deque : 1 2 3
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
1
Enter index and element
3 4
 
Array Deque : 1 2 3 4
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
1
Enter index and element
4 5
 
Array Deque : 1 2 3 4 5
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
1
Enter index and element
5 6
 
Array Deque : 1 2 3 4 5 6
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
1
Enter index and element
6 7
 
Array Deque : 1 2 3 4 5 6 7
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
1
Enter index and element
7 8
 
Array Deque : 1 2 3 4 5 6 7 8
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
7
 
Size = 8
 
Array Deque : 1 2 3 4 5 6 7 8
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
4
 
Enter index
2
 
Array Deque : 1 2 4 5 6 7 8
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
1
Enter index and element
4 24
 
Array Deque : 1 2 4 5 24 6 7 8
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
1
Enter index and element
3 25
 
Array Deque : 1 2 4 25 5 24 6 7 8
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
1
Enter index and element
4 26
 
Array Deque : 1 2 4 25 26 5 24 6 7 8
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
7
 
Size = 10
 
Array Deque : 1 2 4 25 26 5 24 6 7 8
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
2
Enter index
6
Result : 24
 
Array Deque : 1 2 4 25 26 5 24 6 7 8
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
3
Enter index and element
3 28
 
Array Deque : 1 2 4 28 26 5 24 6 7 8
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
7
 
Size = 10
 
Array Deque : 1 2 4 28 26 5 24 6 7 8
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
6
 
Array Deque Cleared
 
Array Deque :
 
Do you want to continue (Type y or n)
 
y
 
Array Deque Operations
 
1. add
2. get
3. set
4. remove
5. check empty
6. clear
7. size
5
 
Empty Status : true
 
Array Deque :
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn