File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .NoSuchElementException ;
2+ import java .util .Iterator ;
3+
14/**
25 * Очередь: FIFO = First Input First Output
36 */
4- public class Queue <T > {
7+ public class Queue <T > implements Iterable <T >{
8+ /**
9+ * head - начало списка
10+ * tail - конец списка
11+ */
12+ Element <T > head =null , tail =null ;
13+
14+ public Iterator <T > iterator () {
15+ return new MyIterator ();
16+ }
17+
518 /**
619 * Добавить в конец очереди
720 *
821 * @param v значение
922 */
10- public void put (T v ) {
23+ public void put (T value ) {
1124 // TODO: реализовать
25+ Element <T > newElement = new Element <T >(value );
26+
27+ if (tail ==null ) {
28+ head =tail =newElement ;
29+ return ;
30+ }
31+ tail .next =newElement ;
32+ tail =newElement ;
1233 }
1334
1435 /**
@@ -18,14 +39,44 @@ public void put(T v) {
1839 */
1940 public T get () {
2041 // TODO: реализовать
21- return null ;
42+
43+ if (head !=null ) {
44+ Element <T > newElement = head ;
45+ head =head .next ;
46+ return newElement .value ;}
47+ throw new NoSuchElementException ("Пустая очередь" );
2248 }
2349
50+
2451 /**
2552 * Элемент очереди
2653 */
27- class Element {
54+ public static class Element < T > {
2855 T value ;
29- Element next ;
56+ Element next =null ;
57+
58+ public Element (T value ) {
59+ this .value = value ;
60+ }
61+ }
62+
63+ public class MyIterator implements Iterator <T > {
64+ private Element <T > cur ;
65+
66+ public MyIterator () {
67+ this .cur =head ;
68+ }
69+
70+ public boolean hasNext () {
71+ return cur !=null ;
72+ }
73+
74+ public T next () {
75+ if (cur == null )
76+ throw new IndexOutOfBoundsException ("Список кончился" );
77+ T value = cur .value ;
78+ cur = cur .next ;
79+ return value ;
80+ }
3081 }
3182}
Original file line number Diff line number Diff line change 1+ import java .util .EmptyStackException ;
12/**
23 * Стек: LIFO = Last Input First Output
34 */
45public 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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ public class StackTest {
1414 */
1515 @ Test
1616 public void testIntStack () {
17- Stack <Integer > stack = new Stack <>();
17+ Stack <Integer > stack = new Stack <Integer >();
1818 stack .push (1 );
1919 stack .push (2 );
2020 stack .push (3 );
@@ -28,7 +28,7 @@ public void testIntStack() {
2828 */
2929 @ Test
3030 public void testStringStack () {
31- Stack <String > stack = new Stack <>();
31+ Stack <String > stack = new Stack <String >();
3232 stack .push ("Hello" );
3333 stack .push ("world" );
3434 assertEquals ("world" , stack .pop ());
@@ -42,7 +42,7 @@ public void testStringStack() {
4242 public void testEmptyStack () {
4343 class MyClass {
4444 }
45- Stack <MyClass > stack = new Stack <>();
45+ Stack <MyClass > stack = new Stack <MyClass >();
4646 stack .pop ();
4747 }
4848}
You can’t perform that action at this time.
0 commit comments