Skip to content

Commit

Permalink
raw entry
Browse files Browse the repository at this point in the history
  • Loading branch information
xt0fer committed Oct 4, 2018
0 parents commit 535948d
Show file tree
Hide file tree
Showing 43 changed files with 3,603 additions and 0 deletions.
153 changes: 153 additions & 0 deletions CodeSamples/GenericList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package Fall0811;
import java.util.Iterator;

public class GenericList<E> implements Iterable<E>{
// class constant
private static final int DEFAULT_CAP = 10;

// instance variables
protected E[] container; // the array is NOT the list
private int listSize;

public Iterator<E> iterator(){
return new GenListIterator();
}

// inner class
private class GenListIterator implements Iterator<E>{
private int indexOfNextElement;
private boolean okToRemove;

private GenListIterator(){
indexOfNextElement = 0;
okToRemove = false;
}

public boolean hasNext(){
return indexOfNextElement < size();
}

public E next(){
assert hasNext();
okToRemove = true;
indexOfNextElement++;
return container[indexOfNextElement - 1];
}

public void remove(){
assert okToRemove;
okToRemove = false;
indexOfNextElement--;
GenericList.this.remove(indexOfNextElement);
}

}

public boolean equals(Object obj){
assert this != null;
if(obj == null)
return false;
else if (this == obj)
return true;
else if( this.getClass() != obj.getClass() )
return false;
else{
// obj is a non null GenericList
GenericList list = (GenericList)obj;
if( list.size() != size() )
return false;
for(int i = 0; i < size(); i++)
if( (get(i) == null && list.get(i) != null) || !get(i).equals(list.get(i)) )
return false;
return true;
}


}


// creates an empty IntList
public GenericList(){
this(DEFAULT_CAP);
}

// pre: initialCap >= 0
public GenericList(int initialCap){
assert initialCap >= 0 : "failed precondition";
container = (E[])(new Object[initialCap]);
listSize = 0;
}

public void insertAll(int pos, GenericList<E> otherList){

for(int i = 0; i < otherList.listSize; i++){
this.insert(pos + i, otherList.container[i]);
}
}

// pre: 0 <= pos < size()
public E remove(int pos){
E result = container[pos];
listSize--;
for(int index = pos; index < size(); index++){
container[index] = container[index + 1];
}
container[listSize] = null;
return result;
}

// pre: 0 <= pos <= size()
public void insert(int pos, E element){
assert 0 <= pos && pos <= size();
if( size() == container.length )
resize();
for(int index = size(); index > pos; index--){
assert index > 0;
container[index] = container[index - 1];
}
container[pos] = element;
listSize++;
}

// get size of list
public int size(){
return listSize;
}

// access elements
// pre: 0 <= position < size()
public E get(int position){
assert 0 <= position && position < size();
return container[position];
}

// pre: none
public void add(E element){
insert(size(), element);
}

private void resize() {
E[] temp = (E[])(new Object[container.length * 2 + 1]);
System.arraycopy(container, 0, temp, 0, size());
container = temp;
}

public String toString(){
StringBuffer result = new StringBuffer("[");
final int LIMIT = size() - 1;
for(int i = 0; i < LIMIT; i++){
if( this == this.get(i) )
result.append("this list");
else{
result.append(get(i));
}
result.append(", ");
}
if( size() != 0)
result.append(get(size() - 1));
result.append("]");
return result.toString();
}

}

152 changes: 152 additions & 0 deletions CodeSamples/LinkedList/IList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import java.util.Iterator;

/**
* Interface for a simple List. Random access to all items in the list is provided.
* The numbering of elements in the list begins at 0.
*
*/
public interface IList<E> extends Iterable<E>{

/**
* Add an item to the end of this list.
* <br>pre: none
* <br>post: size() = old size() + 1, get(size() - 1) = item
* @param item the data to be added to the end of this list
*/
void add(E item);

/**
* Insert an item at a specified position in the list.
* <br>pre: 0 <= pos <= size()
* <br>post: size() = old size() + 1, get(pos) = item, all elements in
* the list with a positon >= pos have a position = old position + 1
* @param pos the position to insert the data at in the list
* @param item the data to add to the list
*/
void insert(int pos, E item);

/**
* Change the data at the specified position in the list.
* the old data at that position is returned.
* <br>pre: 0 <= pos < size()
* <br>post: get(pos) = item, return the
* old get(pos)
* @param pos the position in the list to overwrite
* @param item the new item that will overwrite the old item
* @return the old data at the specified position
*/
E set(int pos, E item);

/**
* Get an element from the list.
* <br>pre: 0 <= pos < size()
* <br>post: return the item at pos
* @param pos specifies which element to get
* @return the element at the specified position in the list
*/
E get(int pos);


/**
* Remove an element in the list based on position.
* <br>pre: 0 <= pos < size()
* <br>post: size() = old size() - 1, all elements of
* list with a positon > pos have a position = old position - 1
* @param pos the position of the element to remove from the list
* @return the data at position pos
*/
E remove(int pos);

/**
* Remove the first occurrence of obj in this list.
* Return <tt>true</tt> if this list changed as a result of this call, <tt>false</tt> otherwise.
* <br>pre: none
* <br>post: if obj is in this list the first occurence has been removed and size() = old size() - 1.
* If obj is not present the list is not altered in any way.
* @param obj The item to remove from this list.
* @return Return <tt>true</tt> if this list changed as a result of this call, <tt>false</tt> otherwise.
*/
boolean remove(E obj);

/**
* Return a sublist of elements in this list from <tt>start</tt> inclusive to <tt>stop</tt> exclusive.
* This list is not changed as a result of this call.
* <br>pre: <tt>0 <= start < size(), start <= stop <= size()</tt>
* <br>post: return a list whose size is stop - start and contains the elements at positions start through stop - 1 in this list.
* @param start index of the first element of the sublist.
* @param stop stop - 1 is the index of the last element of the sublist.
* @return a list with <tt>stop - start</tt> elements, The elements are from positions <tt>start</tt> inclusive to
* <tt>stop</tt> exclusive in this list.
*/
IList<E> getSubList(int start, int stop);

/**
* Return the size of this list. In other words the number of elements in this list.
* <br>pre: none
* <br>post: return the number of items in this list
* @return the number of items in this list
*/
int size();

/**
* Find the position of an element in the list.
* <br>pre: none
* <br>post: return the index of the first element equal to item
* or -1 if item is not present
* @param item the element to search for in the list
* @return return the index of the first element equal to item or a -1 if item is not present
*/
int indexOf(E item);

/**
* find the position of an element in the list starting at a specified position.
* <br>pre: 0 <= pos < size()
* <br>post: return the index of the first element equal to item starting at pos
* or -1 if item is not present from position pos onward
* @param item the element to search for in the list
* @param pos the position in the list to start searching from
* @return starting from the specified position return the index of the first element equal to item or a -1 if item is not present between pos and the end of the list
*/
int indexOf(E item, int pos);

/**
* return the list to an empty state.
* <br>pre: none
* <br>post: size() = 0
*/
void makeEmpty();

/**
* return an Iterator for this list.
* <br>pre: none
* <br>post: return an Iterator object for this List
*/
Iterator<E> iterator();

/**
* Remove all elements in this list from <tt>start</tt> inclusive to <tt>stop</tt> exclusive.
* <br>pre: <tt>0 <= start < size(), start <= stop <= size()</tt>
* <br>post: <tt>size() = old size() - (stop - start)</tt>
* @param start position at beginning of range of elements to be removed
* @param stop stop - 1 is the position at the end of the range of elements to be removed
*/
void removeRange(int start, int stop);

/**
* Return a String version of this list enclosed in
* square brackets, []. Elements are in
* are in order based on position in the
* list with the first element
* first. Adjacent elements are seperated by comma's
* @return a String representation of this IList
*/
public String toString();

/**
* Determine if this IList is equal to other. Two
* ILists are equal if they contain the same elements
* in the same order.
* @return true if this IList is equal to other, false otherwise
*/
public boolean equals(Object other);
}
Loading

0 comments on commit 535948d

Please sign in to comment.