2017-03-27 119 views
-1

程序的功能:所以我有一個應該給用戶4個選項的程序......添加學生,刪除學生,打印學生信息並退出。如果用戶輸入1,那麼添加的學生將被選中,它會詢問你想添加多少學生。然後,他們將輸入他們需要添加的人數,他們將填寫學生課程添加的所有內容。如果選擇了選項2,則會按名稱從鏈接列表中刪除學生。如果選擇了選項3,那麼它只會打印學生姓名,郵編和專業(其他人將用於不同的項目)。如果選擇了選項4,它將退出。它將繼續顯示該菜單,直到選擇4。鏈接列表學生管理系統

我的問題是:我需要幫助將學生添加到鏈接列表中,並通過學生的名字將其刪除。還有 如果有人可以向我解釋toString(我的老師告訴我這樣做),這將非常感激。

學生類別:

public class Student { 
private int studentID; 
private String fName; 
private String lName; 
private String Address; 
private String city; 
private String state; 
private String zip; 
private String major; 


public void Student(){ 

} 

public void Student(int i, String f, String l, String a, String c, String s, String z, String m){ 
    studentID = i; 
    fName = f; 
    lName = l; 
    Address = a; 
    city = c; 
    state = s; 
    zip = z; 
    major = m; 
} 

public int getStudentID(){ 
    return studentID; 
} 
public void setStudentID(int i){ 
    studentID = i; 
} 
public String getFName(){ 
    return fName; 
} 
public void setFName(String f){ 
    fName = f; 
} 
public String getLName(){ 
    return lName; 
} 
public void setLName(String l){ 
    lName = l; 
} 
public String getAddress(){ 
    return Address; 
} 
public void setAddress(String a){ 
    Address = a; 
} 
public String getCity(){ 
    return city; 
} 
public void setCity(String c){ 
    city = c; 
} 
public String getState(){ 
    return state; 
} 
public void setState(String s){ 
    state = s; 
} 
public String getZip(){ 
    return zip; 
} 
public void setZip(String z){ 
    zip = z;; 
} 
public String getMajor(){ 
    return major; 
} 
public void setMajor(String m){ 
    major = m; 
} 

} 

鏈表類:

public class LinkedList { 

private class Node{ 
    String value; 
    Node next; 

    Node (String val, Node n){ 
     value=val; 
     next = n; 
    } 

    Node (String val) 
    { 
     this(val, null); 

    } 
} 
private Node first; 
private Node last; 

public LinkedList(){ 
    first = null; 
    last = null; 
} 

public boolean isEmpty(){ 
    return first== null; 
} 

public int size(){ 
    int count = 0; 
    Node p = first; 
    while (p !=null){ 
     count++; 
     p = p.next; 
    } 
    return count; 
} 

public void add(String s){ 
    if (isEmpty()) 
    { 
     first = new Node(s); 
     last = first; 
    } 
    else 
    { 
     last.next = new Node(s); 
     last = last.next; 

    } 
} 

public void add(int index, String s){ 
    if (index <0 || index > size()){ 
     String message = String.valueOf(index); 
     throw new IndexOutOfBoundsException(message); 
    } 


    if (index ==0){ 
     first = new Node(s, first); 
     if (last == null) 
      last = first; 
     return; 
    } 

    Node pred = first; 
    for (int k = 1; k <= index -1; k++){ 
     pred = pred.next; 
    } 
    pred.next = new Node (s, pred.next); 

    if (pred.next.next == null){ 
     last = pred.next; 
    } 
} 
public String toString(){ 
StringBuilder strBuilder = new StringBuilder(); 

Node p = first; 
while (p != null){ 
    strBuilder.append(p.value+"\n"); 
    p = p.next; 

    } 
    return strBuilder.toString(); 
} 

public String remove(int index){ 
    if (index <0 || index >=size()){ 
     String message = String.valueOf(index); 
     throw new IndexOutOfBoundsException(message); 
    } 

    String element; 
    if (index == 0){ 
     element = first.value; 
     first = first.next; 
     if (first == null){ 
      last = null; 
     } 
    } 
    else 
    { 
     Node pred = first; 
     for (int k = 1; k <=index -1; k++) 
      pred = pred.next; 


     element = pred.next.value; 
     pred.next = pred.next.next; 
     if (pred.next == null) 
      last = pred; 

    } 
    return element; 
} 




} 

主(這是非常糟糕的,到目前爲止):

import java.util.Scanner; 
public class StudentDemo { 

public static void main(String[] args) { 
    Scanner key = new Scanner(System.in); 
    LinkedList list = new LinkedList(); 
    menu(); 
    int input = key.nextInt(); 

     if (input == 1){ 
      if (input ==1){ 
       System.out.println("Enter name"); 
       String name = key.nextLine(); 
       list.add(name); 

       System.out.println(list); 
      } 
     } 



} 

public static void menu(){ 
    System.out.println("Student Maintenence System:"); 
    System.out.println("1. Add Student"); 
    System.out.println("2. Remove Student"); 
    System.out.println("3. Print Student Information"); 
    System.out.println("4. Exit"); 
} 

public static void optionOne(int input, LinkedList list, Scanner key){ 
    if (input ==1){ 
     System.out.println("Enter name");/*supposed to ask for a lot more but for the time being its only asking this*/ 
     String name = key.nextLine(); 
     list.add(name); 
     list.toString(); 
} 
} 
} 
+0

請自己做你的任務,先自己嘗試一下,如果你遇到任何問題,請在這裏提問。 – Omore

回答

0

從我的理解,似乎你不關心鏈表中學生的順序。因此,你可以有添加,如方法:

// This is the start of your linked list 
Node start = ...; 

public void add(String s){ 
    // This may change depending on how you construct a node 
    Node toAdd = new Node(s); 
    // Iterate until we reach the end 
    while(start.getNextNode() != null) 
     start = start.getNextNode(); 
    start.setNextNode(toAdd); 

} 

雖然你可能沒有一些我在上面添加的方法,你會更好實現它們。

對於刪除,你可以使用類似:

// This is the start of your linked list 
Node start = ...; 

public void remove(String s){ 
    // Iterate until we find the node who's next one is the one to delete 
    // We're assuming toString() returns the name of the student 
    while(start.getNextNode().toString() != s) 
     start = start.getNextNode(); 
    // Set our next node to the one 2 nodes down the line, thus skipping it 
    start.setNextNode(start.getNextNode().getNextNode()) 

} 

現在你問一個toString方法是什麼,基本上是一個返回提供有關對象信息的字符串toString()方法。 爲學生類的可能toString()方法可以

public String toString(){ 
    return "Hello! My name is " + fname + .....; // so on and so on 
} 

注意對象在Java有一個toString方法,所以寫自己將覆蓋其超類的方法。如果你沒有使用toString()擴展一個類,那麼它會一直回到它能夠找到的任何字符串,其中最遠的是它在存儲器存儲中的位置的十六進制代碼(我可能對此存在錯誤所以糾正我,如果不是)

希望這可以幫助!