---
# System prepended metadata

title: 資料結構 20220926_鏈結串列程式碼
tags: [java, Data Structure]

---

# 資料結構 20220926_鏈結串列程式碼

>撰寫人[name=AmiYaku1049] [首頁--天空路1049號](https://skys-kid-lai.github.io/1004/)
>>最後編輯[time=Mon, Nov 26, 2022 11:25 AM]
___
## 題目敘述
- 學校學生本學期有五門必修課，為同學們建立功課表的串列，該串列可輸入
1.課程名稱 (String)
2.上課時間 (Int)
3.教室代碼 (Int)

- 再輸出本學期所有必修課
## 程式碼
```java=
import java.io.*;
class ClassWork_0926_1110832059 
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader buf;
		buf=new BufferedReader(new InputStreamReader(System.in));
		String DataName;
		String time;
		String Classroom;

		System.out.println("請輸入3組課程資料：");
		LinkedList list=new LinkedList();
		for(int i=0; i<3; i++)
		{
			System.out.print("Insert ClassName: ");
			DataName=buf.readLine();
			System.out.print("Insert ClassTime: ");
			time=buf.readLine();
			System.out.print("Insert ClassRoom: ");
			Classroom=buf.readLine();

			list.insert(DataName,time,Classroom);
			System.out.println("--------------------");
		}
        System.out.println("Class Data: ");
		System.out.println("ClassName|ClassTime|ClassRoom =========================");
		list.print();
	}
}
class Node
{
	String DataName;
	String time;
	String Classroom;
	Node next;
	public Node(String DataName,String time,String Classroom){
		this.time=time;
		this.Classroom=Classroom;
		this.DataName=DataName;
		this.next=null;
	}
}
class LinkedList
{
	public Node first;
	public Node last;
	public boolean isEmpty()
	{
		return first==null;
	}
	public void print()
	{
		Node current=first;
		while(current!=null)
		{
			System.out.println("["+current.DataName+" "+current.time+" "+current.Classroom+"]");
			current=current.next;
		}
		System.out.println();
	}
	public void insert(String DataName,String time,String Classroom){
		Node newNode=new Node(DataName,time,Classroom);
		if(this.isEmpty())
		{
			first=newNode;
			last=newNode;
		}
		else
		{
			last.next=newNode;
			last=newNode;
		}
	}
}
```

## 錯誤資訊
原本在41行：
```java
public class LinkedList
{
	public Node first;
	public Node last;
	public boolean isEmpty()
	{
```
* 修改原因：一個程式碼，無法出現兩個擁有public的class
* 修改方式：刪除主程式外class的public

## 用書資訊：
>書名：圖解資料結構 使用java（第三版）
>作者：吳燦銘、胡昭民
>出版社：博碩文化
>出版年份：2018年05月