Question

Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...

Java Language

Add a method (convertToRing ()) that converts the list to a circular. This is,
which makes the last node instead of pointing to null point to the first.

Code:

class Node {

int value;

Node nextNode;

Node(int v, Node n)

{

value = v;

nextNode = n;

}

Node (int v)

{

this(v,null);

}

}

class LinkedList

{

Node head; //head = null;

LinkedList()

{

}

int length()

{

Node tempPtr;

int result = 0;

tempPtr = head;

while (tempPtr != null)

{

tempPtr = tempPtr.nextNode;

result = result + 1;

}

return(result);

}

void insertAt(int v, int position)

{

Node newNode = new Node(v,null);

Node tempPtr;

int tempPosition = 0;

if((head == null) || (position ==0))

{

newNode.nextNode = head;

head = newNode;

}

else {

tempPtr = head;

while((tempPtr.nextNode != null)&&(tempPosition < position -1))

{

tempPtr = tempPtr.nextNode;

tempPosition = tempPosition + 1;

}

if (tempPosition == (position - 1))

{

newNode.nextNode = tempPtr.nextNode;

tempPtr.nextNode = newNode;

}

}

}

public String toString()

{

Node tempPtr;

tempPtr = head;

String result = "";

while(tempPtr != null)

{

result = result + "[" + tempPtr.value + "| ]-->";

tempPtr = tempPtr.nextNode;

}

result = result + "null";

return result;

}

}

public class LinkedListDemoInsDel

{

public static void main(String[] args)

{

LinkedList aLinkedList = new LinkedList();

aLinkedList.insertAt(1,0);

aLinkedList.insertAt(9,1);

aLinkedList.insertAt(13,2);

aLinkedList.insertAt(8,1);

aLinkedList.insertAt(3,2);

System.out.println(aLinkedList);

System.out.println("Largo de lista: " + aLinkedList.length());

}

}

Homework Answers

Answer #1
static Node convertToRing(Node head) 
{ 
    // declare a node variable  
    // start and assign head  
    // node into start node. 
    Node start = head; 
  
    // check that while head.next 
    // not equal to null then head  
    // points to next node. 
    while (head.next != null) 
        head = head.next; 
          
    // if head.next points to null  
    // then start assign to the  
    // head.next node. 
    head.next = start; 
    return start; 
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
This is based on LinkedLists. There is a type mismatch in my first method and I...
This is based on LinkedLists. There is a type mismatch in my first method and I dont know how to get around it. I've commented on the line with the type mismatch public class Node {    public T info; public Node link; public Node(T i, Node l) {    info = i; link = l;    } } class LinkedList> {    protected Node head = null; public LinkedList add(T el) { head = new Node(el, head); return this;...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT