Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public void insertInFront(Object element1, Object element2) The insertInFront method insert the second parameter object, i.e. element2 immediately in front of the first occurence of first

public void insertInFront(Object element1, Object element2) The insertInFront method insert the second parameter object, i.e. element2 immediately in front of the first occurence of first parameter object, i.e. element1. If element1 is not found inside the linked list, then insert element2 at the front/head of the linked list.

please help right linkedList method

existing code:

import java.util.NoSuchElementException; public class LinkedList { //The only instance variable that points to the first node private Node first; //nested class to represent a node private class Node { public Object data; public Node next; } // Constructs an empty linked list. public LinkedList() { first = null; } //Returns the first element in the linked list. public Object getFirst() { if (first == null) throw new NoSuchElementException(); return first.data; } //Removes the first element in the linked list. public Object removeFirst() { if (first == null) throw new NoSuchElementException(); Object element = first.data; first = first.next; return element; } //Adds an element to the front of the linked list. public void addFirst(Object element) { Node newNode = new Node(); newNode.data = element; newNode.next = first; first = newNode; } //toString() method prints the elements out from front to tail public String toString() { ListIterator iterator = listIterator(); String result = "{ "; while (iterator.hasNext()) { result += iterator.next() + " "; } result += "} "; return result; } //***************************************************************** //size() method returns the number of nodes inside this LinkedList //***************************************************************** public int size() { ListIterator iterator = listIterator(); int count = 0; while (iterator.hasNext()) { count++; iterator.next(); } return count; } //*************** Below is where you should add your own codes *********** //***************************************************************** //searchElement() method returns the index of the first occurrence of the parameter object //in the linked list if it exists. It returns -1 if it does not exit. //***************************************************************** public int searchElement(Object element) { //Add your own code //---- } //***************************************************************** // getElement() method returns the element at the parameter index // If the index is out of bounds, throw an IndexOutOfBoundsException. //***************************************************************** public Object getElement(int index) { //Add your own code //---- } //***************************************************************** //setElement() method sets the parameter object at the parameter index in the linked list. // If the index is out of bounds, throws an IndexOutOfBoundsException //***************************************************************** public void setElement(int index, Object element) { //Add your own code //---- } //***************************************************************** //insertElement() method inserts the parameter object at the parameter index. //If the index is out of bounds, throws an IndexOutOfBoundsException //Note: the element can be inserted at the end, i.e. inserted at size() index/position //***************************************************************** public void insertElement(int index, Object element) { //Add your own code //---- } //***************************************************************** //removeElement()method removes and returns element at parameter index and throw //an IndexOutOfBoundsException if the index is out of bounds //***************************************************************** public Object removeElement(int index) { //Add your own code //---- } //***************************************************************** //countHowMany(Object) method returns the number of occurences of the parameter object in the LinkedList //***************************************************************** public int countHowMany(Object searchedObject) { //Add your own code //---- } //***************************************************************** //removeDuplicate() method removes all occurences of the parameter objects from the LinkedList //***************************************************************** public void removeDuplicate(Object removedObject) { //Add your own code //---- } //***************************************************************** //insertAtFront(Object, int) method inserts the parameter object number of times at the front of the linked list //for example, a call of insertAtFront("A", 3) will insert string "A" three times in front of the linked list. //***************************************************************** public void insertAtFront(Object element, int howManyTimes) { //Add your own code //---- } //***************************************************************** //appendAtEnd(Object, int) method appends the parameter object number of times at the end of the linked list //for example, a call of appendAtEnd("B", 3) will append string "B" three times at the end of the linked list. //***************************************************************** public void appendAtEnd(Object element, int howManyTimes) { //Add your own code //---- } //***************************************************************** //insertInFront(Object element1, Object element2) method insert the //second parameter object, i.e. element2 immediately in front of the //first occurence of first parameter object - element1. //if element1 is not found inside the linked list, then insert element2 //at the front/head of the linked list. //***************************************************************** public void insertInFront(Object element1, Object element2) { //Add your own code //---- } //***************************************************************** //appendAfter(Object element1, Object element2) method appends the second parameter object, i.e. element2 right after //the first occurence of first parameter object, //if element1 is not inside the linked list, then append element2 at the front/head of the linked list. //***************************************************************** public void appendAfter(Object element1, Object element2) { //Add your own code //---- } //***************************************************************** //reverse(int howMany) reverse the first parameter number of elements inside the linked list. //for example, if the original linked list is { A B C D E F G }, a call of reverse(3) will //change the linked list to { C B A D E F G }. Note #1: you need to consider the boundary value, i.e. //cases where howMany <= 0 or howMany > size() //Note #2: reverse(size()) should reverse the whole linked list //***************************************************************** public void reverse(int howMany) { //Add your own code //---- } //***********Don't change the following LinkedListIterator class****************** //*************************************************************** //Method creates an iterator on the current LinkedList //*************************************************************** public ListIterator listIterator() { return new LinkedListIterator(); } //*************************************************************** //nested class to define its iterator //*************************************************************** private class LinkedListIterator implements ListIterator { private Node position; private Node previous; // Constructs an iterator that points to the front // of the linked list. of the linked list. public LinkedListIterator() { position = null; previous = null; } // Moves the iterator past the next element, and returns // the traversed element's data. public Object next() { if (!hasNext()) throw new NoSuchElementException(); previous = position; // Remember for remove if (position == null) position = first; else position = position.next; return position.data; } // Tests if there is an element after the iterator position position public boolean hasNext() { if (position == null) return first != null; else return position.next != null; } // Adds an element after the iterator position // and moves the iterator to the inserted element. public void add(Object element) { if (position == null) { addFirst(element); position = first; } else { Node newNode = new Node(); newNode.data = element; newNode.next = position.next; position.next = newNode; position = newNode; } previous = position; } // Removes the last traversed element. This method may // only be called after a call to the next() method. public void remove() { if (previous == position) throw new IllegalStateException(); if (position == first) { removeFirst(); } else { previous.next = position.next; } position = previous; } // Sets the last traversed element to a different value public void set(Object element) { if (position == null) throw new NoSuchElementException(); position.data = element; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2017 Skopje Macedonia September 18 22 2017 Proceedings Part 3 Lnai 10536

Authors: Yasemin Altun ,Kamalika Das ,Taneli Mielikainen ,Donato Malerba ,Jerzy Stefanowski ,Jesse Read ,Marinka Zitnik ,Michelangelo Ceci ,Saso Dzeroski

1st Edition

ISBN: 3319712721, 978-3319712727

More Books

Students also viewed these Databases questions