It doesn't say anywhere but it's pretty much common practice. Ok, thank you for your answer.
So, now it's placing the sentence inside the A list but not on the B list. And, for some reason, it's not placing on the correct order on both of them.
I have the list class like this, now:
PHP Code:
package pacote;
public class list {
private node cab;
public list()
{
cab = new node("");
cab.setprev(cab);
cab.setnext(cab);
}
public void receiveinsert(String insert){
String [] words = insert.split(" ");
for (int i = 0; i < words.length; i++){
this.insertordered(words[i]);
}
}
public void receiveinsert2(String insert){
String [] words = insert.split(" ");
for (int i = 0; i < words.length; i++){
this.insertinverted(words[i]);
}
}
public void insertordered(String word){
node newnode=new node(word);
node temp=cab.getnext();
while(temp.getnext()!=cab &&(temp.getnext().getword().compareTo(newnode.getword())<0)){
temp=temp.getnext();
}
newnode.setnext(temp.getnext());
newnode.setprev(temp);
temp.setnext(newnode);
}
public void insertinverted(String word){
node newnode=new node(word);
node temp=cab.getnext();
while(temp.getnext()!=cab &&(temp.getnext().getword().compareTo(newnode.getword())>0)){
temp=temp.getnext();
}
newnode.setnext(temp.getnext());
newnode.setprev(temp);
temp.setnext(newnode);
}
public String printordered()
{
String st = "";
node temp = cab.getnext();
if (temp!= cab)
{
while(temp != cab)
{
st += temp.getword() + " ";
temp = temp.getnext();
}
}
else
st += "The list is empty!";
return st;
}
public String printinverted()
{
String st = "";
node temp = cab.getprev();
if (temp!= cab)
{
while(temp != cab)
{
st += temp.getword() + " ";
temp = temp.getprev();
}
}
else
st += "The list is empty!";
return st;
}
}
And I also had to change the menu class:
PHP Code:
public static void main(String[] args) {
list A = new list();
list B = new list();
int option = options();
while (option != 0) {
String sentence;
switch (option) {
case 1:
{
sentence = JOptionPane.showInputDialog("Input sentence: ");
A.receivesentence(sentence);
B.receivesentence2(sentence);
break;
}
case 2:
{
// sentence = JOptionPane.showInputDialog("Input sentence to reverse: ");
// B.receivesentence2(sentence);
break;
}
case 3: {
break;
}
case 8:
{
JOptionPane.showMessageDialog(null,A.printordered());
JOptionPane.showMessageDialog(null,B.printinverted());
break;
}
}
option = options();
}
}
}