subject

Requirements:

1) You must follow the pattern (and starter code) given by Dr. Rimland in class

2) You are not allowed to use Iterators

3) Your list cannot be doubly linked.

Step 1: Create a new Java project named LinkedListProject



Step 2: Create a new Java class in your project named LinkedStringList



Step 3:

Add an inner Node class as demonstrated in class.



Step 4:

You will be given code for the following methods in class. You should include these in your project:

LinkedStringList() (an empty constructor)

addFirst() (adds an element in the first position)

setFirstValue() (sets the value of the element in the first position)

setCurrentValue() (sets the value of the element in the current position)

moveNext() (moves the current reference to the next element)

moveFirst() (moves the current reference to the first position)

isEmpty() (returns whether the list is empty)

getLength() (returns the length of the list)

displayList() (displays all elements in the list)

Here is the code for these ^

import java. util. NoSuchElementException;

public class LinkedStringList {

private Node first;

private Node currentNode;

private int length;

class Node

{

private String data;

private Node next;

public void printNodeData()

{

System. out. println("Node data: " + data);

}

public Node getNext()

{

return next;

}

}

public LinkedStringList()

{

first = null;

currentNode = null;

length = 0;

}

public void addFirst(String value)

{

//create the Node and set its value

Node newNode = new Node();

newNode. data = value;

// if newNode is the first node, this will be null

// otherwise it will point to the former "first" now

newNode. next = first;

//set our "first" Node to the Node we just created

first = newNode;

currentNode = newNode;

length++;

}

public void setFirstValue(String value)

{

first. data = value;

}

public void setCurrentValue(String value)

{

currentNode. data = value;

}

public void moveNext()

{

if (currentNode. next == null)

{

throw new NoSuchElementException();

}

else

{

currentNode = currentNode. next;

}

}

public void moveFirst()

{

currentNode = first;

}

public boolean isEmpty()

{

return (first == null);

}

public int getLength()

{

return length;

}

public String getFirstValue()

{

if (first == null)

{

throw new NoSuchElementException();

}

else

{

return first. data;

}

}

public String getCurrentValue()

{

if (currentNode == null)

{

throw new NoSuchElementException();

}

else

{

return currentNode. data;

}

}

public void displayList()

{

Node currentNode = first;

System. out. println("List contents:");

while (currentNode != null)

{

currentNode. printNodeData();

currentNode = currentNode. getNext();

}

}

}



Step 5:

Add the following methods on your own:

add(String value) - add an element in the current position

getCurrentValue() - returns the value of the current element

removeFirst() - remove the first element

remove() - remove the current element

indexOf(String value) - return the position of value, or -1 if not found

sortAscending() - sort your linked list in ascending order using a Selection Sort as shown in class.



Step 6:

In your main method:

Create an instance of your LinkedStringList class named list.

Call your add() method 3 times to add the values "First", "Second", and "Third" to list.

Use moveFirst(), moveNext(), and setCurrentValue() to replace those values with "Red", "Green", and "Blue" (respectively).

Use your indexOf() method to display the position of the "Green" string.

Use displayList() to display the contents of list.

Use your sortAscending() method to sort the list in ascending order.

Use displayList() to display the contents of mySortedList.

Call your remove() method twice on mySortedList.

Use displayList() to display the contents of mySortedList (again).

ansver
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 21.06.2019 17:10, isophk
Can be categorized as popularity, relevancy, and user satisfaction. select one: a. mobile search seo b. ranking factors c. serp d. web analytics
Answers: 1
image
Computers and Technology, 22.06.2019 10:40, almaga1979orfvwo
5. illustrate how fine-line inventory classification can be used with product and market segments. what are the benefits and considerations when classifying inventory by product, market, and product/market?
Answers: 2
image
Computers and Technology, 22.06.2019 11:10, golderhadashaowtatz
Which are not examples of chronic or persistent stress? moving
Answers: 1
image
Computers and Technology, 23.06.2019 06:00, hilarydodard7099
Which statistical function in a spreadsheet you to see how far each number varies, on average, from the average value of the list?
Answers: 2
You know the right answer?
Requirements:

1) You must follow the pattern (and starter code) given by Dr. Rimland in...

Questions in other subjects:

Konu
Mathematics, 19.03.2021 18:20
Konu
Mathematics, 19.03.2021 18:20