-
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
1 parent
15adc21
commit b632244
Showing
5 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
Java/AlgoJava/src/main/java/com/ali/algojava/chapter1/section3/QueueWithTwoStacks.java
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,57 @@ | ||
package com.ali.algojava.chapter1.section3; | ||
|
||
import java.util.Iterator; | ||
|
||
// 1.3.49 | ||
public class QueueWithTwoStacks<Item> implements Iterable<Item> { | ||
private Stack<Item> back; | ||
private Stack<Item> front; | ||
|
||
public int size() { | ||
return back.size() + front.size(); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return back.isEmpty() && front.isEmpty(); | ||
} | ||
|
||
public void enqueue(Item item) { | ||
back.push(item); | ||
} | ||
|
||
public Item dequeue() { | ||
if (!front.isEmpty()) { | ||
return front.pop(); | ||
} | ||
while (!back.isEmpty()) { | ||
front.push(back.pop()); | ||
} | ||
return front.pop(); | ||
} | ||
|
||
@Override | ||
public Iterator<Item> iterator() { | ||
return new QueueWithTwoStacksIterator(); | ||
} | ||
|
||
private class QueueWithTwoStacksIterator implements Iterator<Item> { | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return !front.isEmpty() || !back.isEmpty(); | ||
} | ||
|
||
@Override | ||
public Item next() { | ||
if (!front.isEmpty()) { | ||
return front.pop(); | ||
} | ||
|
||
while (!back.isEmpty()) { | ||
front.push(back.pop()); | ||
} | ||
|
||
return front.pop(); | ||
} | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
Java/AlgoJava/src/main/java/com/ali/algojava/chapter1/section3/TwoStacks.java
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,52 @@ | ||
package com.ali.algojava.chapter1.section3; | ||
|
||
// 1.3.48 | ||
public class TwoStacks<Item> { | ||
private Deque<Item> deque; | ||
private int rightStackSize; | ||
private int leftStackSize; | ||
|
||
public boolean isRightStackEmpty() { | ||
return rightStackSize == 0; | ||
} | ||
|
||
public boolean isLeftStackEmpty() { | ||
return leftStackSize == 0; | ||
} | ||
|
||
public int rightStackSize() { | ||
return rightStackSize; | ||
} | ||
|
||
public int leftStackSize() { | ||
return leftStackSize; | ||
} | ||
|
||
public void pushRight(Item item) { | ||
deque.pushRight(item); | ||
rightStackSize++; | ||
} | ||
|
||
public Item popRight() { | ||
if (isRightStackEmpty()) { | ||
return null; | ||
} | ||
Item result = deque.popRight(); | ||
rightStackSize--; | ||
return result; | ||
} | ||
|
||
public void pushLeft(Item item) { | ||
deque.pushLeft(item); | ||
leftStackSize++; | ||
} | ||
|
||
public Item pushLeft() { | ||
if (isLeftStackEmpty()) { | ||
return null; | ||
} | ||
Item result = deque.popLeft(); | ||
leftStackSize--; | ||
return result; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Java/AlgoJava/src/test/java/com/ali/algojava/chapter1/section3/QueueWithTwoStacksTest.java
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,22 @@ | ||
package com.ali.algojava.chapter1.section3; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class QueueWithTwoStacksTest { | ||
|
||
@Test | ||
public void test_queueCopy() { | ||
Queue<String> q1 = new Queue<String>(); | ||
q1.enqueue("a"); | ||
q1.enqueue("b"); | ||
q1.enqueue("c"); | ||
assertEquals(3, q1.size()); | ||
assertEquals("a", q1.dequeue()); | ||
assertEquals("b", q1.dequeue()); | ||
assertEquals("c", q1.dequeue()); | ||
assertTrue(q1.isEmpty()); | ||
} | ||
} |
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