Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class CircularList { private Node first; private int size; public CircularList() { first = null; size = 0; } public void add(int value) {

public class CircularList { private Node first; private int size;

public CircularList() { first = null; size = 0; } public void add(int value) { if (first == null) { first = new Node(value, null); first.setNext(first); } else { Node newNode = new Node(value, first.getNext()); first.setNext(newNode); } size++; // System.out.println("added " + value); } public int get() { return first.getData(); } public void rotate() { if (first != null) first = first.getNext(); } public int size() { return size; } public int count(int value) { int count = 0; for (int i = 0; i < size(); i++) { if (get() == value) count++; rotate(); } return count; } }

class Node { private int data; private Node next;

public Node(int data, Node next) { this.data = data; this.next = next; }

public int getData() { return data; }

public Node getNext() { return next; }

public void setNext(Node next) { this.next = next; } }

This file contains no main program, but rather than using the one created in the walkthrough, we will create a new one that adds items to the list and prints its contents using a toString() method. Add a toString() method to the circular linked list class, and test it by calling it from a main program that adds some items to the list.

We can complete our class by making it possible to remove elements. To keep the number of special cases at a minimum, our removal will only apply in one spot of our choosing: it will remove the element that first is pointing to. Write a remove() method (no parameters) that removes a single item from the list, the node pointed to by first.

To delete the first item from the list, you will need a pointer to the last item (the one counter-clockwise from the first). You can get this pointer using a loop that goes clockwise through the list, stopping one position before you reach the first node again. A diagram may help!

As with the get and rotate operation, the behaviour of removing an item from an empty list can be debated. Since you can't remove something that isn't there, an exception is not a bad thing in other words, no need for an if to check for an empty list.

Hint: There is still one special case for removal, when there is only a single item in the list.

Create a list and add items to it in your main program. Test the new methods by using a loop to repeatedly remove items until the list is empty, printing the entire list after each item is removed.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Database Systems Design Implementation And Project Management

Authors: Albert K.W. Yeung, G. Brent Hall

1st Edition

1402053932, 978-1402053931

More Books

Students also viewed these Databases questions

Question

=+ Why is it important for IHRM to be strategic?

Answered: 1 week ago

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago