Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SingleLP;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class SingleLinkedListUpg3<E> implements Iterable<E> {
- /**
- * Node Class, represent an element in the singleLinkedList
- * contains the element and the Node to the next element in the list
- */
- private static class Node<E>{
- private E data;
- private Node<E> next;
- public Node(E data, Node<E> next) {
- this.data = data;
- this.next = next;
- }
- }
- /**
- * Itr class, iterator for traversing the singleLinkedList
- */
- private class Itr implements Iterator<E> {
- Node<E> c, bc, bbc;
- boolean removable;
- public Itr(Node<E> start){
- c = start;
- bc = null;
- bbc = null;
- removable = false;
- }
- @Override
- public boolean hasNext() {
- return c!=null;
- }
- @Override
- public E next() {
- if(c == null) throw new NoSuchElementException();
- if(!removable) {
- bbc = bc;
- bc = c;
- } else {
- bc = c;
- removable = false;
- }
- E data = c.data;
- c = c.next;
- return data;
- }
- @Override
- public void remove() {
- if(removable) throw new IllegalStateException();
- if(bbc == null){
- bc = null;
- head = head.next;
- } else if (size==1){
- head = null;
- } else {
- bbc.next = c;
- }
- size--;
- removable = true;
- }
- }
- /**
- * Start of SingleLinkedList Methods, fields, constructors etc.
- */
- private Node<E> head;
- private int size;
- public SingleLinkedListUpg3(){
- head = null;
- size = 0;
- }
- public void add(int index, E item){
- if(index<0 || index > size)
- throw new IndexOutOfBoundsException(Integer.toString(index));
- if(index==0)
- addFirst(item);
- else{
- Node<E> node = getNode(index - 1);
- addAfter(node, item);
- }
- }
- private void addFirst(E item){
- head = new Node<E>(item,head);
- size++;
- }
- private Node<E> getNode(int index){
- Node<E> node = head;
- for (int i = 0; i < index && node!=null ; i++) {
- node = node.next;
- }
- return node;
- }
- private void addAfter(Node<E> node, E item){
- node.next = new Node<>(item, node.next);
- size++;
- }
- public E get(int index){
- if(index<0 || index >= size)
- throw new IndexOutOfBoundsException(Integer.toString(index));
- Node<E> node = getNode(index);
- return node.data;
- }
- public E remove(int index){
- if(index<0 || index >= size)
- throw new IndexOutOfBoundsException(Integer.toString(index));
- E removedData;
- if(size == 1){
- removedData = head.data;
- head = null;
- } else {
- if(index == 0){
- removedData = head.data;
- head = head.next;
- } else{
- Node<E> node = getNode(index-1);
- removedData = node.next.data;
- node.next = node.next.next;
- }
- }
- size--;
- return removedData;
- }
- public int size(){
- return size;
- }
- public boolean add(E item){
- add(size, item);
- return true;
- }
- public Iterator<E> iterator(){
- return new Itr(head);
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder("[");
- if(size!=0){
- Node<E> p = head;
- if(p!=null){
- while (p.next!=null){
- sb.append(p.data.toString());
- sb.append(" ==> ");
- p = p.next;
- }
- sb.append(p.data.toString());
- }
- sb.append("]");
- sb.append("\nSize: ").append(size).append(" Head: ").append(head.data);
- } else sb.append("]");
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement