Question

In: Computer Science

(Implement MyLinkedList) The implementations of the methods contains(E e), get(int index), indexOf(E e), lastIndexOf(E e), and...

(Implement MyLinkedList) The implementations of the methods contains(E
e), get(int index), indexOf(E e), lastIndexOf(E e), and set(int
index, E e) are omitted in the MyLinkedList class. Implement these methods.

Solutions

Expert Solution

//java program implementing the required methods

// java class representing the node of a generic linked list

public class GenericNode<T>

{

private T date;

private GenericNode<T>next;

public GenericNode(T data,GenericNode<T>next)

{

this.data=data;

this.next=next;

}

public void setData(T data)

{

this.data=data;

}

public void setNext(GenericNode<T>next)

{

this.next=next;

}

public T getData()

{

return data;

}

public GenericNode<T>getNext()

{

return next;

}

}

/end of generic node

//java program to implement methods contains ,get,indexOf and set method of MyLinkedList generic class

public class MyLinkedList<T>

{

private int size;//size of the list

privateGenericNode<T>head;//point to head of the list

//constructor to initialize the list

public MyLinkedList()

{

head=null;

size=0;

}

//return size of list

public int getSize()

{

return size;

}

//insert element at the front

public void insert(T data)

{

GenericNode<T>node=new GenericNode<T>(data,null);

if(head==null)head=node;

else

{

node.setNext(head);

head=node;

}

size++;

}

//check if element is present in list

public boolean contains(T e)

{

if(head==null)

return false;

else{

GenericNode<T>temp=head;

while(temp!=null)

{

if(temp.getData()==e)

return true;

temp=temp.getNext();

}

}

return false;

// returns the element at index or raise exception if index >=size

public T get(int index)

{

if(index>=size)

{

throw new IndexOutOfBoundsException();

}

else{

int i=0;

GenericNode<T>temp=head;

while(temp!=null)

{

if(i==index)

return

temp.getData();

temp=temp.getNext();

i++;

}

}

return(T)"-1";

}

//returns indexOf e in the list or -1 if not present

public int indexOf(T e)

{

int i=0;(temp!=null)

{

if(temp.getData()==e)

return i;

i++;temp=temp.getNext();

}

return-1;

}

//returns last index of e in list

public int lastIndexOf(T e)

{

int i=0;

int max=-1;

GenericNode<T<temp=head;

while(temp!=null)

{

if(temp.getData()==e)

max=i;

i++;temp=emp.getNext();

}

return max;

}

//set the elements in index to e or raise exception if index>=size

public void set(int index,T e)

{

if(index>=size)

throw newIndexOutOfBoundsException();

else

{

int i=0;

GenericNode<T>temp=head;

GenericNode<T>node=new GenericNode<T>(e,null);

while(temp.getNext()!=null)

{
if(i==index-1)

{

node.setNext(temp.getNext());

temp.setNext(node);

size++;

return;

}

i++;

temp=temp.getNext();

///temp.setNext(node);

}

}

public static void main(String[] args){

MyLinkedList<Integer>list=newMyLinkedList<integer>();

list.insert(1);//1

list.integer(5);//51

list.integer(3);//3 5 1

list.integer(5);// 5 3 5 1

System.out.println("Size:"+list.getSize());

System.out println("Does list contains 2:"+list.contains(2));

System.out println("Does list contains 5:"+list.contains(5));

System.out println("Get element at 2:"+list.get(2));

System.out println("Get element at 3:"+list.get(3));

System.out.println("index of 3:"+list.indexOf(3));

System.out.println("Last Index of 5:"+list.lastindexOf(5));

list.se(3,6);//5 3 5 6 1

System.out.println("Size:"+list.getSize());

System.out.println("Last index of 6 :"+list.lastIndexOf(6));

System.out.println("index of 1:"+list.lastIndexOf(1));

System.out.println("Size:+list.getSize());

}

}


Related Solutions

class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index - 1; // Decrementing the index System.out.printf("%d%n", a[index]); reverse(a, index); // Recursive call } return; } public static void main (String args[]) { int [] array = { 1, 2, 3, 4, 5 }; int n=array.length; reverse(array,n); // function call } } Write a generic version of the corrected recursive reverse method that could be used to print any of the following arrays (or...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that stores the int value represented by this object. • A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice? • A constructor that creates a MyInteger object for a specified int value. • A getter method, valueOf(), that returns value. • A setter method,...
(Implement a doubly linked list) The MyLinkedList class used in Listing 24.5 is a one-way directional...
(Implement a doubly linked list) The MyLinkedList class used in Listing 24.5 is a one-way directional linked list that enables one-way traversal of the list. Modify the Node class to add the new data field name previous to refer to the previous node in the list, as follows : public class Node { E element; Node next; Node previous; public Node(E e) { element = e; } } Implement a new class named TwoWayLinkedList that uses a doubly linked list...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int containsWithin(const int *arr,int size , int k,int i , int j); int *paddedCopy(const int *arr,int oleSize , int newSize); void reverse(int *arr,int size); int *reverseCopy(const int *arr,int size); //array_utils.c #include"array_utils.h" #include<stdlib.h> int contains(const int *arr,int size , int k){    int i=0;    for(i=0;i<size;i++){        if(arr[i] == k)return 1;    }    return 0; } int containsWithin(const int *arr,int size , int k,int...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4]...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4] = { begin[0], begin[1], begin[2], begin[3] }; add: goto print; carry: if ( current[index] < end[index] - 1 ) { current[index]++; goto add; } else if ( index > 0 ) { current[index] = begin[index]; index--; goto carry; } else return; print: cout << current[0] << current[1] << current[2] << current[3] << endl; index = 3; goto carry; } int main( ) { int...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your code in main
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
You have been provided with starter code that contains the place holders and some implementations for...
You have been provided with starter code that contains the place holders and some implementations for Bisection and Secant methods. Complete the code where it says #provide this line so that it could be run for the following case: 1) ?(?) = ? 3 + 4, ?? = −3, ?? = 0, ??????? = 100, ?? = 0.2 Starter Code: def eq(x):           return x**3+4 def err(xr,xold):           return abs((xr-xold)/xr)*100 def bisection(xl,xu,maxIter,sc):     # xl is the lower bound of the initial bracket...
Design a class named ArraySort and it contains the following method: Public int search(int target): if...
Design a class named ArraySort and it contains the following method: Public int search(int target): if the target is found in the array, return the number of its showing up. If the target is not found in the array, return -1. If the array is empty, return -1; Public int maximum():Return the maximum number in the array if the array is nonempty, otherwise return -1; Public int minimum(): Return the minimum number in the array if the array is nonempty,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT