所以我为我的数据结构类分配了一个作业,我已经尽了最大的努力,但是我目前仍停留在这部分作业上,我需要一些帮助。作业基本上需要您创建一个Java程序,该程序是一个学生记录数据库,该数据库存储6个变量:使用链接列表的id,name,subCode,subName,sem和year。到目前为止,这是我所做的。
主类:
import java.util.*;
public class Main {
public int id =0;
public String name= "";
public String subCode="";
public String subName="";
public String sem;
public int year;
LinkedList list = new LinkedList();
Scanner scan = new Scanner(System.in);
public void Menu() {
boolean valid = true;
while (valid) {
final String menu[] = {"Main Menu",
"1. Register student's subjects",
"2. Retrieve all students' subjects listing",
"3. Retrieve a student's subjects listing",
"4. Update a student's registered subjects",
"5. Delete a student's registered subjects",
"6. Delete a student registration",
"7. Exit"};
for (String menuList : menu)
System.out.println(menuList);
switch (scan.nextInt()) {
case 1:
create();
break;
case 2:
list.retrieveAll();
break;
case 3:
//retrieveOne();
break;
case 4:
//update();
break;
case 5:
//deleteSub();
break;
case 6:
deleteStud();
break;
case 7:
System.out.println("Have a nice day!");
valid = false;
break;
default:
System.out.println("You have not entered a valid option");
break;
}
}
}
public void create() {
System.out.println();
boolean validate = true;
while(validate) {
try {
System.out.println("Please enter Name");
scan.nextLine();
name = scan.nextLine();
//validate =Validation.validateName(name);
System.out.println("Please enter Student ID");
String tempID=scan.nextLine();
// validate=Validation.validateId(tempID);
id= Integer.parseInt(tempID);
System.out.println("Please enter student Subject Code");
subCode=scan.nextLine();
System.out.println("Please enter Subject's Name");
subName = scan.nextLine();
System.out.println("Please enter Student's semester");
sem = scan.nextLine();
System.out.println("Please enter Student's Year of Study");
year = scan.nextInt();
scan.nextLine();
list.create(id, name, subCode, subName, sem, year);
validate=false;
/* list.create(17052960, "Samuel Ho", "CSCP 2014", "Programming 2", "Fall 2017", 2);
list.create(17040007, "Jodi Mak", "ENVS 1014", "Enviromental Studies", "Fall 2016", 3);
list.create(17022880, "Yee Hong Chun", "MATH 1024", "Calculus 1", "Fall 2018", 1);
list.retrieveAll();
list.deleteAt(1);
list.retrieveAll();*/
}catch(Exception e) {
validate = true;
}
}
}
public void deleteStud() {
System.out.println("Enter Student ID of the student you wish to delete");
int tempID = scan.nextInt();
scan.nextLine();
}
public static void main(String[] args) {
Main main = new Main();
main.Menu();
}
}
LinkedList类:
public class LinkedList {
Node head;
public void create(int id, String name, String subCode, String subName, String sem, int year) {
Node node= new Node();
node.id=id;
node.name=name;
node.subCode=subCode;
node.subName=subName;
node.sem=sem;
node.year=year;
node.next=null;
if (head==null) {
head=node;
}else {
Node n = head;
while (n.next!=null) {
n=n.next;
}
n.next=node;
}
}
public void retrieveAll() {
Node node=head;
while(node.next!=null) {
System.out.printf("%d %3s %10s %3s %14s %3d\n",
node.id, node.name, node.subCode, node.subName, node.sem, node.year );
node=node.next;
}
System.out.printf("%d %3s %10s %3s %14s %3d\n",
node.id, node.name, node.subCode, node.subName, node.sem, node.year );
}
public void deleteAt(int index) {
if (index==0)
head=head.next;
else {
Node n = head;
Node n1=null;
for (int i =0; i<index-1;i++) {
n=n.next;
}
n1=n.next;
n.next = n1.next;
System.out.println("Deleted " + n1.id);
n1=null;
}
}
}
节点类:
class Node {
int id;
String name;
String subCode;
String subName;
String sem;
int year;
Node next;
}
所以这是我所做的,还远未完成,但是我需要快速进行,并且陷入了deleteStud()方法的困境。我需要删除其学生ID的用户输入。然后,它在链接列表中搜索并删除该学生。我已经实现了deleteAt的某个索引,但是不知道如何从其ID中搜索和删除该学生。这也将帮助我的restoreOne()方法,在该方法中,我必须根据用户输入的ID显示学生的记录。
希望我不要太忙了,但我确实需要您的帮助。非常感谢。
答案 0 :(得分:0)
除了检查node.Id而不是Index之外,它是否与deleteAt函数完全相同?
这里的解决方案很粗糙(我什么也没做)。
public void deleteId(int ID){
Node previousNode=head;
Node currentNode=head;
while(currentNode!=null){
if(currentNode.id==ID){
previousNode.next=currentNode.next;
currentNode=currentNode.next;
}else{
previousNode=currentNode;
currentNode=currentNode.next;
}
}
System.out.println("Deleted " + ID);
}
这段代码本质上的作用是,它遍历从头开始的所有节点,并跟踪您正在检查的当前节点和您先前检查的节点。
如果当前节点的ID等于您要查找的ID,我们可以通过调整上一个节点的下一个指针,从链接列表中删除该节点。