Реализована очередь с итератором, · AleksSPb/JavaStackQueue@879effa · GitHub
Skip to content

Commit 879effa

Browse files
committed
Реализована очередь с итератором,
Куча просто. Исправил тесты т.к. IDEA ругалась на недостаточную версию языка для использования <>, хотя в настройках стоит 8.
1 parent fc9872a commit 879effa

4 files changed

Lines changed: 88 additions & 14 deletions

File tree

‎src/main/java/Queue.java‎

Lines changed: 56 additions & 5 deletions

‎src/main/java/Stack.java‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1+
import java.util.EmptyStackException;
12
/**
23
* Стек: LIFO = Last Input First Output
34
*/
45
public class Stack<T> {
6+
/**
7+
*
8+
*/
9+
Element<T> head = null;
10+
511
/**
612
* Добавить на вершину стека
713
*
814
* @param v значение
915
*/
10-
public void push(T v) {
16+
public void push(T value) {
1117
// TODO: реализовать
18+
Element<T> cur = new Element<T>(value);
19+
if (head==null) {
20+
head=cur; return;}
21+
cur.next=head;
22+
head=cur;
23+
return;
1224
}
1325

1426
/**
@@ -17,15 +29,26 @@ public void push(T v) {
1729
* @return значение
1830
*/
1931
public T pop() {
32+
if (head!=null)
33+
{
34+
Element<T> cur;
35+
cur=head;
36+
head=head.next;
37+
return cur.value;
38+
}
2039
// TODO: реализовать
21-
return null;
40+
throw new EmptyStackException();
2241
}
2342

2443
/**
2544
* Элемент стека
2645
*/
27-
class Element {
46+
class Element<T> {
2847
T value;
29-
Element next;
48+
Element<T> next=null;
49+
50+
public Element(T value){
51+
this.value=value;
52+
}
3053
}
3154
}

‎src/test/java/QueueTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class QueueTest {
1414
*/
1515
@Test
1616
public void testStringQueue() {
17-
Queue<String> queue = new Queue<>();
17+
Queue<String> queue = new Queue<String>();
1818
queue.put("Hello");
1919
queue.put("world");
2020
assertEquals("Hello", queue.get());
@@ -28,7 +28,7 @@ public void testStringQueue() {
2828
public void testEmptyQueue() {
2929
class MyClass {
3030
}
31-
Queue<MyClass> queue = new Queue<>();
31+
Queue<MyClass> queue = new Queue<MyClass>();
3232
queue.get();
3333
}
3434
}

‎src/test/java/StackTest.java‎

Lines changed: 3 additions & 3 deletions

0 commit comments

Comments
 (0)