【发布时间】:2016-01-18 08:28:46
【问题描述】:
各位程序员大家好! 我想问一下如何使用Java(数据结构)实现单链表中的搜索功能。
问题:我的程序的搜索功能只能访问元素的头部和尾部。 问题:如何访问所有元素?
示例代码:
import java.util.*;
public class MyLinkedList<T> {
private Node<T> head;
private Node<T> tail;
public void add(T element){
Node<T> nd = new Node<T>();
nd.setValue(element);
if(head == null){
head = nd;
tail = nd;
}
else {
tail.setNextRef(nd);
tail = nd;
}}
public void delete(){
if(head == null){
System.out.println("List is empty...");
}
else{
Node<T> tmp = head;
head = tmp.getNextRef();
if(head == null){
tail = null;
}
System.out.println("Deleted: "+tmp.getValue());
System.out.println("First Employee Name Deleted!");
}
}
public void search(){
Node<T> tmp = head;
if(head==null){
System.out.println("List is empty...");
}
else{
String b=""+tmp.getValue();
Scanner input=new Scanner(System.in);
System.out.print("Enter an employee name to search: ");
String search = input.nextLine();
if(b.equals(search)){
System.out.println("Found! "+tmp.getValue());
}
else if (b!=search){
System.out.println("Not Found!");
}
tmp = tmp.getNextRef();
} }
public void show(){
Node<T> tmp = head;
if(head==null){
System.out.println("List is empty...");
}
else{
while(true){
if(tmp == null){
break;
}
System.out.println(tmp.getValue());
tmp = tmp.getNextRef();
}}
}
public static void main(String a[]){
MyLinkedList<String> sl = new MyLinkedList<String>();
Scanner input = new Scanner(System.in);
sl.add("Garcia, Bianca Axel, 001");
sl.add("Temprosa, Camille Ann, 002");
sl.add("Villanueva, Von Justin, 003");
sl.add("Rivera, Reygie, 004");
sl.add("Dapapac, Ronnelle, 005");
sl.add("Bati, Aubrey, 006");
sl.add("Fiestada, Diana Rose, 007");
sl.add("Dobalada, Jojo, 008");
sl.add("Del Mundo, Maria Ethel, 009");
sl.add("Alejandro, Rachelle, 010");
System.out.print("Welcome To Singly Linked List Management for Employee Names!\n(Created by: Alex del Rosario || Source Code: Mr. John Carlo Son)\n");
while(true){
System.out.println("*------------------------*");
System.out.println("*PLEASE SELECT A FUNCTION*");
System.out.println("*1.)Add Employee Name *");
System.out.println("*2.)Delete Employee Name *");
System.out.println("*3.)Show Employee List *");
System.out.println("*4.)Search an Employee *");
System.out.println("*5.)Exit the program *");
System.out.println("*------------------------*");
System.out.print("Enter a number: ");
int select = input.nextInt();
if(select==1){
System.out.println("**Enter an employee name with ID number**");
System.out.println("Use this format:(Last Name),(First Name),(ID number)");
System.out.println("(ex. del Rosario, Alex, 0001)");
System.out.print("Input here: ");
input.nextLine();
String employee=input.nextLine();
sl.add(employee);
System.out.println("Employee: "+employee+" successfully added!");
}
else if(select==2){
sl.delete();
}
else if(select==3){
System.out.println("Employee List:");
sl.show();
}
else if(select==4){
sl.search();
}
else if(select==5){
System.out.println("*** THANK YOU FOR USING THIS PROGRAM! :) ***");
System.exit(0);
}
else{
System.out.println("INVALID INPUT!");
}
}}
}
class Node <T> implements Comparable<T> {
private T value;
private Node<T> nextRef;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public Node<T> getNextRef() {
return nextRef;
}
public void setNextRef(Node<T> ref) {
this.nextRef = ref;
}
@Override
public int compareTo(T arg) {
if(arg == this.value){
return 0;
} else {
return 1;
}
}
}
【问题讨论】:
-
请花时间格式化您的代码以便于阅读。
-
您确定不能访问以下节点吗?在您的搜索中,缺少循环或遍历所有元素的内容。
-
很抱歉给您带来不便先生...我只是新来的...
-
此外,您的代码似乎是许多代码的一个片段:
MyLinkedListString sl = new MyLinkedListString();,而您的列表称为public class MyLinkedListAlex和class Node,而不是private NodeAlex head;。请在您的眼中完成后再次发布您的代码。
标签: java data-structures linked-list singly-linked-list