/* As a master, I would like to share with you as a collector for your reference! */
/*
* Vector Interface
*/
Package DSA;
Public interface vector {
// Returns the number of elements in the vector.
Public int getsize ();
// Judge whether the vector is empty
Public Boolean isempty ();
// Elements whose rank is R
Public object getatrank (int r)
Throws exceptionboundaryviolation;
// Replace the element whose rank is R with OBJ
Public object replaceatrank (int r, object OBJ)
Throws exceptionboundaryviolation;
// Insert OBJ as an element whose rank is R. This element is returned.
Public object insertatrank (int r, object OBJ)
Throws exceptionboundaryviolation;
// Delete the element whose rank is R
Public object removeatrank (int r)
Throws exceptionboundaryviolation;
}
/*
* Array-based vector implementation
*/
Package DSA;
Public class vector_array implements vector {
Private Final int n = 1024; // array capacity
Private int n = 0; // actual scale of the Vector
Private object [] A; // object Array
// Constructor
Public vector_array (){
A = new object [N];
N = 0;
}
// Returns the number of elements in the vector.
Public int getsize () {return N ;}
// Judge whether the vector is empty
Public Boolean isempty () {return (0 = N )? True: false ;}
// Elements whose rank is R
Public object getatrank (int r) // o (1)
Throws exceptionboundaryviolation {
If (0> r | r> = N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
Return a [R];
}
// Replace the element whose rank is R with OBJ
Public object replaceatrank (int r, object OBJ)
Throws exceptionboundaryviolation {
If (0> r | r> = N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
Object Bak = A [R];
A [R] = OBJ;
Return bak;
}
// Insert OBJ as an element whose rank is R. This element is returned.
Public object insertatrank (int r, object OBJ)
Throws exceptionboundaryviolation {
If (0> r | r> N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
If (n> = N) throw new exceptionboundaryviolation ("unexpected: array overflow ");
For (INT I = N; I> r; I --) A [I] = A [I-1]; // follow-up element shift
A [R] = OBJ; // insert
N ++; // update the current scale
Return OBJ;
}
// Delete the element whose rank is R
Public object removeatrank (int r)
Throws exceptionboundaryviolation {
If (0> r | r> = N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
Object Bak = A [R];
For (INT I = r; I <n; I ++) A [I] = A [I + 1]; // follow-up element forward sequentially
N --; // update the current scale
Return bak;
}
}
/*
* Vector implementation based on Extensible Arrays
*/
Package DSA;
Public class vector_extarray implements vector {
Private int n = 8; // array capacity, which can be continuously increased
Private int N; // the actual scale of the Vector
Private object a []; // object Array
// Constructor
Public vector_extarray () {A = new object [N]; n = 0 ;}
// Returns the number of elements in the vector.
Public int getsize () {return N ;}
// Judge whether the vector is empty
Public Boolean isempty () {return (0 = N )? True: false ;}
// Elements whose rank is R
Public object getatrank (int r)
Throws exceptionboundaryviolation {
If (0> r | r> = N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
Return a [R];
}
// Replace the element whose rank is R with OBJ
Public object replaceatrank (int r, object OBJ)
Throws exceptionboundaryviolation {
If (0> r | r> = N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
Object Bak = A [R];
A [R] = OBJ;
Return bak;
}
// Insert OBJ as an element whose rank is R and return this element.
Public object insertatrank (int r, object OBJ)
Throws exceptionboundaryviolation {
If (0> r | r> N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
If (n <= N) {// processing of Space Overflow
N * = 2;
Object B [] = new object [N]; // open an array with doubled capacity
For (INT I = 0; I <n; I ++) B [I] = A [I]; // copy the content in a [] to B []
A = B; // replace a with B (the original A [] will be automatically recycled)
}
For (INT I = N; I> r; I --) A [I] = A [I-1]; // follow-up element shift
A [R] = OBJ; // insert
N ++; // update the current scale
Return OBJ;
}
// Delete the element whose rank is R
Public object removeatrank (int r)
Throws exceptionboundaryviolation {
If (0> r | r> = N) throw new exceptionboundaryviolation ("accident: rank out of bounds ");
Object Bak = A [R];
For (INT I = r; I <n-1; I ++) A [I] = A [I + 1]; // follow-up element forward sequentially
N --; // update the current scale
Return bak;
}
}