mercredi 4 mars 2015

substring() method for linked list object



I'm having some trouble writing a substring() method for a class I'm building called LString. This class creates a linked list object called LString that builds strings. It mimics the String and StringBuilder objects in Java.


substring(int start, int end) creates a new LString out of the given this LString, from the index provided by start to end. It returns type LString.


Here is my code:



import java.io.*;
import java.util.*;

public class LString {

node front;
int size;

//Creating a node class
private class node {
char data;
node next;

public node (){
}

public node (char newData){
this.data = newData;
}

public node (char newData, node newNext){
this.data = newData;
this.next = newNext;
}


}
//Constructors
public LString(){
this.size = 0;
this.front = null;
}
public LString(String original) {
this.size = original.length();
if (original.length() > 0){

this.front = new node(original.charAt(0));
node curr = this.front;

for (int i =1; i < original.length(); i++) {
curr.next = new node(original.charAt(i));
curr = curr.next;
}
}



}

// Length method, returns the length of LString
public int length() {
return this.size;
}

// compareTo method, compares this LString to anotherLString, returns 0 if equal,
// -1 if lexicogrpahically less, and 1 if lexicographically greater
public int compareTo(LString anotherLString) {
int len1 = length();
int len2 = anotherLString.length();
int lim = Math.min(len1, len2);

node cn1 = front;
node cn2 = anotherLString.front;

int k = 0;
while (k < lim) {
char c1 = cn1.data;
char c2 = cn2.data;
if (c1 != c2) {
return c1-c2;
}
k++;
cn1 = cn1.next;
cn2 = cn2.next;
}
return len1 - len2;

}

// a boolean equals method that returns true if LString and other are the same, false if not
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other instanceof LString) {
LString otherLString = (LString)other;
int n = length();
if (n == otherLString.length()) {
node n1 = front;
node n2 = otherLString.front;
while (n1 != null) {
if (n1.data != n2.data) {
return false;
}
n1 = n1.next;
n2 = n2.next;
}
return true;
}
}

return false;
}


// charAt returns the character of LString at the argument index
public char charAt(int index) {

if ((index < 0) || (index >= this.length())) {
throw new IndexOutOfBoundsException();
}
node curNode = front;
for (int i = 0; i < this.length(); i++, curNode = curNode.next) {
if (i == index) {
return curNode.data;
}
}
throw new IllegalStateException();
}

//
public void setCharAt(int index, char ch) {
if (index < 0 || index >= this.length()) {
throw new IndexOutOfBoundsException();
}
else {
node currNode = front;
for (int i = 0; i <this.length(); i++, currNode = currNode.next) {
if (i == index) {
currNode.data = ch;
}
}
}
}

public LString substring(int start, int end) {
if (start == end == this.length()) {
return null;
}
if (start < 0 || end > this.length() || start > end) {
throw new IndexOutOfBoundsException();
}
// construct a string out of the characters in this LString?
// turn string in LString with constructor : LString(String original)?
//what I have so far:
node currentNode = front;
for (int i = start; i < end; i++, currentNode = currentNode.next) {
currentNode.data = charAt(i);
}//?


}
}


I'm just caught up in how to write the method efficiently, and I've been working on this for awhile so I need some fresh eyes. Thanks for any advice, trying to learn Java.




Aucun commentaire:

Enregistrer un commentaire