Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.NoSuchElementException;
- public class MyCircularLinkedList<E> implements MyList<E> {
- private Node<E> tail;
- private int size = 0;
- public MyCircularLinkedList() {
- }
- public MyCircularLinkedList(E[] objects) {
- for (int i = 0; i < objects.length; i++)
- add(objects[i]);
- }
- public E getFirst() {
- if (size == 0) {
- throw new NoSuchElementException();
- }
- else {
- return tail.next.element;
- }
- }
- public E getLast() {
- if (size == 0) {
- throw new NoSuchElementException();
- }
- else {
- return tail.element;
- }
- }
- public void addFirst(E e) {
- Node<E> newNode = new Node<>(e);
- if(size == 0) {
- newNode.next = newNode;
- tail = newNode;
- newNode.next = tail.next;
- }
- else{
- newNode.next=tail.next;
- tail.next=newNode;
- }
- size++;
- }
- public void addLast(E e) {
- Node<E> newNode = new Node<>(e);
- if (tail == null) {
- tail.next = tail = newNode;
- }
- else {
- newNode.next = tail.next;
- tail.next = newNode;
- tail = newNode;
- }
- size++;
- }
- @Override
- public void add(int index, E e) {
- if (index > size || index < 0) throw new IndexOutOfBoundsException();
- if (index == 0) {
- addFirst(e);
- }
- else if (index >= size) {
- addLast(e);
- }
- else {
- Node<E> current = tail.next;
- for (int i = 1; i < index; i++) {
- current = current.next;
- }
- Node<E> temp = current.next;
- current.next = new Node<>(e);
- (current.next).next = temp;
- size++;
- }
- }
- public E removeFirst() {
- if (size == 0) {
- throw new NoSuchElementException();
- }
- else {
- E temp = tail.next.element;
- tail.next = (tail.next).next;
- size--;
- if (tail.next == null) {
- tail = null;
- }
- return temp;
- }
- }
- public E removeLast() {
- if (size == 0) {
- throw new NoSuchElementException();
- }
- else if (size == 1) {
- E temp = tail.next.element;
- tail.next = tail = null;
- size = 0;
- return temp;
- }
- else {
- Node<E> current = tail.next;
- for (int i = 0; i < size - 2; i++) {
- current = current.next;
- }
- current.next = tail.next;
- E temp = tail.element;
- tail = current;
- size--;
- return temp;
- }
- }
- @Override
- public E remove(int index) {
- if (index < 0 || index >= size) {
- return null;
- }
- else if (index == 0) {
- return removeFirst();
- }
- else if (index == size - 1) {
- return removeLast();
- }
- else {
- Node<E> previous = tail.next;
- for (int i = 1; i < index; i++) {
- previous = previous.next;
- }
- Node<E> current = previous.next;
- previous.next = current.next;
- size--;
- return current.element;
- }
- }
- @Override
- public String toString() {
- StringBuilder result = new StringBuilder("[");
- if(tail!=null){
- Node<E> current = tail.next;
- for (int i = 0; i < size; i++) {
- result.append(current.element);
- current = current.next;
- if (i != size - 1) {
- result.append(", ");
- }
- }
- }
- result.append("]");
- return result.toString();
- }
- @Override
- public void clear() {
- size = 0;
- tail.next = tail = null;
- }
- @Override
- public boolean contains(Object e) {
- if (size == 0) { return false; }
- Node<E> current = tail.next;
- for (int i = 0; i < size; i++) {
- if (e.equals(current.element)) {
- return true;
- }
- current = current.next;
- }
- return false;
- }
- @Override
- public E get(int index) {
- if (index < 0 || index >= size) {
- throw new IndexOutOfBoundsException(index + "");
- }
- Node<E> current = tail.next;
- for (int i = 0; i < index; i++) {
- current = current.next;
- }
- return current.element;
- }
- @Override
- public int indexOf(Object e) {
- if (size == 0) { return -1; }
- Node<E> current = tail.next;
- for (int i = 0; i < size; i++) {
- if (e.equals(current.element)) { return i; }
- current = current.next;
- }
- return -1;
- }
- @Override
- public int lastIndexOf(E e) {
- if (size == 0) { return -1; }
- Node<E> current = tail.next;
- int lastIndex = -1;
- for (int i = 0; i < size; i++) {
- if (e.equals(current.element)) { lastIndex = i; }
- current = current.next;
- }
- return lastIndex;
- }
- @Override
- public E set(int index, E e) {
- if (index < 0 || index >= size) {
- throw new IndexOutOfBoundsException(index + "");
- }
- Node<E> current = tail.next;
- for (int i = 0; i < index; i++) {
- current = current.next;
- }
- E temp = current.element;
- current.element = e;
- return temp;
- }
- @Override
- public java.util.Iterator<E> iterator() {
- return new CircularLinkedListIterator();
- }
- private class CircularLinkedListIterator implements java.util.Iterator<E> {
- private Node<E> current = tail.next;
- private Node<E> temp = tail;
- @Override
- public E next() {
- E e = current.element;
- if(current.element == tail.element){
- current = null;
- }
- else {
- current = current.next;
- }
- return e;
- }
- @Override
- public boolean hasNext() {
- return (current!=null);
- }
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
- }
- private static class Node<E> {
- E element;
- Node<E> next;
- public Node(E element) {
- this.element = element;
- }
- }
- @Override
- public int size() {
- return size;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement