Single Linked List Implementa

this is template:

//File: SimpleLinkedList.java.

//Add a file header comment or a class header comment to your work.

package assign3_template;

/**
* Assign 3 template. Implement more methods in List interface.
*/
public class SimpleLinkedList {

//-------Start of Assign 3 --------/

//2.1.2 Coding Requirements
//You can call other methods to complete a method.
//You can also add private methods, and then call these methods
// to complete a method required in this assignment.
//You are NOT allowed to add or remove data fields to/from SimpleLinkedList class.
//You are NOT allowed to change the definition of Node class.

//2.1.1 What Code to Add
//-----Required ---------------//

//Remove the first occurrence of the specified item from this linked list.
// If success, return true. Otherwise, return false.
public boolean removeByValue(int item) {
//add your own code

//Hint:
// call your indexOf(...) to locate the item,
// convert the following in SingleLinkedList<E>
// private E removeFirst()
// private Node<E> getNode(int index)
// private E removeAfter(Node<E> node)


//Hint: if you implement this from scratch, remember to save:
// predecessor node reference
// current node reference

return false;
}

//add item to be at [index];
// if index is [0, size-1], insert item between [index-1] and [index]
// if index is size, append item to the end of this linked list.
public void add(int index, int item) {
//add your own code

//Hint:
//convert the following in SingleLinkedList<E>
// public void add(int index, E item)
// public void addFirst(E item)
// private Node<E> getNode(int index)
// private void addAfter(Node<E> node, E item)

}

//Get the integer item at the specified position and return the integer value.
//If the index is not valid, throw an exception or print an error message
// and return Integer.MIN_VALUE.
public int get(int index) {
//add your own code

//Hint:
//convert the following in SingleLinkedList<E>
// public E get(int index)
// private Node<E> getNode(int index)

return 2;

}

//Search this linked list for the first occurrence of the specified integer: item.
//If the item is found, return its index. Otherwise return -1.
public int indexOf(int item) {
//add your own code

//Hint:
//use a looping like the one in toString()
//in addition, add a counter,
// increment the counter for each element checked
//can use size for loop control.

return -1;
}

//Find out if the specified integer: item is in this linked list.
// Returns true if yes, false otherwise.
public boolean contains(int item) {
//add your own code

//Hint:
//can either call indexOf(...)
// or directly search for the item using a loop like the one in toString()

return false;
}

//return how many integers are in this linked list
public int size() {
//add your own code

//Hint: this is actually a getter

return 0;
}

//-----Bonus ---------------//
//Remove the item at the specified position from this linked list and
// return the removed item.
//If the index is not valid, throw an exception or print an error message
// and return Integer.MIN_VALUE.
public int removeByIndex(int index) {
//Add your own code

//Hint:
//verify that index is valid
//convert the following in SingleLinkedList<E>
// private Node<E> getNode(int index)
// private E removeAfter(Node<E> node)

//Hint: if you implement this from scratch, remember to save:
// predecessor node reference
// current node reference

return 2;
}

//-------End of Assign 3 --------/

//Following code were covered in Lec#6.
//Don't change them.
//Nested class Node, enclosing class: SimpleLinkedList
private static class Node {

private int data;
private Node next;

private Node(int data) {
this.data = data;
next = null;
}

//constructor used to create a Node that has a following neighbor
private Node(int dataNew, Node nextNew) {
data = dataNew;
next = nextNew;
}

@Override
public String toString() {
return data + ""; //"10"
}
}

//the reference to the first Node in this linked list.
private Node head;
//the size of this linked list
private int size;

//create an empty linked list
public SimpleLinkedList() {
head = null;
size = 0;
}

//append newItem
public boolean add(int newItem) {
Node temp = new Node(newItem);
if (head == null) { //empty list
head = temp;
} else { //non-empty list
//locate last node
Node current = head; //start with the first node
while (current.next != null) { //check if current node is not the last node
current = current.next; //move on to the next node on the list
}
current.next = temp; //append the new node immediately following the current node
}
size++;
return true;
}

//return a string that contains all integers (in the original sequence) in the linked list.
@Override
public String toString() {
String result = ""; //result string
Node current = head; //start with first Node
while (current != null) { //check if there is still nodes remaining
result += current.data; //add the integer in current Node to the result string
result += "-->";
current = current.next; //move on to the next Node
}
return result;
}
} /// //File: SimpleLinkedListTest.java.
//Add a file header comment or a class header comment to your work.

package assign3_template;

/**
* Assign 3 Template.
* Testing different methods (successful and failed calls)
*
*/
public class SimpleLinkedListTest {

public static void main(String[] args) {

//2.2 Revise SimpleLinkedListTest

//create an empty singly linked list of int values
//append some integers

//test each implemented method using at least 2 calls:
// successful call, e.g. call indexOf(..) and pass an existing integer
// failed call, e.g. call indexOf(..) and pass a non-existing integer

//You can reuse some of the code below given to you in Lec#6.

//--Following code were given in Lec#6 --//

//create an empty list
SimpleLinkedList numbers = new SimpleLinkedList();

//append 3 integers
numbers.add(10);
numbers.add(20);
numbers.add(30);

//dump the contents in the list in the original order
System.out.println(numbers);
//or use:
//System.out.println(numbers.toString());
}

} this is the simple output: 10-->20-->30--> 10-->40-->20-->30--> 4 40 true false 3 -1 true
50 not in the list! false 40

Place this order or similar order and get an amazing discount. USE Discount code “GET20” for 20% discount

Posted in Uncategorized