So basically I have created a linked list which allows me to enter numerical data into a list and for it to be sorted, added to and deleted but I need to add text into the list as well but not sure how to go about it I've tried changing integer into string but cant seem to get it to work.
this is my file List.java
import java.io.*;
public class List {
public static Listnode head;
public List() {
head = new Listnode(0);
}
public static int readValue( )throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input;
int value =0;
boolean OK = false;
while (!OK){
try{
input = in.readLine();
value = Integer.parseInt(input);
OK = true;
}
catch (Exception e) {
System.out.println("Invalid - please enter integer value");
}
}
return value;
}
public static boolean isEmpty() {
if (head.next == null)
return true;
else
return false;
}
public static void insert() throws IOException {
Listnode current, previous, temp;
int value;
System.out.print("Enter number to insert, 0 if no more: ");
value = readValue();
while (value != 0) {
temp = new Listnode(value);
current = head;
previous = head;
while ((current.next != null) && (value > current.value)) {
previous = current;
current = current.next;
}
if (value < current.value) {
temp.next = current;
previous.next = temp;
} else
current.next = temp;
System.out.print("Enter number to insert, 0 if no more: ");
value = readValue(); }
}
public static void search() throws IOException {
Listnode current = head.next;
int sought;
boolean found = false;
if (isEmpty())
System.out.println("List is empty ");
else {
System.out.print(" Value sought: ");
sought = readValue();
while ((!found) && !(current == null)) {
if (current.value == sought) {
System.out.println(sought + " is in the list ");
found = true;
}
current = current.next;
}
if (!found)
System.out.println("Item is not in the list ");
}
}
public static void deleteItem() throws IOException {
Listnode current, previous, temp;
int sought;
boolean found = false;
if (isEmpty())
System.out.println("List is empty ");
else {
System.out.print("Item to delete: ");
sought = readValue();
current = head;
previous = head;
while (!found && !(current == null)) {
if (current.value == sought) {
previous.next = current.next;
System.out.println(sought + " deleted from list ");
found = true;
}
else {
previous = current;
current = current.next;
}
}
if (!(found))
System.out.println("Item is not in the list ");
}
}
public static void writeList() {
Listnode current = head.next;
if (isEmpty())
System.out.println("List is empty ");
else {
while (current != null) {
System.out.println(current.value);
current = current.next;
}
}
}
}
This is my Listnode.java file
public class Listnode {
int value;
Listnode next;
public Listnode(int data) {
value = data;
next = null;
}
}
Listtest.java file
import java.io.*;
public class Listtest {
public static int readValue( )throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input;
int value =0;
boolean OK = false;
while (!OK){
try{
input = in.readLine();
value = Integer.parseInt(input);
OK = true;
}
catch (Exception e) {
System.out.println("Invalid - please enter integer value");
}
}
return value;
}
public static void main(String[] args) throws IOException {
List mylist = new List();
int choice = 7;
System.out.println("Start by adding some items to the list...");
mylist.insert();
while (choice != 0) {
System.out.println(
"------------------------------------------");
System.out.print(
"1..Write out list 2..Insert items 3..Search 4..delete 0..Quit: ");
choice = readValue();
if (choice == 1)
mylist.writeList();
else if (choice == 2)
mylist.insert();
else if (choice == 3)
mylist.search();
else if (choice == 4)
mylist.deleteItem();
}
}
}
So need to be able to add text into list as well so inserting into list wont just be "0800" but can be " 0800 Berlin" or something along those lines but I still need the wrtie out list, insert items, search and delete functions