Sayfalar

17 Eylül 2015 Perşembe

Circular List Implementation

Benim yazdığım bir doubly circularly linked list implementation'ı aşağıda verilmiştir. Yine bir önceki paylaşımımdaki Position.java sınıfı kullanılmıştır. Bu paylaşımın da amacı General Tree Implementation'ında olduğu gibi, ileriki paylaşımlarda kullanacak olmam.

CircularList.java


package datastructures.linkedlists;

import java.util.ArrayList;
import java.util.Iterator;

import datastructures.Position;

public class CircularList<E> implements Iterable<Position<E>>{
 
 private static class Node<E> implements Position<E>{

  private E element; 

  private Node<E> prev;
  private Node<E> next;

  public Node(E element, Node<E> prev, Node<E> next) {
   this.element = element;
   this.prev = prev;
   this.next = next;
  }

  @Override
  public E getElement() {
   return element;
  }
  
  public void setElement(E element) {
   this.element = element;
  }

  public Node<E> getPrev() {
   return prev;
  }
  
  public void setPrev(Node<E> prev) {
   this.prev = prev;
  }

  public Node<E> getNext() {
   return next;
  }

  public void setNext(Node<E> next) {
   this.next = next;
  }
  
 } 
 
 private int size;
 private Node<E> head;
 
 public CircularList() {
  
 }
 
 public CircularList(int size) {
  for (int i = 0; i < size; i++)
   addLast(null);
 }
 
 public CircularList(E[] array) {
  for (E e: array) {
   addLast(e);
  }
 }
 
 private Node<E> validate(Position<E> p) throws IllegalArgumentException {
  if (!(p instanceof Node)) 
   throw new IllegalArgumentException("Not a valid position type");     

  return (Node<E>)p;
 }
 
 public int size() {
  return size;
 }

 public boolean isEmpty() {
  return size == 0;
 }
 
 public Position<E> first() {
  return head;
 }
 
 public Position<E> last() {
  return head.getPrev();
 }
 
 public Position<E> before(Position<E> p) {
  return validate(p).getPrev();
 }
 
 public Position<E> after(Position<E> p) {
  return validate(p).getNext();
 }
 
 private void createHead(E e) {
  head = new Node<>(e, null, null);
  head.setNext(head);
  head.setPrev(head);
  size = 1;
 }
 
 private void addBetween(E ele, Node<E> pred, Node<E> succ) {
  Node<E> newer = new Node<>(ele, pred, succ);
  pred.setNext(newer);
  succ.setPrev(newer);
  
  size++;
 }
 
 public void addBefore(Position<E> p, E e) {
  Node<E> node = validate(p);
  addBetween(e, node.getPrev(), node);
  
 }
 
 public void addAfter(Position<E> p, E e) {
  Node<E> node = validate(p);
  addBetween(e, node, node.getNext());
 }
  
 public void addFirst(E e) {
  if (isEmpty())  
   createHead(e);
  else  
   addBefore(head, e);
  
  head = head.getPrev();
  
 }
 
 public void addLast(E e) {
  if (isEmpty()) 
   createHead(e);
  else  
   addAfter(head.getPrev(), e);
 }
 
 public void rotateRight() {
  head = head.getPrev();
 }
 
 public void rotateLeft() {
  head = head.getNext();
 }
 
 public void rotateRight(int r) {
  for (int i = 0; i < r; i++)
   rotateRight();
 }
 
 public void rotateLeft(int r) {
  for (int i = 0; i < r; i++) 
   rotateLeft();
 }
 
 public Position<E> get(int i) {
  if (i > size)
   throw new IndexOutOfBoundsException();
  Node<E> current = head;
  
  for (int j = 0; j < i; j++)
   current = current.getNext();
  
  return current;
 }
 
 public void set(int i, E e) {
  validate(get(i)).setElement(e);
 }
 
 public String toString() {
  Node<E> current = head;  
  StringBuilder string = new StringBuilder();
 
  do {
   string.append(current.getElement() + " ");
   current = current.getNext();
  } while(current != head);
  
  return string.toString();
 }

 @Override
 public Iterator<Position<E>> iterator() {
  Node<E> current = head;  
  ArrayList<Position<E>> list = new ArrayList<>();
  
  do {
   list.add(current);
   current = current.getNext();
  } while(current != head);
  
  return list.iterator();
 }
 
}

Hiç yorum yok: