Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Problema3;
- import java.util.Iterator;
- public class LinkedList<T> {
- private static class Node<T> {
- T value;
- Node<T> next;
- Node(T value) {
- this.value = value;
- this.next = null;
- }
- Node(T value, Node<T> nextNode) {
- this.value = value;
- this.next = nextNode;
- }
- }
- public class ListIterator implements Iterator<T> {
- Node<T> iterator = LinkedList.this.first;
- @Override
- public boolean hasNext() {
- return iterator != null;
- }
- @Override
- public T next() {
- Node<T> prv = iterator;
- iterator = iterator.next;
- return prv.value;
- }
- }
- Node<T> first, last;
- //Insereaza un nod la inceputul listei
- public void addFirst(T data) {
- Node<T> newNode = new Node<T>(data);
- if (this.first == null && this.last == null) {
- this.first = newNode;
- this.last = newNode;
- return;
- }
- newNode.next = this.first;
- this.first = newNode;
- }
- //Insereaza un nod la sfarsitul listei
- public void add(T data) {
- Node<T> newNode = new Node<T>(data);
- if (this.first == null && this.last == null) {
- this.first = newNode;
- this.last = newNode;
- return;
- }
- last.next = newNode;
- this.last = newNode;
- }
- //Returneaza primul nod din lista
- public T getNode() {
- return this.first.value;
- }
- //Returneaza un obiect de tip ListIterator
- public Iterator<T> iterator() {
- return new ListIterator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement