forked from Berkeley-CS61B/skeleton-sp21
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
338 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
package deque; | ||
/** second part of project1A. | ||
* deque implemented by array | ||
* @author FlyingPig | ||
*/ | ||
public class ArrayDeque<T> implements Deque<T> { | ||
|
||
/** array to save data.*/ | ||
private T[] array; | ||
/** size of the deque. */ | ||
private int size; | ||
|
||
/** size of the array. */ | ||
private int length; | ||
|
||
/** front index. */ | ||
private int front; | ||
|
||
/** last index. */ | ||
private int last; | ||
|
||
/** constructor for ArrayDeque. */ | ||
public ArrayDeque() { | ||
array = (T[]) new Object[8]; | ||
size = 0; | ||
length = 8; | ||
front = 4; | ||
last = 4; | ||
} | ||
|
||
/** decide if the deque is empty. | ||
* @return true if the deque is empty, vice versa. | ||
*/ | ||
@Override | ||
public boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
/** return the size of the deque. */ | ||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
/** return the "index - 1". | ||
* @param index index | ||
*/ | ||
private int minusOne(int index) { | ||
if (index == 0) { | ||
return length - 1; | ||
} | ||
return index - 1; | ||
} | ||
|
||
/** return the "index + 1". | ||
* @param index index | ||
*/ | ||
private int plusOne(int index, int module) { | ||
index %= module; | ||
if (index == module - 1) { | ||
return 0; | ||
} | ||
return index + 1; | ||
} | ||
|
||
private void grow() { | ||
T[] newArray = (T[]) new Object[length * 2]; | ||
int ptr1 = front; | ||
int ptr2 = length; | ||
while (ptr1 != last) { | ||
newArray[ptr2] = array[ptr1]; | ||
ptr1 = plusOne(ptr1, length); | ||
ptr2 = plusOne(ptr2, length * 2); | ||
} | ||
front = length; | ||
last = ptr2; | ||
array = newArray; | ||
length *= 2; | ||
} | ||
|
||
private void shrink() { | ||
T[] newArray = (T[]) new Object[length / 2]; | ||
int ptr1 = front; | ||
int ptr2 = length / 4; | ||
while (ptr1 != last) { | ||
newArray[ptr2] = array[ptr1]; | ||
ptr1 = plusOne(ptr1, length); | ||
ptr2 = plusOne(ptr2, length / 2); | ||
} | ||
front = length / 4; | ||
last = ptr2; | ||
array = newArray; | ||
length /= 2; | ||
} | ||
|
||
/** add one item at the front of the deque. | ||
* @param item the item we want to add | ||
*/ | ||
@Override | ||
public void addFirst(T item) { | ||
if (size == length - 1) { | ||
grow(); | ||
} | ||
front = minusOne(front); | ||
array[front] = item; | ||
size++; | ||
} | ||
|
||
/** add one item at the end of the deque. | ||
* @param item item we want to add | ||
*/ | ||
@Override | ||
public void addLast(T item) { | ||
if (size == length - 1) { | ||
grow(); | ||
} | ||
array[last] = item; | ||
last = plusOne(last, length); | ||
size++; | ||
} | ||
|
||
/** remove the first item. | ||
* @return the removed first item | ||
*/ | ||
@Override | ||
public T removeFirst() { | ||
if (length >= 16 && length / size >= 4) { | ||
shrink(); | ||
} | ||
if (size == 0) { | ||
return null; | ||
} | ||
T ret = array[front]; | ||
front = plusOne(front, length); | ||
size--; | ||
return ret; | ||
} | ||
|
||
/** remove the last item. | ||
* @return the removed last item | ||
*/ | ||
@Override | ||
public T removeLast() { | ||
if (length >= 16 && length / size >= 4) { | ||
shrink(); | ||
} | ||
if (size == 0) { | ||
return null; | ||
} | ||
last = minusOne(last); | ||
size--; | ||
return array[last]; | ||
} | ||
|
||
/** return the item indexed at index. | ||
* @param index index | ||
*/ | ||
@Override | ||
public T get(int index) { | ||
if (index >= size) { | ||
return null; | ||
} | ||
int ptr = front; | ||
for (int i = 0; i < index; i++) { | ||
ptr = plusOne(ptr, length); | ||
} | ||
return array[ptr]; | ||
} | ||
|
||
/** print the entire deque from front to end. */ | ||
@Override | ||
public void printDeque() { | ||
int ptr = front; | ||
while (ptr != last) { | ||
System.out.print(array[ptr] + " "); | ||
ptr = plusOne(ptr, length); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package deque; | ||
import java.util.Comparator; | ||
|
||
public class MaxArrayDeque<T> extends ArrayDeque<T>{ | ||
public Comparator<T> comparator; | ||
public MaxArrayDeque(Comparator<T> c) { | ||
super(); | ||
comparator = c; | ||
} | ||
|
||
public T max() { | ||
T maxOne = null; | ||
if(this.isEmpty()) { | ||
return null; | ||
} | ||
if(this.size() == 1) { | ||
maxOne = this.get(0); | ||
} | ||
for(int i = 0; i < this.size() - 1; i++) { | ||
maxOne = this.get(i); | ||
if(comparator.compare(maxOne,this.get(i +1)) < 0) { | ||
maxOne = this.get(i + 1); | ||
} | ||
} | ||
return maxOne; | ||
} | ||
|
||
public T max(Comparator<T> c) { | ||
comparator = c; | ||
T maxOne = null; | ||
if(this.isEmpty()) { | ||
return null; | ||
} | ||
if(this.size() == 1) { | ||
maxOne = this.get(0); | ||
} | ||
for(int i = 0; i < this.size() - 1; i++) { | ||
maxOne = this.get(i); | ||
if(comparator.compare(maxOne,this.get(i +1)) < 0) { | ||
maxOne = this.get(i + 1); | ||
} | ||
} | ||
return maxOne; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.