Someone answer me like this , but I am not sure. Please send me a correct answer
ID: 3586168 • Letter: S
Question
Someone answer me like this , but I am not sure.
Please send me a correct answer with details pls. Thanks. Page 1 of 6 Circular Linked List Assignment Overview A circular linked list is essentially a singly linked list in which the next pointer of the tail node is set to point to the head node of the linked list rather than set to nul. The first figure below shows a linked list as a singly linked list. The second figure below shows the singly linked list from the first figure as a circular linked list. In this assignment, you will write code for an implementation of a circular linked list 12 Head Tail Singly Linked List 12 Tail Circular Linked List
Explanation / Answer
public class LinkedList {
private class Node {
int data;
Node next;
//next will point to next node
//node consist of data and the value of next node
Node(int data, Node next) {
this.data = data;
this.next = next;
}
}
//node consist of two parts that is head and tail
//head part is the starting of the node
//tail is the end part of the linked list
private int size;
private Node head;
private Node tail;
public LinkedList() {
// TODO Auto-generated constructor stub
this.size = 0;
this.head = null;
this.tail = null;
}
public int size() {
return this.size;
}
//if list is empty then head and tail part will be the same
public boolean isEmpty() {
return this.size() == 0;
}
//adding in the head part of the list and then make the previous head to next
public void addFirst(int item) {
Node node = new Node(item, this.head);
this.head = node;
if (this.isEmpty()) {
this.tail = node;
}
this.size++;
}
//adding in the last part of the list and then take the previous tail node and point it to the new tail part
public void addLast(int item) {
Node node = new Node(item, null);
if (this.isEmpty()) {
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
this.tail = node;
}
this.size++;
}
//this will get the node
private Node getNode(int index) throws Exception {
if (index < 0 || index >= this.size()) {
throw new Exception("Index out of Range");
}
Node temp = this.head;
int counter = 0;
while (counter < index) {
temp = temp.next;
counter++;
}
return temp;
}
//calling the function get node where user want to add the node and then after calling that node add the node
public void addAt(int index, int item) throws Exception {
if (index < 0 || index > this.size()) {
throw new Exception("Index out of Range");
}
if (index == 0) {
this.addFirst(item);
} else if (index == this.size()) {
this.addLast(item);
} else {
Node temp = getNode(index - 1);
Node node = new Node(item, temp.next);
temp.next = node;
this.size++;
}
}
//getting the first node of the list
public int getFirst() throws Exception {
if (this.isEmpty()) {
throw new Exception("List is Empty");
}
return this.head.data;
}
//getting the last node of the list
public int getLast() throws Exception {
if (this.isEmpty()) {
throw new Exception("List is Empty");
}
return this.tail.data;
}
public int getAt(int index) throws Exception {
if (this.isEmpty()) {
throw new Exception("List is Empty");
}
if (index < 0 || index >= this.size()) {
throw new Exception("Index out of Range");
}
Node temp = this.getNode(index);
return temp.data;
}
public int removeFirst() throws Exception {
if (this.isEmpty()) {
throw new Exception("List is empty");
}
Node rv = this.head;
if (this.size() == 1) {
this.tail = null;
this.head = null;
} else {
this.head = this.head.next;
}
this.size--;
return rv.data;
}
public int removeLast() throws Exception {
if (this.isEmpty()) {
throw new Exception("List is empty");
}
Node rv = this.tail;
if (this.size() == 1) {
this.tail = null;
this.head = null;
} else {
Node temp = this.getNode(this.size() - 2);
temp.next = null;
this.tail = temp;
}
this.size--;
return rv.data;
}
//removing the element from where the user wants to remove
public int removeAt(int index) throws Exception {
if (this.isEmpty()) {
throw new Exception("List is empty");
}
if (index < 0 || index >= this.size()) {
throw new Exception("Index out of Range");
}
if (index == 0) {
return this.removeFirst();
} else if (index == this.size() - 1) {
return this.removeLast();
} else {
Node rv = this.getNode(index);
Node temp = this.getNode(index - 1);
temp.next = temp.next.next;
this.size--;
return rv.data;
}
}
//triverse the list from first node to last and display the data and stops when list becomes empty
public void display() throws Exception {
if (this.isEmpty()) {
throw new Exception("List is empty");
}
Node temp = this.head;
while (temp != null) {
System.out.print(temp.data + "=>");
temp = temp.next;
}
System.out.println("END");
System.out.println("*************************************");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.