Implementation of a Circularly Doubly Linked List in Java

Verified

Added on  2019/09/30

|2
|290
|178
Practical Assignment
AI Summary
This assignment presents a Java implementation of a Circularly Doubly Linked List. The solution includes the definition of a `Node` class and a `CircularlyDoublyLinkedList` class. The `CircularlyDoublyLinkedList` class contains methods such as `size()`, `isEmpty()`, `first()`, `last()`, `rotate()`, `addFirst()`, `addLast()`, and `removeFirst()`. These methods allow for the manipulation of the linked list, including adding and removing elements, checking its size and emptiness, and rotating the list. This assignment can be useful for understanding and implementing data structures in Java. Desklib provides access to this and other assignments, offering valuable resources for students studying data structures and algorithms.
Document Page
// here Node has 3 fields (element, next and previous)
public class CircularlyDoublyLinkedList<E> {
private node<E> tail = null;
private int size = 0;
public CircularlyDoublyLinkedList(){ }
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public E first(){
if (isEmpty()) return null;
return tail.getNext().getElement();
}
public E last(){
if(isEmpty()) return null;
return tail.getElement();
}
public void rotate(){
if(tail != NULL){
tail = tail.getNext();
}
}
public void addFirst(E e){
if(size==0){
tail = new Node<> (e, null);
tail.setNext(tail);
tail.setPrevious(tail);
} else {
Node<E> newest = new Node<> (e, tail.getNext(), tail);
tail.setNext(newest);
newest.getNext().setPrevious(newest);
}
size++;
}
public void addLast(E e){
addFirst(e);
tail = tail.getNext();
}
public E removeFirst(){
if (isEmpty()) return null;
Node<E> head = tail.getNext();
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
if(head == tail){
tail = null;
} else {
tail.setNext(head.getNext());
head.getNext().setPrevious(tail);
}
size--;
return head.getElement();
}
}
chevron_up_icon
1 out of 2
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]