Question

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: " + number);
}
   public int getNumber()
{
return number;
}

public void setNumber(int y)
{
   number =y;
}
   public Link getNext()
{
return next;
}
   public void setNext(Link l)
{
next=l;
}
}

public class LinkedList {

private static int size=0;
     private Link first;
   public LinkedList()
{
first = null;
}
   public boolean isEmpty()
{
return (first==null);
}
   public void insertFirst(int x)//insert at first
{
Link newLink = new Link(x);
newLink.setNext(first);
first = newLink;
size++;
}
   public void deleteFirst()
{
first = first.getNext();
size--;
}
   public void displayList()
{
System.out.println("List (first-->last): ");
Link current = first;
while(current != null)
{
current.displayLink();
current = current.getNext();
}
}
  public Link find(int key)
{
Link current = first;
while(current.getNumber() != key)
{
if(current.getNext() == null)
{
return null;
}
else
{
current = current.getNext();
}
}
return current;
}


public int size()
{
   return size;
}
}

public class TestLinkedList {
public static void main(String[] args) {
    LinkedList list = new LinkedList();
list.insertFirst(2);
list.insertFirst(4);
list.insertFirst(6);
list.insertFirst(8);
list.insertFirst(10);
    list.displayList();
  }
}

The output must look like the following:
List (first-->last):
The number is: 10
The number is: 8
The number is: 6
The number is: 4
The number is: 2
List (first-->last):
The number is: 20
The number is: 16
The number is: 12
The number is: 8
The number is: 4

Homework Answers

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
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:...
Using the given SList as a template to implement a doubly linked list with the following...
Using the given SList as a template to implement a doubly linked list with the following functionalities Add first: add an element to the head of the list Add last: add an element to the end of the list Print: print the content of the list starting from the head Find: a private method that will return a reference to a node if it contains the same value of a given argument Insert after: a method that takes a key...
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...
Implement Polynomial Linked List multiply method which multiply two polynomial and returns the product as a...
Implement Polynomial Linked List multiply method which multiply two polynomial and returns the product as a new polynomial. - It’s an instance method which called by a polynomial and takes second polynomial as the parameter pl. It should correctly multiply two polynomials together and returns this product polynomial. //please see the given code below public class PolynomialLinkedlist{ private static class PNode{ private int coe; private int exp; private PNode next; public PNode(int c, int e){ this(c, e, null); } public...
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;...
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;...
Implement Doubly Linked List get method which behaves like the operator[] of array. - It takes...
Implement Doubly Linked List get method which behaves like the operator[] of array. - It takes an integer parameter i as the index, it throw an Exception if the index i is illegal, returns the element at given index i, it traverse from the header of the list if index i is in the lower end of the list(less than half of the size), and traverse from the trailer of the list if index i is in the higher end...
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;...
To the TwoDArray, add a method called transpose() that generates the transpose of a 2D array...
To the TwoDArray, add a method called transpose() that generates the transpose of a 2D array with the same number of rows and columns (i.e., a square matrix). Add appropriate code in main() of the TwoDArrayApp class to execute the transpose() method and use the display() method to print the transposed matrix. /** * TwoDArray.java */ public class TwoDArray { private int a[][]; private int nRows; public TwoDArray(int maxRows, int maxCols) //constructor { a = new int[maxRows][maxCols]; nRows = 0;...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT