Please use Java programming:
Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(),
This method will reverse the lists.
When testing the method: print out the original list, call the new method, then print out the list again
-------------------------------------------------------------------------
//ARRAY LIST class:
public class ArrayList<E> implements List<E> {
/** Array of elements in this List. */
private E[] data;
/** Number of elements currently in this List. */
private int size;
/** The List is initially empty. */
public ArrayList() {
data = (E[])(new Object[1]); // This causes a
compiler warning
size = 0;
}
public void add(E target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
public boolean contains(E target) {
for (int i = 0; i < size; i++) {
if (data[i].equals(target)) {
return true;
}
}
return false;
}
public boolean isEmpty() {
return size == 0;
}
/** Return true if data is full. */
protected boolean isFull() {
return size == data.length;
}
public java.util.Iterator<E> iterator() {
return new ArrayIterator<E>(this);
}
public E get(int index) {
return data[index];
}
public E remove(int index) {
E result = data[index];
for (int i = index + 1; i < size; i++)
{
data[i - 1] = data[i];
}
size--;
return result;
}
public boolean remove(E target) {
for (int i = 0; i < size; i++) {
if (data[i].equals(target)) {
for (int j = i; j <
size - 1; j++) {
data[j] =
data[j + 1];
}
size--;
return true;
}
}
return false;
}
public void set(int index, E target) {
data[index] = target;
}
public int size() {
return size;
}
/** Double the length of data. */
protected void stretch() {
E[] newData = (E[])(new Object[data.length *
2]); // Warning
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
data = newData;
}
public String toString() {
String result = "[ ";
for (int i = 0; i < size; i++) {
result += data[i] + " ";
}
return result + "]";
}
}
----------------------------------------------------------------
//LINKED LIST class
import java.util.*;
/** A linked list. */
public class LinkedList<Integer> implements
List<Integer>, Predecessor<Integer> {
/** The first node in the List. */
private ListNode<Integer> front;
/** A LinkedList is initially empty. */
public LinkedList() {
front = null;
}
public void add(Integer target) {
Predecessor<Integer> last = this;
while (last.getNext() != null) {
last = last.getNext();
}
last.setNext(new
ListNode<Integer>(target));
}
public boolean contains(Integer target) {
for (ListNode<Integer> node = front;
node !=
null;
node =
node.getNext()) {
if (node.getItem().equals(target))
{
return true;
}
}
return false;
}
public Integer get(int index) {
ListNode<Integer> node = front;
for (int i = 0; i < index; i++) {
node = node.getNext();
}
return node.getItem();
}
public ListNode<Integer> getNext() {
return front;
}
public boolean isEmpty() {
return front == null;
}
public java.util.Iterator<Integer> iterator() {
return new
ListIterator<Integer>(this);
}
public Integer remove(int index) {
Predecessor<Integer> prev = this;
ListNode<Integer> node = front;
for (int i = 0; i < index; i++) {
prev = node;
node = node.getNext();
}
prev.setNext(node.getNext());
return node.getItem();
}
public boolean remove(Integer target) {
Predecessor<Integer> prev = this;
ListNode<Integer> node = front;
while (node != null) {
if (node.getItem().equals(target))
{
prev.setNext(node.getNext());
return true;
}
prev = node;
node = node.getNext();
}
return false;
}
public void set(int index, Integer target) {
ListNode<Integer> node = front;
for (int i = 0; i < index; i++) {
node = node.getNext();
}
node.setItem(target);
}
public void setNext(ListNode<Integer> next) {
front = next;
}
public int size() {
int tally = 0;
for (ListNode<Integer> node = front;
node !=
null;
node =
node.getNext()) {
tally++;
}
return tally;
}
public String toString() {
String result = "( ";
for (ListNode<Integer> node = front;
node !=
null;
node =
node.getNext()) {
result += node.getItem() + "
";
}
return result + ")";
}
public static void main(String[] args)
{
LinkedList list = new
LinkedList();
/*add(String Element) is
used for adding
* the elements to the
linked list*/
list.add("1");
list.add("10");
list.add("20");
list.add("30");
list.add("40");
list.add("50");
list.add("6000");
list.toString();
}
}
import java.io.*;
import java.util.*;
public class Solution {
public static ArrayList<Integer>
reverse(ArrayList<Integer> a){
ArrayList<Integer>
rev = new ArrayList<Integer>();
for (int i = a.size() -
1; i >= 0; i--) {
// add the elements from end to start
rev.add(a.get(i));
}
// Return the reversed
list
return rev;
}
public static LinkedList<Integer>
revlink(LinkedList<Integer> link){
LinkedList<Integer> l=new LinkedList<Integer>();
for(int
i=0;i<4;i++){
Integer temp=link.getLast();
link.removeLast();
l.add(temp);
}
return l;
}
public static void main(String args[]){
ArrayList<Integer>
a=new ArrayList<Integer>();
a.add(new
Integer(1));
a.add(new
Integer(2));
a.add(new
Integer(3));
a.add(new
Integer(4));
System.out.println("Before: ");
for(int
i=0;i<4;i++){
System.out.print(a.get(i)+" ");
}
a=reverse(a);
System.out.println();
System.out.println("After: ");
for(int
i=0;i<4;i++){
System.out.print(a.get(i)+" ");
}
System.out.println();
LinkedList<Integer> link = new
LinkedList<Integer>();
// Adding elements to
the linked list
link.add(1);
link.add(2);
link.add(3);
link.add(4);
System.out.println("Before: " + link);
link=revlink(link);
System.out.println("After: " + link);
}
}
COMMENT DOWN FOR ANY QUERIES,
AND LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.
-----------------ArrayList--------------
//ARRAY LIST class:
public class ArrayList<E> implements List<E> {
/** Array of elements in this List. */
private E[] data;
/** Number of elements currently in this List. */
private int size;
/** The List is initially empty. */
public ArrayList() {
data = (E[])(new Object[1]); // This causes a compiler warning
size = 0;
}
public void add(E target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
public boolean contains(E target) {
for (int i = 0; i < size; i++) {
if (data[i].equals(target)) {
return true;
}
}
return false;
}
public boolean isEmpty() {
return size == 0;
}
/** Return true if data is full. */
protected boolean isFull() {
return size == data.length;
}
public java.util.Iterator<E> iterator() {
return new ArrayIterator<E>(this);
}
public E get(int index) {
return data[index];
}
public E remove(int index) {
E result = data[index];
for (int i = index + 1; i < size; i++) {
data[i - 1] = data[i];
}
size--;
return result;
}
public boolean remove(E target) {
for (int i = 0; i < size; i++) {
if (data[i].equals(target)) {
for (int j = i; j < size - 1; j++) {
data[j] = data[j + 1];
}
size--;
return true;
}
}
return false;
}
public void set(int index, E target) {
data[index] = target;
}
public int size() {
return size;
}
/** Double the length of data. */
protected void stretch() {
E[] newData = (E[])(new Object[data.length * 2]); // Warning
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
data = newData;
}
public String toString() {
String result = "[ ";
for (int i = 0; i < size; i++) {
result += data[i] + " ";
}
return result + "]";
}
public void reverseThisList()
{
int i;
for( i = 0 ; i < this.size / 2 ; i++ )
{
// swap the i th and ( n - i - 1 ) th element
E temp = this.data[i];
this.data[i] = this.data[ this.size - i - 1 ];
this.data[ this.size - i - 1 ] = temp;
}
}
public static void main(String[] args)
{
ArrayList<Integer> arr = new ArrayList<Integer>();
System.out.println("List ...\n" + arr);
arr.reverseThisList();
System.out.println("\nReversed List ...\n" + arr);
}
}
-------------------------------LinkedList-------------------------
import java.util.*;
/** A linked list. */
public class LinkedList<Integer> implements List<Integer>, Predecessor<Integer> {
/** The first node in the List. */
private ListNode<Integer> front;
/** A LinkedList is initially empty. */
public LinkedList() {
front = null;
}
public void add(Integer target) {
Predecessor<Integer> last = this;
while (last.getNext() != null) {
last = last.getNext();
}
last.setNext(new ListNode<Integer>(target));
}
public boolean contains(Integer target) {
for (ListNode<Integer> node = front;
node != null;
node = node.getNext()) {
if (node.getItem().equals(target)) {
return true;
}
}
return false;
}
public Integer get(int index) {
ListNode<Integer> node = front;
for (int i = 0; i < index; i++) {
node = node.getNext();
}
return node.getItem();
}
public ListNode<Integer> getNext() {
return front;
}
public boolean isEmpty() {
return front == null;
}
public java.util.Iterator<Integer> iterator() {
return new ListIterator<Integer>(this);
}
public Integer remove(int index) {
Predecessor<Integer> prev = this;
ListNode<Integer> node = front;
for (int i = 0; i < index; i++) {
prev = node;
node = node.getNext();
}
prev.setNext(node.getNext());
return node.getItem();
}
public boolean remove(Integer target) {
Predecessor<Integer> prev = this;
ListNode<Integer> node = front;
while (node != null) {
if (node.getItem().equals(target)) {
prev.setNext(node.getNext());
return true;
}
prev = node;
node = node.getNext();
}
return false;
}
public void set(int index, Integer target) {
ListNode<Integer> node = front;
for (int i = 0; i < index; i++) {
node = node.getNext();
}
node.setItem(target);
}
public void setNext(ListNode<Integer> next) {
front = next;
}
public int size() {
int tally = 0;
for (ListNode<Integer> node = front;
node != null;
node = node.getNext()) {
tally++;
}
return tally;
}
public String toString() {
String result = "( ";
for (ListNode<Integer> node = front;
node != null;
node = node.getNext()) {
result += node.getItem() + " ";
}
return result + ")";
}
public void reverseThisList()
{
// point trav to the first node
ListNode<Integer> trav = this.first;
// points to the previous node
ListNode<Integer> pre = null;
// points to the next node
ListNode<Integer> nxt = null;
// traverse the list
while( trav != null )
{
// point nxt to the next node
nxt = trav.getNext();
// make the current node point to the previous node
trav.setNext( pre );
pre = trav;
// make trav point to the next node
trav = nxt;
}
this.front = pre;
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
/*add(String Element) is used for adding
* the elements to the linked list*/
list.add("1");
list.add("10");
list.add("20");
list.add("30");
list.add("40");
list.add("50");
list.add("6000");
list.toString();
}
}
Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...
if we have following List classes: public class LinkedList<T> { class ListNode { protected T value; protected ListNode next; public ListNode(T val, ListNode nxt) { value = val; next = nxt; } public ListNode(T val) { this(val, null); } public ListNode() { this(null, null); } } can you write the folowing methods in java: 1.Write a method for the LinkedList class called int indexOf(T val) which returns the integer index indicating the location of val in the list, or -1...
JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1. You are given a list of phrases each ending with a pound sign: ‘#’. 2. Create a single String object from this list. 3. Then, split the String of phrases into an array of phrases...
NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...
When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Any suggestions? public class LinkedList<T> { Node<T> itsFirstNode; Node<T> itsLastNode; private int size; public LinkedList() { itsFirstNode = null; itsLastNode = null; size = 0; } public Iterator<T> getIterator() { return new Iterator(this); } // THIS WILL NEED...
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size) { this.size = size; } /** Reference to list head. */ private Node<E> head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E...
Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...
// I need help with the following questions. Please use java programming ECLIPSE language to solve the questions. YOU ONLY NEED TO DIRECTLY COPY IT IN YOUR ECLIPSE APPLICATION AND RUN IT. I NEED THOSE PART WHICH IS SAYS --> "TO BE COMPLETED" I NEED HELP WITH [GET*] AND [REPLACE ALL] AND [ADD INT DOUBLE] PLEASE. import java.util.ArrayList; public class CustomArrayList { //instance variables public int[] data; //data.length gives the capacity public int nItems; //nItems gives items currently in the...
Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...