In: Computer Science
Java program to implement circular linked list. NO COPY PASTE ANSWERS plz follow the given template...
public class CircularLinkedList {
private Node tail;
private int size;
public CircularLinkedList() {
tail= null;
size = 0;
}
public int size(){
return size;
}
public boolean isEmpty()
{
return size==0;
}
//if list is not empty return the first element
public E first() {
if (isEmpty()) return null;
//code here
return 0;
}
//if list not empty return last element
public E last() {
if (isEmpty()) return null;
return tail.getElement();
}
/*
move tail to the next node
*/
public void rotate()
{
}
/*
add element to the first of the linked list
increase the size
*/
public void addFirst(E e)
{
}
/*
add element to the end of the linked list
increase size
*/
public void addLast(E e)
{
}
/*
take out the first element
decrease the size
return first element or null
*/
public E removeFirst()
{
return null;
}
public String toString()
{
String s;
Node<E> n;
if ( tail == null )
return "null";
s = "[";
n = tail.getNext();
if (n==null)
{
return s+ "empty list]";
}
int iter =0;
//change the while loop below for circular list instead of singly linked list
while (n!=null&&iter<2*size)
{
iter++;
s = s+ n.getElement();
if (n.getNext()!=null) s = s + ", ";
n = n.getNext();
}
return s+"]";
------------------------------------------------------------------------------------------------------
implements....
public class Node {
// Instance variables:
//element = data
private E element;
private Node next;
/** Creates a node with null references to its element and next node. */
public Node() {
this(null, null);
}
/** Creates a node with the given element and next node. */
public Node(E e, Node n) {
element = e;
next = n;
}
// Accessor methods:
public E getElement() {
return element;
}
public Node getNext() {
return next;
}
// Modifier methods:
public void setElement(E newElem) {
element = newElem;
}
public void setNext(Node newNext) {
next = newNext;
}
}
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
Node.java
package c15;
public class Node<E> {
// Instance variables:
//element = data
private E element;
private Node next;
/** Creates a node with null references to its element and next
node. */
public Node() {
this(null, null);
}
/** Creates a node with the given element and next node. */
public Node(E e, Node n) {
element = e;
next = n;
}
// Accessor methods:
public E getElement() {
return element;
}
public Node getNext() {
return next;
}
// Modifier methods:
public void setElement(E newElem) {
element = newElem;
}
public void setNext(Node newNext) {
next = newNext;
}
}
CircularLinkedList.java
package c15;
public class CircularLinkedList<E> {
private Node tail;
private Node head;
private int size;
public CircularLinkedList() {
tail= null;
size = 0;
}
public int size(){
return size;
}
public boolean isEmpty()
{
return size==0;
}
//if list is not empty return the first element
public E
first() {
if (isEmpty()) return null;
return (E) head.getElement();
}
//if list not
empty return last element
public E
last() {
if (isEmpty()) return null;
return (E) tail.getElement();
}
/*
move tail to the next node
*/
public void
rotate(){
tail = tail.getNext();
head = head.getNext();
}
/*
add element to the first of the linked list
increase the size
*/
public void
addFirst(E e)
{
Node<E> newNOde =new Node<>();
newNOde.setElement(e);
newNOde.setNext(head);
head = newNOde;
if(isEmpty()) {
tail = head;
}
tail.setNext(head);
size++;
}
/*
add element to the end of the linked list
increase size
*/
public void
addLast(E e){
Node<E> newNOde =new Node<>();
newNOde.setElement(e);
if(isEmpty()) {
head = newNOde;
tail = head;
tail.setNext(head);
}else {
tail.setNext(newNOde);
newNOde.setNext(head);
tail = newNOde;
}
size++;
}
/*
take out the first element
decrease the size
return first element or null
*/
public E
removeFirst()
{
head = head.getNext();
tail.setNext(head);
return null;
}
public String
toString()
{
String s;
Node<E> n;
if ( tail == null )
return "null";
s = "[";
n = tail.getNext();
if (n==null)
{
return s+ "empty
list]";
}
int iter =0;
//change the while loop below for circular list
instead of singly linked list
while (n!=null)
{
iter++;
s = s+ n.getElement();
if (n.getNext()!=null) s = s
+ ", ";
n = n.getNext();
if(n==head) {
break;
}
}
return s+"]";
}
public static
void main(String[] args) {
CircularLinkedList<Integer> list = new
CircularLinkedList<>();
list.addLast(1);
list.addLast(2);
list.addLast(3);
list.addLast(4);
list.addLast(5);
list.addLast(6);
list.addLast(7);
list.addLast(8);
System.out.println(list);
for(int i=0;i<10;i++) {
System.out.println("Rotate
"+(i+1));
list.rotate();
System.out.println(list);
}
}
}
output
