Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1 (5 marks) Consider the following two classes Node and Circularlylinke

ID: 3733638 • Letter: Q

Question

Question 1 (5 marks) Consider the following two classes Node and CircularlylinkedList private class Node private int element; private Node next; public Node (int e, Node n) element = e; next = n; public int getElement () public Node getNext ) public void setNext (Node n) next n return element; f return next;) public class CircularlyLinkedList private Node tail-null; private int size0 Add the method countInt() that returns the number of times a specific integer value i stored in a List of the circularlyLinkedList class.

Explanation / Answer

Below is the method. I have added comments to help you understand it.

//method to count the number of times element appears in list

public int countInt(int val) {

//checking if no element is present

if(getSize() == 0) {

return 0;

} else {

//creating a temporary variable and initialize to head

Node temp = head;

//setting count to 0

int count = 0;

//loop till we traverse all elements

do {

//If element is present than increment the count

if(temp.getElement() == val) {

count++;

}

temp = temp.getNext();

}while(temp!=head);

//return the count

return count;

}

}

I have created a full program to demonstrate this via main function.

//Class definition of Node class

class Node {

private int element;

private Node next;

public Node(int e, Node n) {

element = e;

next = n;

}

// method to get the element/ data from Node

public int getElement() {

return element;

}

//method to get the Next node

public Node getNext() {

return next;

}

//method to set the next node

public void setNext(Node n) {

next = n;

}

}

public class CircularlyLinkedList {

public int size = 0;

public Node head = null;

public Node tail = null;

//add a new node at the start of the linked list

public void addNodeAtStart(int data){

System.out.println("Adding node " + data + " at start");

  

if(size==0){

Node n = new Node(data,null);

head = n;

tail = n;

n.setNext(head);

}else{

Node temp = head;

Node n = new Node(data,temp);

  

head = n;

tail.setNext(head);

}

size++;

}

public void addNodeAtEnd(int data){

if(size==0){

addNodeAtStart(data);

}else{

Node n = new Node(data,null);

tail.setNext(n);

tail=n;

tail.setNext(head);

size++;

}

System.out.println(" Node " + data + " is added at the end of the list");

}

public void deleteNodeFromStart(){

if(size==0){

System.out.println(" List is Empty");

}else{

System.out.println(" deleting node " + head.getElement() + " from start");

head = head.getNext();

tail.setNext(head);

size--;

}

}

public int elementAt(int index){

if(index>size){

return -1;

}

Node n = head;

while(index-1!=0){

n=n.getNext();

index--;

}

return n.getElement();

}

//print the linked list

public void print(){

System.out.print("Circular Linked List:");

Node temp = head;

if(size<=0){

System.out.print("List is empty");

}else{

do {

System.out.print(" " + temp.getElement());

temp = temp.getNext();

}

while(temp!=head);

}

System.out.println();

}

//get Size

public int getSize(){

return size;

}

  

//method to count the number of times element appears in list

public int countInt(int val) {

//checking if no element is present

if(getSize() == 0) {

return 0;

} else {

//creating a temporary variable and initialize to head

Node temp = head;

//setting count to 0

int count = 0;

//loop till we traverse all elements

do {

//If element is present than increment the count

if(temp.getElement() == val) {

count++;

}

temp = temp.getNext();

}while(temp!=head);

//return the count

return count;

}

}

  

public static void main(String[] args) {

CircularlyLinkedList c = new CircularlyLinkedList();

c.addNodeAtStart(2);

c.addNodeAtStart(3);

c.addNodeAtStart(2);

c.addNodeAtStart(2);

c.addNodeAtStart(1);

c.addNodeAtStart(2);

c.print();

System.out.println("Get number of times element 2 is in list: "+c.countInt(2));

c.deleteNodeFromStart();

c.print();

c.addNodeAtEnd(4);

c.print();

System.out.println("Size of linked list: " + c.getSize());

System.out.println("Element at 2nd position: " + c.elementAt(2));

}

}

Output

Adding node 2 at start
Adding node 3 at start
Adding node 2 at start
Adding node 2 at start
Adding node 1 at start
Adding node 2 at start
Circular Linked List: 2 1 2 2 3 2
Get number of times element 2 is in list: 4

deleting node 2 from start
Circular Linked List: 1 2 2 3 2

Node 4 is added at the end of the list
Circular Linked List: 1 2 2 3 2 4
Size of linked list: 6
Element at 2nd position: 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote