Here is what I am trying. I am trying to create a method that takes my object LString and converts the integer argument into an object LString. This is using Linked lists. I have a Node class which constructs and initializes nodes. This is the last method for me to complete on a project.
I am confused on how to go about it however. I have been creating a string class using linked lists and nodes. How do I convert this integer argument into my object type of LString?
Here are parts of my LString class that seem relevant to the question:
HTML Code:
public class LString{
private Node front ; //first val in list *******CHANGED
private Node back; //last val in list
private int size = 0;
private int i;
private int offset;
public LString(){
//construct empty list
Node LString = new Node();
front = null;
}
//return value of specified index
public char charAt(int index){
Node current = front;
for(int i = 0; 0 < index; i++){
current = current.next;
}
return current.data;
}
//return number of chars of lstring
public int length(){
int count = 0;
Node current = front;
while(current != null){
current = current.next;
count++;
}
return count++;
}
public String toString(){
if(front == null){
return "[]";
} else {
String result = "[" + front.data;
Node current = front.next;
while(current != null){
result += current.data; //might need to add ", page 967
current = current.next;
}
result += "]";
return result;
}
}
my attempt although wrong:
HTML Code:
public static LString valueOf(int i){
int c;
char m;
Node current = new Node();
// convert the String to int
for(int w = i;w < i; w++) {
c = i % 10;
i = i / 10;
m = (char) ('0' + c);
LString ans = new LString();
}
return LString;
}
public void add(int value){
if(front == null){
front = new Node(value);
} else{
Node current = front;
while(current.next != null){
current = current.next;
}
current.next = new Node(value);
}
}
}
My Node class:
HTML Code:
public class Node{
public char data;
public Node next;
//constructors from page 956
public Node()
{
this('\0',null); //'\0' is null char for java
}
public Node(char initialData, Node initialNext)
{
data = initialData;
next = initialNext;
}
public void addNodeAfter(char element)
{
next = new Node(element, next);
}
public char getData()
{
return data;
}
public Node getNext(){
return next;
}
public void setNext(Node n){
next = n;
}
public void setData(char d){
data = d;
}
}