diff --git a/1-Fundamentals/1-3-BagsQueuesStacks/Ex_1_3_33_Deque.java b/1-Fundamentals/1-3-BagsQueuesStacks/Ex_1_3_33_Deque.java new file mode 100644 index 0000000..6974d92 --- /dev/null +++ b/1-Fundamentals/1-3-BagsQueuesStacks/Ex_1_3_33_Deque.java @@ -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 { + 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; + } + } + + + +} diff --git a/1-Fundamentals/1-3-BagsQueuesStacks/Ex_1_3_33_ResizingArrayDequue.java b/1-Fundamentals/1-3-BagsQueuesStacks/Ex_1_3_33_ResizingArrayDequue.java new file mode 100644 index 0000000..f06c1e0 --- /dev/null +++ b/1-Fundamentals/1-3-BagsQueuesStacks/Ex_1_3_33_ResizingArrayDequue.java @@ -0,0 +1,62 @@ +/* + * Write a class ResizingArrayDeque that uses a resizing array to implement this API. + */ +public class ResizingArrayDequue { + 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 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; + } +} \ No newline at end of file