Skip to content

Commit

Permalink
algoJava: c2 s3 finish
Browse files Browse the repository at this point in the history
  • Loading branch information
AliNisarAhmed committed Apr 10, 2024
1 parent 15adc21 commit b632244
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 1 deletion.
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.ali.algojava.chapter1.section3;

import java.util.ConcurrentModificationException;
import java.util.Iterator;

public class Stack<Item> implements Iterable<Item> {
private Node first; // top of stack
private int N; // size of stack
private Node last;
// 1.3.50
private int operationCount;

private class Node {
Item item;
Expand All @@ -13,6 +17,7 @@ private class Node {

public Stack() {
first = null;
last = null;
N = 0;
}

Expand All @@ -31,8 +36,21 @@ public Stack(Stack<Item> stack2) {
}
}

// 1.3.47
public void concat(Stack<Item> stack2) {
if (stack2.isEmpty()) {
return;
}
stack2.last.next = first;
N += stack2.size();
first = stack2.first;
stack2.N = 0;
stack2.first = null;
stack2.last = null;
}

public boolean isEmpty() {
return first == null;
return N == 0;
}

public int size() {
Expand All @@ -44,7 +62,11 @@ public void push(Item item) {
first = new Node();
first.item = item;
first.next = oldFirst;
if (first.next == null) {
last = first;
}
N++;
operationCount++;
}

public Item pop() {
Expand All @@ -54,6 +76,10 @@ public Item pop() {
Item item = first.item;
first = first.next;
N--;
if (isEmpty()) {
last = null;
}
operationCount--;
return item;
}

Expand All @@ -73,14 +99,23 @@ public Iterator<Item> iterator() {

private class ListIterator implements Iterator<Item> {
private Node current = first;
private int operationCountSnapshot = operationCount;

@Override
public boolean hasNext() {
if (operationCountSnapshot != operationCount) {
throw new ConcurrentModificationException(
"stack modified during iteration");
}
return current != null;
}

@Override
public Item next() {
if (operationCountSnapshot != operationCount) {
throw new ConcurrentModificationException(
"stack modified during iteration");
}
Item item = current.item;
current = current.next;
return item;
Expand Down
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;
}
}
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@ public void test_copy() {
assertEquals(4, s2.peek());
assertEquals(2, s1.peek());
}

@Test
public void test_concat() {
Stack<Integer> s1 = new Stack<Integer>();
s1.push(1);
s1.push(2);
s1.push(3);

Stack<Integer> s2 = new Stack<Integer>();
s2.push(4);
s2.push(5);
s2.push(6);

s1.concat(s2);
assertEquals(6, s1.size());
assertEquals(6, s1.peek());
}
}

0 comments on commit b632244

Please sign in to comment.