SinglyCircularLinkedList - pred = pred.next if(pred.next == null return false pred.next = pred.next.next if(pred.next == null last = pred last = first

SinglyCircularLinkedList - pred = pred.next if(pred.next ==...

This preview shows page 1 - 3 out of 3 pages.

class SinglyCircularLinkedList { private class Node { String value; Node next; Node(String val, Node n) { value = val; next = n; } Node(String val) { this(val, null); } } private Node first; private Node last; public SinglyCircularLinkedList() { first = null; last = null; } public boolean isEmpty() { return first == null; } public int size() { int count = 0; Node p = first; while (p != null) { count ++; p = p.next; } return count; } public void add(String e) { if (isEmpty()) { first = new Node(e); last = first; last.next = first; } else { last.next = new Node(e); last = last.next; last = first; } } public void add(int index, String e) {
Image of page 1
if (index < 0 || index > size()) { String message = String.valueOf(index); throw new IndexOutOfBoundsException(message); } if (index == 0) { first = new Node(e, first); if (last == null) last = first; return; } Node pred = first; for (int k = 1; k <= index - 1; k++) { pred = pred.next; } pred.next = new Node(e, pred.next); if (pred.next.next == null) last = pred.next; last = first; } public boolean remove(String element) { if (isEmpty()) return false; if (element.equals(first.value)) { first = first.next; if (first == null) last = null; return true; } Node pred = first; while (pred.next != null && !pred.next.value.equals(element))
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 3 pages?

  • Spring '14
  • Void type, carol

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture