Someone send me below answer Is this correct or not , if not please Send me the
ID: 3586174 • Letter: S
Question
Someone send me below answer
Is this correct or not , if not please Send me the whole answer please I will promise to give you thumbs up. 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;
Node(int data, Node next) {
this.data = data;
this.next = next;
}
}
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;
}
public boolean isEmpty() {
return this.size() == 0;
}
public void addFirst(int item) {
Node node = new Node(item, this.head);
this.head = node;
if (this.isEmpty()) {
this.tail = node;
}
this.size++;
}
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++;
}
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;
}
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++;
}
}
public int getFirst() throws Exception {
if (this.isEmpty()) {
throw new Exception("List is Empty");
}
return this.head.data;
}
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;
}
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;
}
}
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("*************************************");
}
public void reverseDI() throws Exception {
int si = 0, li = this.size - 1;
while (si <= li) {
Node left = this.getNode(si);
Node right = this.getNode(li);
int temp = left.data;
left.data = right.data;
right.data = temp;
si++;
li--;
}
}
public void reversePI() {
Node prev = this.head;
Node curr = prev.next;
while (curr != null) {
Node tempPrev = prev;
Node tempCurr = curr;
prev = curr;
curr = curr.next;
tempCurr.next = tempPrev;
}
Node temp = this.head;
this.head = this.tail;
this.tail = temp;
this.tail.next = null;
}
public void reversePR() {
reverse(this.head);
Node temp = this.head;
this.head = this.tail;
this.tail = temp;
this.tail.next = null;
}
private void reverse(Node node) {
if (node == this.tail) {
return;
}
reverse(node.next);
node.next.next = node;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.