Use the provided LinkedList.java file to define the toString, getLength, clear,
ID: 644267 • Letter: U
Question
Use the provided LinkedList.java file to define the toString, getLength, clear, addEnd, replace and get methods (unfinished in the file) using the new methods stepStart and step. The previous methods display and remove have been modified to use these two new methods. This will give a consistent pattern to some of the methods you need to define for this assignment.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ecet370wk2;
/**
*
* @author joel
*/
public class LinkedList {
public LinkedList() {
first = new Node();
first.next = null;
}
public boolean isEmpty() {
return (first.next == null);
}
public void display() {
/*Node temp = first.next;
while(temp != null)
{
System.out.print(temp.item+" ");
temp = temp.next;
}
System.out.println();*/
stepStart();
if (curr == null)
System.out.print("empty list");
else
do {
if (curr!=null)
System.out.print(curr.item + " ");
}while(step());
System.out.println();
}
public void add(int x) {
Node temp = new Node();
temp.item = x;
temp.next = first.next;
first.next = temp;
}
// removes first found instance (last added)
public boolean remove(int x) {
boolean found = false;
/*Node current = first.next, previous = first;
while (current != null && !found) {
if (current.item == x)
found = true;
else {
previous = current;
current = current.next;
}
}
if (found)
previous.next = current.next;*/
stepStart();
do {
if (curr!=null)
if (curr.item == x) {
found = true;
break;
}
}while(step());
if (found)
prev.next = curr.next;
return found;
}
@Override
public String toString()
{
}
public int getLength()
{
}
public void clear()
{
}
public void addEnd(int x)
{
}
public boolean replace (int loc, int x)
{
}
public int get(int loc)
{
}
protected class Node {
private int item;
private Node next;
}
private void stepStart()
{
prev = first;
curr = first.next;
}
private boolean step()
{
if (curr == null)
return false;
else {
prev = curr;
curr = curr.next;
return true;
}
}
private Node first, prev, curr;
}
Explanation / Answer
public class LinkedList {
public LinkedList() {
first = new Node();
first.next = null;
}
public boolean isEmpty() {
return (first.next == null);
}
public void display() {
/*
* Node temp = first.next; while(temp != null) {
* System.out.print(temp.item+" "); temp = temp.next; }
* System.out.println();
*/
stepStart();
if (curr == null)
System.out.print("empty list");
else
do {
if (curr != null)
System.out.print(curr.item + " ");
} while (step());
System.out.println();
}
public void add(int x) {
Node temp = new Node();
temp.item = x;
temp.next = first.next;
first.next = temp;
}
// removes first found instance (last added)
public boolean remove(int x) {
boolean found = false;
/*
* Node current = first.next, previous = first; while (current != null
* && !found) { if (current.item == x) found = true; else { previous =
* current; current = current.next; } } if (found) previous.next =
* current.next;
*/
stepStart();
do {
if (curr != null)
if (curr.item == x) {
found = true;
break;
}
} while (step());
if (found)
prev.next = curr.next;
return found;
}
@Override
public String toString() {
String ret = "";
stepStart();
while (curr != null) {
ret = ret.concat(Integer.toString(curr.item) + " ");
step();
}
return ret;
}
public int getLength() {
int length = 0;
stepStart();
while (curr != null) {
length++;
step();
}
return length;
}
public void clear() {
first = null;
}
public void addEnd(int x) {
Node temp = new Node();
temp.item = x;
temp.next = null;
stepStart();
while (curr != null) {
step();
}
prev.next = temp;
}
public boolean replace(int loc, int x) {
int count = 0;
stepStart();
while(curr != null){
if(count == loc){
curr.item = x;
return true;
}
step();
}
return false;
}
public int get(int loc) {
int count = 0;
stepStart();
while(curr != null){
if(count == loc){
return curr.item;
}
step();
}
return -1;
}
protected class Node {
private int item;
private Node next;
}
private void stepStart() {
prev = first;
curr = first.next;
}
private boolean step() {
if (curr == null)
return false;
else {
prev = curr;
curr = curr.next;
return true;
}
}
private Node first, prev, curr;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.