Node - pred.next = middle middle.prev = pred middle.next = succ succ.prev = middle size Node removeFirst(String element Node remove = head if(size == 0

Node - pred.next = middle middle.prev = pred middle.next =...

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

class Node{ String item; Node prev, next; Node(String newItem){ item = newItem; prev = next =null; } } class CircularDoublyLinkedList{ private Node head; private Node tail; private int size; CircularDoublyLinkedList(){ } public void addFirst(String e){ if(size == 0) { tail =new Node(e); head = tail; } else if(size == 1){ head =new Node(e); head.next = tail; head.prev = tail; tail.prev = head; tail.next = head; } else{ head.prev = new Node(e); head.prev.next = head; head.prev.prev = tail; head = head.prev; tail.next = head; } size ++; } public void addLast(String e){ if(size == 0) { tail = new Node(e); head = tail; } else if(size == 1) { head.next = new Node(e); head.prev = head.next; head.next.prev = head; head.next.next = head; tail = head.next; } else { tail.next = new Node(e); tail.next.prev = tail; tail.next.next = head; head.prev = tail.next; tail = tail.next; } size ++;
Image of page 1
} public void add(int index, String e){ if(index == 0) addFirst(e); else if(index >= size) addLast(e); else{ Node pred = head; for(int i = 1; i < index; i++) { pred = pred.next; } Node succ = pred.next; Node middle = new Node(e);
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, tail

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture