編程實現鏈表:
要求:1.循環輸入學生學號和成績。並將學生信息加入到鏈表中;
2.鏈表中的信息按學生成績的高-->低進行排序;
3.如果輸入的學生信息中,學號重複,則僅更新學生成績,不添加新的節點。
import java.util.Scanner;
public class TestLink {
// 測試主函數
public static void main(String[] args) {
LinkList list = new LinkList();
Scanner sc = new Scanner(System.in);
String ask = "";
do {
System.out.print("請錄入學號:");
int no = sc.nextInt();
System.out.print("請錄入成績:");
int num = sc.nextInt();
Student s = new Student(no, num);
list.addAndSet(s);// 調用方法.將學生信息擦人鏈表
System.out.println("是否繼續Y/N");
ask = sc.next();
} while (ask.equals("Y") || ask.equals("y"));
System.out.println("排序前學生成績一覽:");
list.listAll();// 輸出學生信息
// 對學生成績進行排序.(按成績高-->低)
list.bubbleSort();
System.out.println("\n排序後學生成績一覽:");
list.listAll();// 輸出學生信息
}
}
/**
* 學生類
*/
class Student {
private int no;
private int num;
public Student(int no, int num) {
super();
this.no = no;
this.num = num;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
/**
* 鏈表結點類
*/
class Node {
private Student student;
private Node next; // 鏈表結點的指針域,指向直接後繼結點
public Node() {
next = null;
}
public Node(Student student, Node next) {
this.student = student;
this.next = next;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Node getNext() {
return this.next;
}
public void setNext(Node next) {
this.next = next;
}
}
/**
* 鏈表類
*/
class LinkList {
private Node head = null; // 頭結點指針
private int size = 0;
public LinkList() {
head = new Node();
size = 0;
}
public Node getHead() {
return this.head;
}
public void setHead(Node head) {
this.head = head;
}
public int getSize() {
return this.size;
}
public boolean isEmpty() {
return (size == 0);
}
// 插入/修改 學生對象
public boolean addAndSet(Student stu) {
Node node;
// 判斷鏈表是否爲空;如果爲空則在表頭插入
if (size == 0) {
// 定義一個新的節點,並將將新結點的指針指向鏈表的首結點
node = new Node(stu, this.head.getNext());
// 把節點插入到head後,設置新結點爲鏈表的首結點
this.head.setNext(node);
// 鏈表長度加1
size++;
return true;
}
// 當鏈表不爲空時候,判斷是否有重複的學號.有則替換.無則在結尾插入.
else {
Node n;
for (n = head; n.getNext() != null; n = n.getNext()) {
// 學號相同.更新學生成績
if (n.getNext().getStudent().getNo() == stu.getNo()) {
n.getNext().getStudent().setNum(stu.getNum());
return true;
}
}
// 如果學號不同,在鏈表結尾插入
Node temp = head;
while (null != temp.getNext()) {
temp = temp.getNext();
}
node = new Node(stu, temp.getNext());
temp.setNext(node);
size++;
return true;
}
}
// 控制檯輸出鏈表所有內容
public void listAll() {
for (Node curr = head.getNext(); curr != null; curr = curr.getNext()) {
System.out.print("\n學號: " + curr.getStudent().getNo() + "\t");
System.out.print("成績: " + curr.getStudent().getNum() + "\t");
}
System.out.println("");
}
// 鏈表冒泡排序方法.學生成績進行排序.(按成績高-->低)
public void bubbleSort() {
Node p, q;
Student temp;
for (p = head.getNext(); p.getNext() != null; p = p.getNext()) {
for (q = head.getNext(); q.getNext() != null; q = q.getNext()) {
if (q.getStudent().getNum() < q.getNext().getStudent().getNum()) {
temp = q.getStudent();
q.setStudent(q.getNext().getStudent());
q.getNext().setStudent(temp);
}
}
}
}
}
內容可以直接拉入IDE中.右鍵運行.