-
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.
5. Очередь на двух стеках [#160 #182061]
- Loading branch information
Showing
4 changed files
with
148 additions
and
0 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,29 @@ | ||
package ru.j4j.collection; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class SimpleQueue<T> { | ||
private final SimpleStack<T> input = new SimpleStack<>(); | ||
private final SimpleStack<T> output = new SimpleStack<>(); | ||
int size; | ||
|
||
public T poll() { | ||
if (size == 0) { | ||
throw new NoSuchElementException("Queue is empty"); | ||
} | ||
for (int i = 0; i < size; i++) { | ||
output.push(input.pop()); | ||
} | ||
T result = output.pop(); | ||
size--; | ||
for (int i = 0; i < size; i++) { | ||
input.push(output.pop()); | ||
} | ||
return result; | ||
} | ||
|
||
public void push(T value) { | ||
input.push(value); | ||
size++; | ||
} | ||
} |
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,14 @@ | ||
package ru.j4j.collection; | ||
|
||
public class SimpleStack<T> { | ||
|
||
private final ForwardLinked<T> linked = new ForwardLinked<>(); | ||
|
||
public T pop() { | ||
return linked.deleteFirst(); | ||
} | ||
|
||
public void push(T value) { | ||
linked.addFirst(value); | ||
} | ||
} |
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,63 @@ | ||
package ru.j4j.collection; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class SimpleQueueTest { | ||
private SimpleQueue<Integer> queue; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
queue = new SimpleQueue<>(); | ||
queue.push(1); | ||
} | ||
|
||
@Test | ||
void whenPushPoll() { | ||
assertThat(queue.poll()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
void when2PushPoll() { | ||
queue.push(2); | ||
assertThat(queue.poll()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
void when2PushPollPushPoll() { | ||
queue.poll(); | ||
queue.push(2); | ||
assertThat(queue.poll()).isEqualTo(2); | ||
} | ||
|
||
@Test | ||
void whenEmptyPoll() { | ||
SimpleQueue<Integer> queue = new SimpleQueue<>(); | ||
assertThatThrownBy(queue::poll) | ||
.isInstanceOf(NoSuchElementException.class) | ||
.hasMessageContaining("Queue is empty"); | ||
} | ||
|
||
@Test | ||
void when2PushPollPushPollEmpty() { | ||
queue.poll(); | ||
queue.push(2); | ||
queue.poll(); | ||
assertThatThrownBy(queue::poll) | ||
.isInstanceOf(NoSuchElementException.class) | ||
.hasMessageContaining("Queue is empty"); | ||
} | ||
|
||
@Test | ||
void whenPushPushPollAndPush() { | ||
queue.push(2); | ||
queue.poll(); | ||
queue.push(3); | ||
assertThat(queue.poll()).isEqualTo(2); | ||
} | ||
} |
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,42 @@ | ||
package ru.j4j.collection; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class SimpleStackTest { | ||
private SimpleStack<Integer> stack; | ||
|
||
@BeforeEach | ||
void init() { | ||
stack = new SimpleStack<>(); | ||
stack.push(1); | ||
stack.push(2); | ||
} | ||
|
||
@Test | ||
void whenPushThenPoll() { | ||
stack.push(3); | ||
stack.push(4); | ||
assertThat(stack.pop()).isEqualTo(4); | ||
} | ||
|
||
@Test | ||
void whenPushPollThenPushPoll() { | ||
stack.pop(); | ||
stack.push(3); | ||
assertThat(stack.pop()).isEqualTo(3); | ||
} | ||
|
||
@Test | ||
void whenPushPushThenPollPoll() { | ||
stack.pop(); | ||
assertThat(stack.pop()).isEqualTo(1); | ||
assertThatThrownBy(stack::pop) | ||
.isInstanceOf(NoSuchElementException.class); | ||
} | ||
} |