我在处理搜索和删除链接列表的小java活动时遇到了一点麻烦。
以下是问题:
以下是课程:
public class LLNode {
private String data;
private LLNode next;
public LLNode() {
this.data = null;
this.next = null;
}
public LLNode (String newData) {
this.data = (newData);
this.next = null;
}
public void updateNode (LLNode nextOne) {
this.next = nextOne;
}
public String toString () {
return this.data;
}
public LLNode getNext() {
return this.next;
}
}
public class LList {
private LLNode head;
public LList() {
head = null;
}
public void addAtHead (String newData) {
LLNode newNode = new LLNode (newData);
newNode.updateNode(head);
head = newNode;
}
public void display() {
LLNode temp = head;
while (temp != null) {
System.out.println (temp);
temp = temp.getNext();
}
}
public LLNode deleteAtHead ( ) {
LLNode removedOne = head;
head = head.getNext();
return removedOne;
}
}
public class LinkedListExample {
public static void main(String[] args) {
LList list = new LList();
list.addAtHead("Bob");
list.addAtHead("Tom");
System.out.println("The list is ");
list.display();
LLNode removedOne = list.deleteAtHead();
System.out.println("After delete, the list new is ");
list.display();
System.out.println("The one that was deleted is..." + removedOne);
}
}
答案 0 :(得分:0)
{
public void main(String[] args) {
string input;
Scanner n = new Scanner(System.in);
while (!(input.equals("exit")) {
System.out.println("menu item 1");
System.out.println("menu item 2");
System.out.println("etc");
input = n.nextLine();
switch (input) {
case "menu 1": //do whatever menu 1 is
case "menu 2": //do whatever menu 2 is
case "exit": //exit // save whatever
default: System.out.println("message not understood");
}
}
这是一个包含方法。这应该为您提供有关如何在链表中查找元素以及如何删除元素的强烈指示。 (我将此留给您,因为这相对容易,您需要学习)。
public boolean contains(String str) {
Node ref;
while (ref != null)
ref = ref.next;
if (ref.data == str) {
return true;
}
return false;
}