SlideShare a Scribd company logo
1 of 15
Download to read offline
/**
*/
public class Treque extends AbstractList {
/**
* You decide on the instance variables you need.
*/
public Treque(Class t) {
// Put your own code here
throw new UnsupportedOperationException("Constructor not yet implemented");
}
public T get(int i) {
if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException();
// Put your own code here instead of throwing this exception
throw new UnsupportedOperationException("get(i) not yet implemented");
}
public T set(int i, T x) {
if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException();
// Put your own code here instead of throwing this exception
throw new UnsupportedOperationException("set(i,x) not yet implemented");
}
public void add(int i, T x) {
if (i < 0 || i > size()) throw new IndexOutOfBoundsException();
// Put your own code here
throw new UnsupportedOperationException("add(i,x) not yet implemented");
}
public T remove(int i) {
if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException();
// Put your own code here
throw new UnsupportedOperationException("remove(i) not yet implemented");
}
public int size() {
// Put your own code here
throw new UnsupportedOperationException("size() not yet implemented");
}
public static void main(String[] args) {
//List tr = new ArrayDeque(Integer.class);
List tr = new Treque(Integer.class);
int K = 1000000;
Stopwatch s = new Stopwatch();
System.out.print("Appending " + K + " items...");
System.out.flush();
s.start();
for (int i = 0; i < K; i++) {
tr.add(i);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Prepending " + K + " items...");
System.out.flush();
for (int i = 0; i < K; i++) {
tr.add(0, i);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Midpending(?!) " + K + " items...");
System.out.flush();
s.start();
for (int i = 0; i < K; i++) {
tr.add(tr.size()/2);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Removing " + K + " items from the back...");
System.out.flush();
for (int i = 0; i < K; i++) {
tr.remove(tr.size()-1);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Removing " + K + " items from the front...");
System.out.flush();
for (int i = 0; i < K; i++) {
tr.remove(0);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Removing " + K + " items from the middle...");
System.out.flush();
for (int i = 0; i < K; i++) {
tr.remove(tr.size()/2);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
}
}
-------------------------------------------------------------------
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
/**
* This class implements the List interface using a collection of arrays of
* sizes 1, 2, 3, 4, and so on. The main advantages of this over an
* implementation like ArrayList is that there is never more than O(sqrt(size())
* space being used to store anything other than the List elements themselves.
* Insertions and removals take O(size() - i) amortized time.
*
* This provides a space-efficient implementation of an ArrayList.
* The total space used beyond what is required to store elements is O(sqrt(n))
* @author morin
*
* @param the type of objects stored in this list
*/
public class RootishArrayStack extends AbstractList {
/**
* The type of objects stored in this list
*/
Factory f;
/*
* The blocks that contains the list elements
*/
List blocks;
/**
* The number of elements in the list
*/
int n;
/**
* Convert a list index i into a block number
* @param i
* @return the index of the block that contains list
* element i
*/
protected static int i2b(int i) {
double db = (-3.0 + Math.sqrt(9 + 8*i)) / 2.0;
int b = (int)Math.ceil(db);
return b;
}
protected void grow() {
blocks.add(f.newArray(blocks.size()+1));
}
protected void shrink() {
int r = blocks.size();
while (r > 0 && (r-2)*(r-1)/2 >= n) {
blocks.remove(blocks.size()-1);
r--;
}
}
public T get(int i) {
if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
int b = i2b(i);
int j = i - b*(b+1)/2;
return blocks.get(b)[j];
}
public T set(int i, T x) {
if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
int b = i2b(i);
int j = i - b*(b+1)/2;
T y = blocks.get(b)[j];
blocks.get(b)[j] = x;
return y;
}
public void add(int i, T x) {
if (i < 0 || i > n) throw new IndexOutOfBoundsException();
int r = blocks.size();
if (r*(r+1)/2 < n + 1) grow();
n++;
for (int j = n-1; j > i; j--)
set(j, get(j-1));
set(i, x);
}
public T remove(int i) {
if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
T x = get(i);
for (int j = i; j < n-1; j++)
set(j, get(j+1));
n--;
int r = blocks.size();
if ((r-2)*(r-1)/2 >= n) shrink();
return x;
}
public int size() {
return n;
}
public RootishArrayStack(Class t) {
f = new Factory(t);
n = 0;
blocks = new ArrayList();
}
public void clear() {
blocks.clear();
n = 0;
}
}
-------------------------------------------------------------------
import java.util.AbstractList;
import java.util.List;
import java.util.ArrayList;
/**
*/
public class RootishArrayDeque extends AbstractList {
/**
* You decide on the instance variables you need.
*/
public RootishArrayDeque(Class t) {
// Put your own code here
throw new UnsupportedOperationException("Constructor not yet implemented");
}
public T get(int i) {
if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException();
// Put your own code here instead of throwing this exception
throw new UnsupportedOperationException("get(i) not yet implemented");
}
public T set(int i, T x) {
if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException();
// Put your own code here instead of throwing this exception
throw new UnsupportedOperationException("set(i,x) not yet implemented");
}
public void add(int i, T x) {
if (i < 0 || i > size()) throw new IndexOutOfBoundsException();
// Put your own code here
throw new UnsupportedOperationException("add(i,x) not yet implemented");
// set(i, x);
}
public T remove(int i) {
if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException();
// Put your own code here
throw new UnsupportedOperationException("size(i) not yet implemented");
}
public int size() {
// Put your own code here
throw new UnsupportedOperationException("size() not yet implemented");
}
public static void main(String[] args) {
//List rad = new ArrayDeque(Integer.class);
List rad = new RootishArrayDeque(Integer.class);
int K = 1000000;
Stopwatch s = new Stopwatch();
System.out.print("Appending " + K + " items...");
System.out.flush();
s.start();
for (int i = 0; i < K; i++) {
rad.add(i);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Prepending " + K + " items...");
System.out.flush();
for (int i = 0; i < K; i++) {
rad.add(0, i);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Removing " + K + " items from the back...");
System.out.flush();
for (int i = 0; i < K; i++) {
rad.remove(rad.size()-1);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Removing " + K + " items from the front...");
System.out.flush();
for (int i = 0; i < K; i++) {
rad.remove(0);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
}
}
Solution
/**
*/
public class Treque extends AbstractList {
/**
* You decide on the instance variables you need.
*/
public Treque(Class t) {
// Put your own code here
throw new UnsupportedOperationException("Constructor not yet implemented");
}
public T get(int i) {
if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException();
// Put your own code here instead of throwing this exception
throw new UnsupportedOperationException("get(i) not yet implemented");
}
public T set(int i, T x) {
if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException();
// Put your own code here instead of throwing this exception
throw new UnsupportedOperationException("set(i,x) not yet implemented");
}
public void add(int i, T x) {
if (i < 0 || i > size()) throw new IndexOutOfBoundsException();
// Put your own code here
throw new UnsupportedOperationException("add(i,x) not yet implemented");
}
public T remove(int i) {
if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException();
// Put your own code here
throw new UnsupportedOperationException("remove(i) not yet implemented");
}
public int size() {
// Put your own code here
throw new UnsupportedOperationException("size() not yet implemented");
}
public static void main(String[] args) {
//List tr = new ArrayDeque(Integer.class);
List tr = new Treque(Integer.class);
int K = 1000000;
Stopwatch s = new Stopwatch();
System.out.print("Appending " + K + " items...");
System.out.flush();
s.start();
for (int i = 0; i < K; i++) {
tr.add(i);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Prepending " + K + " items...");
System.out.flush();
for (int i = 0; i < K; i++) {
tr.add(0, i);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Midpending(?!) " + K + " items...");
System.out.flush();
s.start();
for (int i = 0; i < K; i++) {
tr.add(tr.size()/2);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Removing " + K + " items from the back...");
System.out.flush();
for (int i = 0; i < K; i++) {
tr.remove(tr.size()-1);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Removing " + K + " items from the front...");
System.out.flush();
for (int i = 0; i < K; i++) {
tr.remove(0);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Removing " + K + " items from the middle...");
System.out.flush();
for (int i = 0; i < K; i++) {
tr.remove(tr.size()/2);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
}
}
-------------------------------------------------------------------
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
/**
* This class implements the List interface using a collection of arrays of
* sizes 1, 2, 3, 4, and so on. The main advantages of this over an
* implementation like ArrayList is that there is never more than O(sqrt(size())
* space being used to store anything other than the List elements themselves.
* Insertions and removals take O(size() - i) amortized time.
*
* This provides a space-efficient implementation of an ArrayList.
* The total space used beyond what is required to store elements is O(sqrt(n))
* @author morin
*
* @param the type of objects stored in this list
*/
public class RootishArrayStack extends AbstractList {
/**
* The type of objects stored in this list
*/
Factory f;
/*
* The blocks that contains the list elements
*/
List blocks;
/**
* The number of elements in the list
*/
int n;
/**
* Convert a list index i into a block number
* @param i
* @return the index of the block that contains list
* element i
*/
protected static int i2b(int i) {
double db = (-3.0 + Math.sqrt(9 + 8*i)) / 2.0;
int b = (int)Math.ceil(db);
return b;
}
protected void grow() {
blocks.add(f.newArray(blocks.size()+1));
}
protected void shrink() {
int r = blocks.size();
while (r > 0 && (r-2)*(r-1)/2 >= n) {
blocks.remove(blocks.size()-1);
r--;
}
}
public T get(int i) {
if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
int b = i2b(i);
int j = i - b*(b+1)/2;
return blocks.get(b)[j];
}
public T set(int i, T x) {
if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
int b = i2b(i);
int j = i - b*(b+1)/2;
T y = blocks.get(b)[j];
blocks.get(b)[j] = x;
return y;
}
public void add(int i, T x) {
if (i < 0 || i > n) throw new IndexOutOfBoundsException();
int r = blocks.size();
if (r*(r+1)/2 < n + 1) grow();
n++;
for (int j = n-1; j > i; j--)
set(j, get(j-1));
set(i, x);
}
public T remove(int i) {
if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
T x = get(i);
for (int j = i; j < n-1; j++)
set(j, get(j+1));
n--;
int r = blocks.size();
if ((r-2)*(r-1)/2 >= n) shrink();
return x;
}
public int size() {
return n;
}
public RootishArrayStack(Class t) {
f = new Factory(t);
n = 0;
blocks = new ArrayList();
}
public void clear() {
blocks.clear();
n = 0;
}
}
-------------------------------------------------------------------
import java.util.AbstractList;
import java.util.List;
import java.util.ArrayList;
/**
*/
public class RootishArrayDeque extends AbstractList {
/**
* You decide on the instance variables you need.
*/
public RootishArrayDeque(Class t) {
// Put your own code here
throw new UnsupportedOperationException("Constructor not yet implemented");
}
public T get(int i) {
if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException();
// Put your own code here instead of throwing this exception
throw new UnsupportedOperationException("get(i) not yet implemented");
}
public T set(int i, T x) {
if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException();
// Put your own code here instead of throwing this exception
throw new UnsupportedOperationException("set(i,x) not yet implemented");
}
public void add(int i, T x) {
if (i < 0 || i > size()) throw new IndexOutOfBoundsException();
// Put your own code here
throw new UnsupportedOperationException("add(i,x) not yet implemented");
// set(i, x);
}
public T remove(int i) {
if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException();
// Put your own code here
throw new UnsupportedOperationException("size(i) not yet implemented");
}
public int size() {
// Put your own code here
throw new UnsupportedOperationException("size() not yet implemented");
}
public static void main(String[] args) {
//List rad = new ArrayDeque(Integer.class);
List rad = new RootishArrayDeque(Integer.class);
int K = 1000000;
Stopwatch s = new Stopwatch();
System.out.print("Appending " + K + " items...");
System.out.flush();
s.start();
for (int i = 0; i < K; i++) {
rad.add(i);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Prepending " + K + " items...");
System.out.flush();
for (int i = 0; i < K; i++) {
rad.add(0, i);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Removing " + K + " items from the back...");
System.out.flush();
for (int i = 0; i < K; i++) {
rad.remove(rad.size()-1);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
System.out.print("Removing " + K + " items from the front...");
System.out.flush();
for (int i = 0; i < K; i++) {
rad.remove(0);
}
s.stop();
System.out.println("done (" + s.elapsedSeconds() + "s)");
}
}

More Related Content

Similar to public class TrequeT extends AbstractListT { .pdf

Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfkostikjaylonshaewe47
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfoptokunal1
 
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
JAVA - Design a data structure IntSet that can hold a set of integers-.docxJAVA - Design a data structure IntSet that can hold a set of integers-.docx
JAVA - Design a data structure IntSet that can hold a set of integers-.docxolsenlinnea427
 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdfasarudheen07
 
Java interface and inheritance
Java interface and inheritanceJava interface and inheritance
Java interface and inheritanceJaromirJagr
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfravikapoorindia
 
The output should now look like this Row 1 sum 30 Row 2 sum.pdf
The output should now look like this Row 1 sum 30 Row 2 sum.pdfThe output should now look like this Row 1 sum 30 Row 2 sum.pdf
The output should now look like this Row 1 sum 30 Row 2 sum.pdfxlynettalampleyxc
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewPaulo Morgado
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
Please add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docxPlease add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docxStewartt0kJohnstonh
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdffantasiatheoutofthef
 
Tools and Techniques for Understanding Threading Behavior in Android*
Tools and Techniques for Understanding Threading Behavior in Android*Tools and Techniques for Understanding Threading Behavior in Android*
Tools and Techniques for Understanding Threading Behavior in Android*Intel® Software
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfillyasraja7
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 

Similar to public class TrequeT extends AbstractListT { .pdf (20)

C# labprograms
C# labprogramsC# labprograms
C# labprograms
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
 
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
JAVA - Design a data structure IntSet that can hold a set of integers-.docxJAVA - Design a data structure IntSet that can hold a set of integers-.docx
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
 
Java interface and inheritance
Java interface and inheritanceJava interface and inheritance
Java interface and inheritance
 
Java generics
Java genericsJava generics
Java generics
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
The output should now look like this Row 1 sum 30 Row 2 sum.pdf
The output should now look like this Row 1 sum 30 Row 2 sum.pdfThe output should now look like this Row 1 sum 30 Row 2 sum.pdf
The output should now look like this Row 1 sum 30 Row 2 sum.pdf
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
 
Java practical
Java practicalJava practical
Java practical
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Please add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docxPlease add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docx
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
Tools and Techniques for Understanding Threading Behavior in Android*
Tools and Techniques for Understanding Threading Behavior in Android*Tools and Techniques for Understanding Threading Behavior in Android*
Tools and Techniques for Understanding Threading Behavior in Android*
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Property-based testing
Property-based testingProperty-based testing
Property-based testing
 

More from info30292

Use the Henderson eqn pOH = pKb + log(baseacid).pdf
                     Use the Henderson eqn pOH = pKb + log(baseacid).pdf                     Use the Henderson eqn pOH = pKb + log(baseacid).pdf
Use the Henderson eqn pOH = pKb + log(baseacid).pdfinfo30292
 
The purpose is to protonate the ether present the.pdf
                     The purpose is to protonate the ether present the.pdf                     The purpose is to protonate the ether present the.pdf
The purpose is to protonate the ether present the.pdfinfo30292
 
no reaction since in metal activity series Zn is .pdf
                     no reaction since in metal activity series Zn is .pdf                     no reaction since in metal activity series Zn is .pdf
no reaction since in metal activity series Zn is .pdfinfo30292
 
nuclei can contain different numbers of neutrons..pdf
                     nuclei can contain different numbers of neutrons..pdf                     nuclei can contain different numbers of neutrons..pdf
nuclei can contain different numbers of neutrons..pdfinfo30292
 
KOH is a base as will yield OH-. FeSO4 is a salt.pdf
                     KOH is a base as will yield OH-.  FeSO4 is a salt.pdf                     KOH is a base as will yield OH-.  FeSO4 is a salt.pdf
KOH is a base as will yield OH-. FeSO4 is a salt.pdfinfo30292
 
The vagus nerve is the longest cranial nerve. It contains motor and .pdf
The vagus nerve is the longest cranial nerve. It contains motor and .pdfThe vagus nerve is the longest cranial nerve. It contains motor and .pdf
The vagus nerve is the longest cranial nerve. It contains motor and .pdfinfo30292
 
Solution 3-2Following are the basic users of financial statements.pdf
Solution 3-2Following are the basic users of financial statements.pdfSolution 3-2Following are the basic users of financial statements.pdf
Solution 3-2Following are the basic users of financial statements.pdfinfo30292
 
So, you know that atoms have electrons. But how are they organized a.pdf
So, you know that atoms have electrons. But how are they organized a.pdfSo, you know that atoms have electrons. But how are they organized a.pdf
So, you know that atoms have electrons. But how are they organized a.pdfinfo30292
 
Rutherford model of atom1) The electron cloud of the atom does n.pdf
Rutherford model of atom1) The electron cloud of the atom does n.pdfRutherford model of atom1) The electron cloud of the atom does n.pdf
Rutherford model of atom1) The electron cloud of the atom does n.pdfinfo30292
 
Raising the pH will increase the basicity of the solution.Since su.pdf
Raising the pH will increase the basicity of the solution.Since su.pdfRaising the pH will increase the basicity of the solution.Since su.pdf
Raising the pH will increase the basicity of the solution.Since su.pdfinfo30292
 
Process improvementThe chance for development to either operating.pdf
Process improvementThe chance for development to either operating.pdfProcess improvementThe chance for development to either operating.pdf
Process improvementThe chance for development to either operating.pdfinfo30292
 
Net operating assets = operating assets-operating liabilitiesNet o.pdf
Net operating assets = operating assets-operating liabilitiesNet o.pdfNet operating assets = operating assets-operating liabilitiesNet o.pdf
Net operating assets = operating assets-operating liabilitiesNet o.pdfinfo30292
 
In above scenarion Animator class,Actor pikachu=new Actor (picka.pdf
In above scenarion Animator class,Actor pikachu=new Actor (picka.pdfIn above scenarion Animator class,Actor pikachu=new Actor (picka.pdf
In above scenarion Animator class,Actor pikachu=new Actor (picka.pdfinfo30292
 
import java.util.; public class NaturalNumberTriangle { publi.pdf
import java.util.; public class NaturalNumberTriangle { publi.pdfimport java.util.; public class NaturalNumberTriangle { publi.pdf
import java.util.; public class NaturalNumberTriangle { publi.pdfinfo30292
 
HNO3 is a strong acid pH = -log[H+] = -log[0.05] = 1.3p.pdf
HNO3 is a strong acid  pH = -log[H+] = -log[0.05] = 1.3p.pdfHNO3 is a strong acid  pH = -log[H+] = -log[0.05] = 1.3p.pdf
HNO3 is a strong acid pH = -log[H+] = -log[0.05] = 1.3p.pdfinfo30292
 
From psychological and cognitive perspectives, there are two basic b.pdf
From psychological and cognitive perspectives, there are two basic b.pdfFrom psychological and cognitive perspectives, there are two basic b.pdf
From psychological and cognitive perspectives, there are two basic b.pdfinfo30292
 
Formatting and code conversion occurs in the Presentation layer of t.pdf
Formatting and code conversion occurs in the Presentation layer of t.pdfFormatting and code conversion occurs in the Presentation layer of t.pdf
Formatting and code conversion occurs in the Presentation layer of t.pdfinfo30292
 
Correct answer is option A. As epistasis occurs when the effect of o.pdf
Correct answer is option A. As epistasis occurs when the effect of o.pdfCorrect answer is option A. As epistasis occurs when the effect of o.pdf
Correct answer is option A. As epistasis occurs when the effect of o.pdfinfo30292
 
Cloud computingIt is a kind of net based computing that gives sha.pdf
Cloud computingIt is a kind of net based computing that gives sha.pdfCloud computingIt is a kind of net based computing that gives sha.pdf
Cloud computingIt is a kind of net based computing that gives sha.pdfinfo30292
 
Check listpolicy to be implemented for having controls for purchasi.pdf
Check listpolicy to be implemented for having controls for purchasi.pdfCheck listpolicy to be implemented for having controls for purchasi.pdf
Check listpolicy to be implemented for having controls for purchasi.pdfinfo30292
 

More from info30292 (20)

Use the Henderson eqn pOH = pKb + log(baseacid).pdf
                     Use the Henderson eqn pOH = pKb + log(baseacid).pdf                     Use the Henderson eqn pOH = pKb + log(baseacid).pdf
Use the Henderson eqn pOH = pKb + log(baseacid).pdf
 
The purpose is to protonate the ether present the.pdf
                     The purpose is to protonate the ether present the.pdf                     The purpose is to protonate the ether present the.pdf
The purpose is to protonate the ether present the.pdf
 
no reaction since in metal activity series Zn is .pdf
                     no reaction since in metal activity series Zn is .pdf                     no reaction since in metal activity series Zn is .pdf
no reaction since in metal activity series Zn is .pdf
 
nuclei can contain different numbers of neutrons..pdf
                     nuclei can contain different numbers of neutrons..pdf                     nuclei can contain different numbers of neutrons..pdf
nuclei can contain different numbers of neutrons..pdf
 
KOH is a base as will yield OH-. FeSO4 is a salt.pdf
                     KOH is a base as will yield OH-.  FeSO4 is a salt.pdf                     KOH is a base as will yield OH-.  FeSO4 is a salt.pdf
KOH is a base as will yield OH-. FeSO4 is a salt.pdf
 
The vagus nerve is the longest cranial nerve. It contains motor and .pdf
The vagus nerve is the longest cranial nerve. It contains motor and .pdfThe vagus nerve is the longest cranial nerve. It contains motor and .pdf
The vagus nerve is the longest cranial nerve. It contains motor and .pdf
 
Solution 3-2Following are the basic users of financial statements.pdf
Solution 3-2Following are the basic users of financial statements.pdfSolution 3-2Following are the basic users of financial statements.pdf
Solution 3-2Following are the basic users of financial statements.pdf
 
So, you know that atoms have electrons. But how are they organized a.pdf
So, you know that atoms have electrons. But how are they organized a.pdfSo, you know that atoms have electrons. But how are they organized a.pdf
So, you know that atoms have electrons. But how are they organized a.pdf
 
Rutherford model of atom1) The electron cloud of the atom does n.pdf
Rutherford model of atom1) The electron cloud of the atom does n.pdfRutherford model of atom1) The electron cloud of the atom does n.pdf
Rutherford model of atom1) The electron cloud of the atom does n.pdf
 
Raising the pH will increase the basicity of the solution.Since su.pdf
Raising the pH will increase the basicity of the solution.Since su.pdfRaising the pH will increase the basicity of the solution.Since su.pdf
Raising the pH will increase the basicity of the solution.Since su.pdf
 
Process improvementThe chance for development to either operating.pdf
Process improvementThe chance for development to either operating.pdfProcess improvementThe chance for development to either operating.pdf
Process improvementThe chance for development to either operating.pdf
 
Net operating assets = operating assets-operating liabilitiesNet o.pdf
Net operating assets = operating assets-operating liabilitiesNet o.pdfNet operating assets = operating assets-operating liabilitiesNet o.pdf
Net operating assets = operating assets-operating liabilitiesNet o.pdf
 
In above scenarion Animator class,Actor pikachu=new Actor (picka.pdf
In above scenarion Animator class,Actor pikachu=new Actor (picka.pdfIn above scenarion Animator class,Actor pikachu=new Actor (picka.pdf
In above scenarion Animator class,Actor pikachu=new Actor (picka.pdf
 
import java.util.; public class NaturalNumberTriangle { publi.pdf
import java.util.; public class NaturalNumberTriangle { publi.pdfimport java.util.; public class NaturalNumberTriangle { publi.pdf
import java.util.; public class NaturalNumberTriangle { publi.pdf
 
HNO3 is a strong acid pH = -log[H+] = -log[0.05] = 1.3p.pdf
HNO3 is a strong acid  pH = -log[H+] = -log[0.05] = 1.3p.pdfHNO3 is a strong acid  pH = -log[H+] = -log[0.05] = 1.3p.pdf
HNO3 is a strong acid pH = -log[H+] = -log[0.05] = 1.3p.pdf
 
From psychological and cognitive perspectives, there are two basic b.pdf
From psychological and cognitive perspectives, there are two basic b.pdfFrom psychological and cognitive perspectives, there are two basic b.pdf
From psychological and cognitive perspectives, there are two basic b.pdf
 
Formatting and code conversion occurs in the Presentation layer of t.pdf
Formatting and code conversion occurs in the Presentation layer of t.pdfFormatting and code conversion occurs in the Presentation layer of t.pdf
Formatting and code conversion occurs in the Presentation layer of t.pdf
 
Correct answer is option A. As epistasis occurs when the effect of o.pdf
Correct answer is option A. As epistasis occurs when the effect of o.pdfCorrect answer is option A. As epistasis occurs when the effect of o.pdf
Correct answer is option A. As epistasis occurs when the effect of o.pdf
 
Cloud computingIt is a kind of net based computing that gives sha.pdf
Cloud computingIt is a kind of net based computing that gives sha.pdfCloud computingIt is a kind of net based computing that gives sha.pdf
Cloud computingIt is a kind of net based computing that gives sha.pdf
 
Check listpolicy to be implemented for having controls for purchasi.pdf
Check listpolicy to be implemented for having controls for purchasi.pdfCheck listpolicy to be implemented for having controls for purchasi.pdf
Check listpolicy to be implemented for having controls for purchasi.pdf
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 

public class TrequeT extends AbstractListT { .pdf

  • 1. /** */ public class Treque extends AbstractList { /** * You decide on the instance variables you need. */ public Treque(Class t) { // Put your own code here throw new UnsupportedOperationException("Constructor not yet implemented"); } public T get(int i) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here instead of throwing this exception throw new UnsupportedOperationException("get(i) not yet implemented"); } public T set(int i, T x) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here instead of throwing this exception throw new UnsupportedOperationException("set(i,x) not yet implemented"); } public void add(int i, T x) { if (i < 0 || i > size()) throw new IndexOutOfBoundsException(); // Put your own code here throw new UnsupportedOperationException("add(i,x) not yet implemented"); } public T remove(int i) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here throw new UnsupportedOperationException("remove(i) not yet implemented"); } public int size() { // Put your own code here throw new UnsupportedOperationException("size() not yet implemented"); } public static void main(String[] args) {
  • 2. //List tr = new ArrayDeque(Integer.class); List tr = new Treque(Integer.class); int K = 1000000; Stopwatch s = new Stopwatch(); System.out.print("Appending " + K + " items..."); System.out.flush(); s.start(); for (int i = 0; i < K; i++) { tr.add(i); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Prepending " + K + " items..."); System.out.flush(); for (int i = 0; i < K; i++) { tr.add(0, i); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Midpending(?!) " + K + " items..."); System.out.flush(); s.start(); for (int i = 0; i < K; i++) { tr.add(tr.size()/2); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Removing " + K + " items from the back..."); System.out.flush(); for (int i = 0; i < K; i++) { tr.remove(tr.size()-1); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Removing " + K + " items from the front...");
  • 3. System.out.flush(); for (int i = 0; i < K; i++) { tr.remove(0); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Removing " + K + " items from the middle..."); System.out.flush(); for (int i = 0; i < K; i++) { tr.remove(tr.size()/2); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); } } ------------------------------------------------------------------- import java.util.AbstractList; import java.util.ArrayList; import java.util.List; /** * This class implements the List interface using a collection of arrays of * sizes 1, 2, 3, 4, and so on. The main advantages of this over an * implementation like ArrayList is that there is never more than O(sqrt(size()) * space being used to store anything other than the List elements themselves. * Insertions and removals take O(size() - i) amortized time. * * This provides a space-efficient implementation of an ArrayList. * The total space used beyond what is required to store elements is O(sqrt(n)) * @author morin * * @param the type of objects stored in this list */ public class RootishArrayStack extends AbstractList { /** * The type of objects stored in this list */
  • 4. Factory f; /* * The blocks that contains the list elements */ List blocks; /** * The number of elements in the list */ int n; /** * Convert a list index i into a block number * @param i * @return the index of the block that contains list * element i */ protected static int i2b(int i) { double db = (-3.0 + Math.sqrt(9 + 8*i)) / 2.0; int b = (int)Math.ceil(db); return b; } protected void grow() { blocks.add(f.newArray(blocks.size()+1)); } protected void shrink() { int r = blocks.size(); while (r > 0 && (r-2)*(r-1)/2 >= n) { blocks.remove(blocks.size()-1); r--; } } public T get(int i) { if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException(); int b = i2b(i); int j = i - b*(b+1)/2; return blocks.get(b)[j]; }
  • 5. public T set(int i, T x) { if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException(); int b = i2b(i); int j = i - b*(b+1)/2; T y = blocks.get(b)[j]; blocks.get(b)[j] = x; return y; } public void add(int i, T x) { if (i < 0 || i > n) throw new IndexOutOfBoundsException(); int r = blocks.size(); if (r*(r+1)/2 < n + 1) grow(); n++; for (int j = n-1; j > i; j--) set(j, get(j-1)); set(i, x); } public T remove(int i) { if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException(); T x = get(i); for (int j = i; j < n-1; j++) set(j, get(j+1)); n--; int r = blocks.size(); if ((r-2)*(r-1)/2 >= n) shrink(); return x; } public int size() { return n; } public RootishArrayStack(Class t) { f = new Factory(t); n = 0; blocks = new ArrayList(); } public void clear() {
  • 6. blocks.clear(); n = 0; } } ------------------------------------------------------------------- import java.util.AbstractList; import java.util.List; import java.util.ArrayList; /** */ public class RootishArrayDeque extends AbstractList { /** * You decide on the instance variables you need. */ public RootishArrayDeque(Class t) { // Put your own code here throw new UnsupportedOperationException("Constructor not yet implemented"); } public T get(int i) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here instead of throwing this exception throw new UnsupportedOperationException("get(i) not yet implemented"); } public T set(int i, T x) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here instead of throwing this exception throw new UnsupportedOperationException("set(i,x) not yet implemented"); } public void add(int i, T x) { if (i < 0 || i > size()) throw new IndexOutOfBoundsException(); // Put your own code here throw new UnsupportedOperationException("add(i,x) not yet implemented"); // set(i, x); } public T remove(int i) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException();
  • 7. // Put your own code here throw new UnsupportedOperationException("size(i) not yet implemented"); } public int size() { // Put your own code here throw new UnsupportedOperationException("size() not yet implemented"); } public static void main(String[] args) { //List rad = new ArrayDeque(Integer.class); List rad = new RootishArrayDeque(Integer.class); int K = 1000000; Stopwatch s = new Stopwatch(); System.out.print("Appending " + K + " items..."); System.out.flush(); s.start(); for (int i = 0; i < K; i++) { rad.add(i); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Prepending " + K + " items..."); System.out.flush(); for (int i = 0; i < K; i++) { rad.add(0, i); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Removing " + K + " items from the back..."); System.out.flush(); for (int i = 0; i < K; i++) { rad.remove(rad.size()-1); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Removing " + K + " items from the front..."); System.out.flush();
  • 8. for (int i = 0; i < K; i++) { rad.remove(0); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); } } Solution /** */ public class Treque extends AbstractList { /** * You decide on the instance variables you need. */ public Treque(Class t) { // Put your own code here throw new UnsupportedOperationException("Constructor not yet implemented"); } public T get(int i) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here instead of throwing this exception throw new UnsupportedOperationException("get(i) not yet implemented"); } public T set(int i, T x) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here instead of throwing this exception throw new UnsupportedOperationException("set(i,x) not yet implemented"); } public void add(int i, T x) { if (i < 0 || i > size()) throw new IndexOutOfBoundsException(); // Put your own code here throw new UnsupportedOperationException("add(i,x) not yet implemented"); } public T remove(int i) {
  • 9. if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here throw new UnsupportedOperationException("remove(i) not yet implemented"); } public int size() { // Put your own code here throw new UnsupportedOperationException("size() not yet implemented"); } public static void main(String[] args) { //List tr = new ArrayDeque(Integer.class); List tr = new Treque(Integer.class); int K = 1000000; Stopwatch s = new Stopwatch(); System.out.print("Appending " + K + " items..."); System.out.flush(); s.start(); for (int i = 0; i < K; i++) { tr.add(i); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Prepending " + K + " items..."); System.out.flush(); for (int i = 0; i < K; i++) { tr.add(0, i); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Midpending(?!) " + K + " items..."); System.out.flush(); s.start(); for (int i = 0; i < K; i++) { tr.add(tr.size()/2); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)");
  • 10. System.out.print("Removing " + K + " items from the back..."); System.out.flush(); for (int i = 0; i < K; i++) { tr.remove(tr.size()-1); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Removing " + K + " items from the front..."); System.out.flush(); for (int i = 0; i < K; i++) { tr.remove(0); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Removing " + K + " items from the middle..."); System.out.flush(); for (int i = 0; i < K; i++) { tr.remove(tr.size()/2); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); } } ------------------------------------------------------------------- import java.util.AbstractList; import java.util.ArrayList; import java.util.List; /** * This class implements the List interface using a collection of arrays of * sizes 1, 2, 3, 4, and so on. The main advantages of this over an * implementation like ArrayList is that there is never more than O(sqrt(size()) * space being used to store anything other than the List elements themselves. * Insertions and removals take O(size() - i) amortized time. * * This provides a space-efficient implementation of an ArrayList.
  • 11. * The total space used beyond what is required to store elements is O(sqrt(n)) * @author morin * * @param the type of objects stored in this list */ public class RootishArrayStack extends AbstractList { /** * The type of objects stored in this list */ Factory f; /* * The blocks that contains the list elements */ List blocks; /** * The number of elements in the list */ int n; /** * Convert a list index i into a block number * @param i * @return the index of the block that contains list * element i */ protected static int i2b(int i) { double db = (-3.0 + Math.sqrt(9 + 8*i)) / 2.0; int b = (int)Math.ceil(db); return b; } protected void grow() { blocks.add(f.newArray(blocks.size()+1)); } protected void shrink() { int r = blocks.size(); while (r > 0 && (r-2)*(r-1)/2 >= n) { blocks.remove(blocks.size()-1);
  • 12. r--; } } public T get(int i) { if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException(); int b = i2b(i); int j = i - b*(b+1)/2; return blocks.get(b)[j]; } public T set(int i, T x) { if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException(); int b = i2b(i); int j = i - b*(b+1)/2; T y = blocks.get(b)[j]; blocks.get(b)[j] = x; return y; } public void add(int i, T x) { if (i < 0 || i > n) throw new IndexOutOfBoundsException(); int r = blocks.size(); if (r*(r+1)/2 < n + 1) grow(); n++; for (int j = n-1; j > i; j--) set(j, get(j-1)); set(i, x); } public T remove(int i) { if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException(); T x = get(i); for (int j = i; j < n-1; j++) set(j, get(j+1)); n--; int r = blocks.size(); if ((r-2)*(r-1)/2 >= n) shrink(); return x; }
  • 13. public int size() { return n; } public RootishArrayStack(Class t) { f = new Factory(t); n = 0; blocks = new ArrayList(); } public void clear() { blocks.clear(); n = 0; } } ------------------------------------------------------------------- import java.util.AbstractList; import java.util.List; import java.util.ArrayList; /** */ public class RootishArrayDeque extends AbstractList { /** * You decide on the instance variables you need. */ public RootishArrayDeque(Class t) { // Put your own code here throw new UnsupportedOperationException("Constructor not yet implemented"); } public T get(int i) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here instead of throwing this exception throw new UnsupportedOperationException("get(i) not yet implemented"); } public T set(int i, T x) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here instead of throwing this exception throw new UnsupportedOperationException("set(i,x) not yet implemented");
  • 14. } public void add(int i, T x) { if (i < 0 || i > size()) throw new IndexOutOfBoundsException(); // Put your own code here throw new UnsupportedOperationException("add(i,x) not yet implemented"); // set(i, x); } public T remove(int i) { if (i < 0 || i > size() - 1) throw new IndexOutOfBoundsException(); // Put your own code here throw new UnsupportedOperationException("size(i) not yet implemented"); } public int size() { // Put your own code here throw new UnsupportedOperationException("size() not yet implemented"); } public static void main(String[] args) { //List rad = new ArrayDeque(Integer.class); List rad = new RootishArrayDeque(Integer.class); int K = 1000000; Stopwatch s = new Stopwatch(); System.out.print("Appending " + K + " items..."); System.out.flush(); s.start(); for (int i = 0; i < K; i++) { rad.add(i); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Prepending " + K + " items..."); System.out.flush(); for (int i = 0; i < K; i++) { rad.add(0, i); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)");
  • 15. System.out.print("Removing " + K + " items from the back..."); System.out.flush(); for (int i = 0; i < K; i++) { rad.remove(rad.size()-1); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); System.out.print("Removing " + K + " items from the front..."); System.out.flush(); for (int i = 0; i < K; i++) { rad.remove(0); } s.stop(); System.out.println("done (" + s.elapsedSeconds() + "s)"); } }