简体   繁体   中英

Putting Characters in Alphabetical Order in Nodes (Java)

I have been trying to figure out how to do this for about 4 days now and I just can't grasp the concept.

Main Program:

String answer = "y";  
        String n = null;  
        char nodePointer = ' ';  
        KeyboardReader reader = new KeyboardReader();  

        Node start = null;  
        Node last = null;  
        Node temp = null;  

do{  

                    while(start && !(temp < last->start)){  
                        last = start;    
                        start = start->last;  
                    }  
                    if(last == temp)  
                        temp = new Node(nodePointer,temp);  
                    else  
                        temp = new Node(nodePointer,start);  
                    while(temp != null){  
                        System.out.print(temp.letter);  
                        temp = temp.nodeptr;  
                    }
}while(answer.compareTo(n) == 0);  

Constructor:

public class Node {

char letter;  
Node nodeptr;  

Node(){  
    letter = ' ';  
    nodeptr = null;  
}

Node(char x){  
    letter = x;  
    nodeptr = null;  
}  

Node(char x, Node y){  
    letter = x;  
    nodeptr = y;  
}  
}  

Can someone please help me make the output as follows:

Enter a letter: m
Linked list: m
Would you like to enter another letter (y/n)? y

Enter a letter: o
Linked list: mo
Would you like to enter another letter (y/n)? y

Enter a letter: n
Linked list: mno
Would you like to enter another letter (y/n)? y

Enter a letter: e
Linked list: emno
Would you like to enter another letter (y/n)? y

Enter a letter: y
Linked list: emnoy
Would you like to enter another letter (y/n)? n

Thank you for using my program today!

PS: The main program probably doesn't work because I am honestly so lost I legitimately cannot grasp the concept... And yes, this is a homework assignment.. :(

I'm not in mood to open my Java editor, you'll need to convert the code to Java and to make those inputs, but I can give you an idea here:

var myList = new List();

function GetAlphabeticalPlace(char e){
  return "ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(Character.toUpperCase(e));
}

function AssignOrder(char e){

  if(myList.length == 0) {
     myList.insert(0, e);
     return;
  } 
  for(var i = 0, i < myList.length, i++){
    if (GetAlphabeticalPlace(e) < GetAlphabeticalPlace(myList[i])){
       myList.insert(i, e); //insert the char in the corresponding index
       exit;
    }
  }
}

function PrintList(List l){
  var res = "";
  for (var i = 0, i  < l.length, i++){
    res += l[i];
  }
  return res;
}

console.write(PrintList(myList));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM