我想確定兩個單鏈表是否相同。 如果它們相同,程序應該打印匹配的字母。確定兩個單鏈表是否相同?
例如: 雜音和牙垢是相同的,因爲它們都具有相同的模式「abcabc」。
AAABBCbbaaa和11122322111相同
匹配的字母: 甲↔1 乙↔2 ç↔3
我必須僅使用單鏈接列表。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter a string:");
String linked1=scanner.nextLine();
System.out.println();
System.out.print("Please enter another string:");
String linked2=scanner.nextLine();
SingleLinkedList SLL1 = new SingleLinkedList();
SingleLinkedList SLL2 = new SingleLinkedList();
for (int i = 0; i < linked1.length(); i++) {
char a=linked1.charAt(i);
a = Character.toLowerCase(a);
SLL1.addToEnd(a);
}
for (int i = 0; i < linked2.length(); i++) {
char a=linked2.charAt(i);
a = Character.toLowerCase(a);
SLL2.addToEnd(a);
}
public class SingleLinkedList{
private Node head;
public SingleLinkedList()
{
head = null;
}
public boolean isEmpty(){
return head == null;
}
public void addToEnd(Object dataToAdd)
{
Node newNode = new Node(dataToAdd);
if(head == null)
{
head = newNode;
}
else
{
Node temp = head;
while(temp.getLink() != null)
{
temp = temp.getLink();
}
temp.setLink(newNode);
}
}
public String display()
{
String output = "";
Node temp = head;
while(temp != null)
{
output += temp.getData() + " ";
temp = temp.getLink();
}
return output;
}
}
那麼你到目前爲止嘗試過什麼? –
我試過創建2臨時單鏈接ist然後,我想刪除字符到這個臨時工。但順序是重要的,我不能正確地做出。做得很好 –
你到目前爲止試圖比較兩個列表是否相等?我們可以幫助您解決遇到的問題,但首先您必須嘗試一些方法。請參閱http://stackoverflow.com/help/on-topic:「要求提供家庭作業幫助的問題必須包括您迄今爲止解決問題所做的工作摘要,以及您解決問題的難度的描述「。 –