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

Answer #1

If you have any problem with the code feel free to comment.

doubleValue()

    public void doubleValue() {
                //getting the first link address
                Link current = first;
                //looping through the list till there is no next value
                while (current != null) {
                        //getteing the number and setting double its value
                        current.setNumber(current.getNumber()*2);
                        //pointing to next link
                        current = current.getNext();
                }
        }

Test Class

public class Test {
        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();
                list.doubleValue();
                list.displayList();
        }
}

Output

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:...
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...
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;...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const size_t& count) class ListNode { public: typedef int value_type; ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }    //Assessor value_type getDatum () const { return datum; } ListNode const* getNext () const { return next; }    //Mutator void setDatum (const value_type& d) {datum = d; } ListNode* getNext () { return next; } void...
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;...
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....
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...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...