forked from DwarfPorter/Interview
-
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
07aed62
commit 868588d
Showing
2 changed files
with
86 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,13 @@ | ||
package ru.geekbrain; | ||
public class Main { | ||
public static void main(String[] args) { | ||
VectorList<Integer> vectorList = new VectorList<>(); | ||
vectorList.add(1); | ||
vectorList.add(4); | ||
vectorList.add(6); | ||
vectorList.add(4); | ||
for(Integer item : vectorList){ | ||
System.out.println(item); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
Lesson3/Algorithms/VectorList/src/ru/geekbrain/VectorList.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,73 @@ | ||
package ru.geekbrain; | ||
import java.util.Iterator; | ||
public class VectorList<E> implements Iterable<E> { | ||
private Node<E> node = null; | ||
private int counter = 0; | ||
|
||
public VectorList(){ | ||
node = new Node<>(); | ||
} | ||
|
||
public int size(){ | ||
return counter; | ||
} | ||
public void add(E element){ | ||
if (counter == 0){ // первый узел надо обработать отдельно, иначе в первом узле элемент будет балластом | ||
node.set(element); | ||
} | ||
else{ | ||
node.add(element); | ||
} | ||
counter++; // увеличим счетчик элементов | ||
} | ||
@Override | ||
public Iterator<E> iterator() { | ||
return new VectorIterator(node); | ||
} | ||
// Узел с элементом, сделан внутренним, чтобы не светить его наружу | ||
private class Node<E>{ | ||
private E element; | ||
private Node<E> next = null; | ||
|
||
Node(){} | ||
Node(E element){ | ||
set(element); | ||
} | ||
void set(E element){ | ||
this.element = element; | ||
} | ||
void add(E element){ | ||
if (isNext()){ // если существует следующий узел, передадим ему эстафету | ||
next.add(element); | ||
} | ||
else{ // иначе создадим следующий узел | ||
next = new Node<>(element); | ||
} | ||
} | ||
E get(){ | ||
return element; | ||
} | ||
boolean isNext(){ | ||
return next != null; | ||
} | ||
} | ||
// Итератор, светить наружу его тоже не хочется, он слишком специфичный. | ||
private class VectorIterator implements Iterator<E>{ | ||
private Node<E> current; | ||
private boolean isFirst = true; // первый узел надо обработать отдельно | ||
VectorIterator(Node<E> node){ | ||
current = node; | ||
} | ||
@Override | ||
public boolean hasNext() { | ||
if (counter == 1 && isFirst) return true; // отдельно обработать один элемент (потому как у него нет next) | ||
return current.isNext(); | ||
} | ||
@Override | ||
public E next() { | ||
if (isFirst) isFirst = false; | ||
else current = current.next; | ||
return current.get(); | ||
} | ||
} | ||
} |