Question

In: Computer Science

JAVA /**    * dataAtPosition returns the int data at the described position.    * if...

JAVA

/**

   * dataAtPosition returns the int data at the described position.

   * if the position is an invalid position it throws an Exception.

   *

   * Examples:

   * pos : 0 and LinkedList : 2 --> 3 --> null ==> return 2

   * pos : 1 and LinkedList : 1 --> -3 --> null ==> return -3

   * pos : 2 and LinkedList : -2 --> 3 --> -2 --> null ==> return -2

   */

Code I have is:

   public int dataAtPosition(int pos) {

       return 0;

   }

Solutions

Expert Solution

package snippet;


public class LinkedList<T> {

private Node<T> head;
private Node<T> tail;
//function to add data
public void add(T element){

Node<T> nd = new Node<T>();//create new node
nd.setValue(element);//set value to node

//if list is empty,create head node
if(head == null){
  
head = nd;
tail = nd;
} else {
  
tail.setNext(nd);//insert node to end
  
tail = nd;
}
System.out.println("Added: "+element);
}
//function to display linked list
public void Display(){

Node<T> tmp = head;//start from head
while(true){
if(tmp == null){
break;
}
System.out.println(tmp.getValue());//print node data
tmp = tmp.getNext();//go to next node
}
}


public int dataAtPosition(int pos) {
   Node<T> tmp = head;
     
while(true)
{
if(tmp == null)//break if list is empty
{
   break;
  
}
if(pos==0)
   break;
  
tmp = tmp.getNext();//go to next node
pos--;
}
return (int) tmp.getValue();

}

public boolean contains(int elem) {
   Node<T> tmp = head;
while(true)
{
if(tmp == null)//break if list is empty
{
   break;
  
}
int data=(int) tmp.getValue();//get value of node
if(data==elem)//compare value of current node with elem
   return true;
else
tmp = tmp.getNext();//go to next node
}
return false;//if nothing matches return false
}
public static void main(String a[]){
LinkedList<Integer> ll = new LinkedList<Integer>();
ll.add(2);
ll.add(3);
System.out.println("Linked List is ");
ll.Display();
  
System.out.println("Data st position 0 "+ll.dataAtPosition(0));
  



  

}
}

class Node<T> implements Comparable<T> {

private T value;
private Node<T> nextRef;

public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public Node<T> getNext() {
return nextRef;
}
public void setNext(Node<T> ref) {
this.nextRef = ref;
}
@Override
public int compareTo(T arg) {
if(arg == this.value){
return 0;
} else {
return 1;
}
}
}

================================

Output:

Added: 2
Added: 3
Linked List is
2
3
Data st position 0 2


Related Solutions

Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that...
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that is * less than or equal to the limit parameter * if all values are greater than theVal, return -1; * * Precondition: the array is nonempty and all elements are unique. * Your solution must go through the array exactly once. * * <pre> * 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7 is in pos 0 * 5 == posOfLargestElementLtOeT(3,...
2. Create ACCESSOR FUNCTIONS in JAVA a) String moneyToString(int[] money); // Returns a nice looking string....
2. Create ACCESSOR FUNCTIONS in JAVA a) String moneyToString(int[] money); // Returns a nice looking string. Ex, "$6.25", "$0.21", "$4.01", "$2.00". MAKE SURE TO CONSIDER ALL EXAMPLES! b) *String moneyToText(int[] money); // Returns the Money as words. Ex,{123,51} => "one hundred and twenty three dollars and fifty one cents." YOU MAY ASSUME money <$1000. context: this is what I have so far package com.company; import java.util.*; public class Main { public static void main(String[]args) { int money[] = createMoney(12, 34);...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method receives the following arrangement: [1,2,3,4,5] The invert method returns the array [5,4,3,2,1] Example 2: the print method receives the following array: [5,4,3,2,1] The print method prints: 5,4,3,2,1 (data separated by commas). TIP: for the print method use System.out.print () without the "ln".
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1,...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1, 2, 3, 4}; get_value(data, 2) = 100; } Write ????? that returns the value in the array at index: Question Blank type your answer...
Coding in Java private int getLastDayOfMonth(int m, int y)– return the last day of the month...
Coding in Java private int getLastDayOfMonth(int m, int y)– return the last day of the month and year entered as input parameter. Remember to invoke the isLeapYear method when appropriate. If the month entered as input parameter is 2 (February) and the year passed as input parameter is a leap year this method should return 29. private boolean isDateValid (int m, int d, int y) – return true if day, month, and year entered as input parameters form a valid...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
Write the BNF for mini Java language based of the following information: Data Types Integer Int...
Write the BNF for mini Java language based of the following information: Data Types Integer Int Long Double Boolean Char References Complex Data Structures Arrays int v[30]; Classes member variables class Name { int a; char b; char name[25]; } Methods Return data type Primitive data type Void Method Name Parameter list Could be empty Statement Block { Variable declarations Executable Statements } Program Variable Declarations Class Definitions Methods Only one method named "main" but must have one method named...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT