java - Problems extending a circular array deque -


greetings,

i'm trying implement deque using circular array extends when array gets full. problem seems array refuses extend. either computing size() incorrectly or there problem how update front , rear indices. i've looked on many times , seem figure out. can help?

public class arraydeque     {        public static final int init_capacity = 8;   // initial array capacity        protected int capacity;  // current capacity of array        protected int front;     // index of front element        protected int rear;      // index of rear element        protected int[] a;       // array deque         public arraydeque( )      // constructor method        {           = new int[ init_capacity ];           capacity = init_capacity;           front = rear = 0;        }          /**          * display content of deque          */         public void printdeque( )         {           ( int = front; != rear; = (i+1) % capacity )               system.out.print( a[i] + " " );               system.out.println();         }        /**          * returns number of items in collection.          */         public int size()         {             return (capacity - front + rear) % capacity;         }         /**          * returns true if collection empty.          */          public boolean isempty()         {             return front == rear;         }         /**          * returns first element of deque          */         public int getfirst() throws emptydequeexception         {                  if(isempty()){                 throw new emptydequeexception("deque empty.");             }             return a[front % capacity];                }         /**          * returns last element of deque          */         public int getlast() throws emptydequeexception         {               if(isempty()){                 throw new emptydequeexception("deque empty.");             }             return a[(rear - 1) % capacity];                 }         /**          * inserts e @ beginning (as first element) of deque          * if array full, extend array doubling capacity , insert element in new array          */         public void insertfirst(int e)         {             if(size() == capacity){                 int[] b = new int[2*capacity];                 for(int = 0; < size(); i++){                     b[i] = a[i];                 }                 = b;             }             int[] b = new int[capacity];             for(int = 0; < size(); i++){                 b[i] = a[i];             }             = b;             for(int = size(); >= front; i--){                 a[i+1] = a[i];             }             a[front] = e;             rear = (rear + 1) % capacity;         }         /**          * inserts e @ end (as last element) of deque          * if array full, extend array doubling capacity , insert element in new array          */         public void insertlast(int e)         {             if(size() == capacity){                 capacity *= 2;                 int[] b = new int[capacity];                 for(int = 0; < size(); i++){                     b[i] = a[i];                 }                 = b;             }             a[rear] = e;             rear = (rear + 1) % capacity;         }         /**          * removes , returns first element of deque          * shrink array half of current size n when number of elements in deque falls below n/4          * minimum capacity should 8          */         public int removefirst() throws emptydequeexception         {             if(isempty()){                 throw new emptydequeexception("deque empty.");             }             else if(capacity >= 8){                 if(size() < capacity/4){                     capacity /= 2;                     int[] b = new int[capacity];                     for(int = 0; < size(); i++){                         b[i] = a[i];                     }                     = b;                 }             }             int temp = a[front];             a[front] = 0;             front = (front + 1) % capacity;             return temp;         }         /**          * removes , returns last element of deque          * shrink array half of current size n when number of elements in deque falls below n/4          * minimum capacity should 8          */         public int removelast() throws emptydequeexception         {             if(isempty()){                 throw new emptydequeexception("deque empty.");             }             else if(capacity >= 8){                 if(size() < capacity/4){                     int[] b = new int[capacity/2];                     for(int = 0; < capacity; i++){                         b[i] = a[i];                     }                     = b;                 }             }             int temp = a[rear - 1];             a[rear] = 0;             rear = (rear - 1) % capacity;             return temp;         }     }  // end class 

test input:

for(i = 1; <= 100; i++)    q.insertlast(i);    q.printdeque();  for(i = 1; <= 99; i++)    k = q.removefirst();    q.printdeque(); 

test output: i've set several print statements , size remains @ 7 reason...

exception in thread "main" a3.emptydequeexception: deque empty.     @ a3.arraydeque.removefirst(arraydeque.java:133)     @ a3.arraymain.main(arraymain.java:37) 

well, consider this...

if max capacity 8, queue can have 9 total size states: 0 1 2 3 4 5 6 7 , 8.

any_number % 8 can have 8 states: 0 1 2 3 4 5 6 , 7.

this homework (thanks being honest it) don't want spoil you, should point in right direction. luck!


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -