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

The language is java package hw; public class MyLinkedList<E> { SLLNode<E> head = null; public MyLinkedList()...
The language is java package hw; public class MyLinkedList<E> { SLLNode<E> head = null; public MyLinkedList() {} // O(1) public MyLinkedList(E[] elements) { // O(elements.length) for(int i=elements.length-1;i>=0;i--) add(elements[i]); } public void printLinkedList() { // T(N) = O(N) System.out.print("printLinkedList(): "); SLLNode<E> node = head; while(node != null) { System.out.print(node.info + " "); node = node.next; // move to the next node } System.out.println(); } public void add(E e) { // T(N) = O(1) SLLNode<E> newNode = new SLLNode<E>(e); newNode.next = head;...
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...
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...
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...
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...
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)
Answer the following What is the difference between these two methods? Void display(int x) Int display(int...
Answer the following What is the difference between these two methods? Void display(int x) Int display(int s) Write a one line of code to generate random numbers from 35 to 55.( not a full program) Draw the UML diagram for a switch statement with 4 cases . Write a while loop to output the value of the variable x starting from 5, decreasing it by 0.5 each time, as long as x remains positive. Write a for loop to display...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT