Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the answer of Exercise 1.3.33 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions 1-Fundamentals/1-3-BagsQueuesStacks/Ex_1_3_33_Deque.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 1.3.33 Deque. A double-ended queue or deque (pronounced “deck”) is like a stack or
* a queue but supports adding and removing items at both ends.
*/
public class Deque<Item> {
private class Node {
Node previous;
Node next;
Item item;
}

private Node Left; //the leftmost item of the queue
private Node Right; // the rightmost item of the queue
private int N;

Deque (){ //constructor,create an empty deque
Left = null;
Right = null;
N = 0;
}
public boolean isEmpty(){ //is the deque empty?
return N == 0;
}
public int size(){ //number of items in the deque
return N;
}
public void pushLeft(Item item) { //add an item to the left end
Node oldLeft = Left;
Left = new Node();
Left.item = item;
Left.previous = null;
if (isEmpty()){
Right = Left;
}else{
Left.next = oldLeft;
oldLeft.previous = Left;
}
N++;
}
public void pushRight(Item item){ //add an item to the right end
Node oldRight = Right;
Right = new Node();
Right.item = item;
Right.next = null;
if (isEmpty()){
Left = Right;
}else{
oldRight.next = Right;
Right.previous = Right;
}
N++;
}
public Item popLeft(){ //remove an item from the left end
if (isEmpty()){
return null;
}else{
Item item = Left.item;
Left = Left.next;
N--;
if(isEmpty()){
Right = null;
}else {
Left.previous = null;
}
return item;
}
}
public Item popRight(){ //remove an item from the right end
if (isEmpty()){
return null;
}else{
Item item = Right.item;
Right = Right.next;
N--;
if(isEmpty()){
Left = null;
}else {
Right.next = null;
}
return item;
}
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Write a class ResizingArrayDeque that uses a resizing array to implement this API.
*/
public class ResizingArrayDequue<Item> {
private Item[] a;
private int N =0;
ResizingArrayDequue(){ //constructor
a = (Item[]) new Object[1];
}
public boolean isEmpty(){
return N == 0;
}
public int size(){
return N;
}
private void resize(int max){
Item[] temp = (Item[]) new Object[max];
for (int i = 0;i <N;i++){
temp[i] = a[i];
}
a =temp;
}
public void pushLeft(Item item){
if (N == a.length){
resize(a.length*2);
}
Item[] temp = (Item[]) new Object[a.length+1];
temp[0] = item;
for (int i = 1;i<a.length+1;i++){
temp[i] = a[i-1];
}
a = temp;
N++;
}
public void pushRight(Item item){
if (N == a.length){
resize(a.length*2);
}
a[N++] = item;
}
public Item popLeft(){
Item item = a[0];
Item[] temp = (Item[]) new Object[a.length-1];
for (int i = 0;i<a.length;i++){
temp[i] = a[i+1];
}
a = temp;
if(N > 0 && N == a.length/4){
resize(a.length/2);
}
N--;
return item;
}
public Item popRight(){
Item item = a[--N];
a[N] = null;
if(N > 0 && N == a.length/4){
resize(a.length/2);
}
return item;
}
}