---
# System prepended metadata

title: 資料結構 20221003_鏈結串列程式碼_新增修改刪除查詢
tags: [java, Data Structure]

---

# 資料結構 20221003_鏈結串列程式碼_新增修改刪除查詢

>撰寫人[name=AmiYaku1049] [首頁--天空路1049號](https://skys-kid-lai.github.io/1004/)
>>最後編輯[time=Wed, Jan 18, 2023 7:53 PM]
___
## 題目敘述
- 每筆資料可存入
1.座號
2.姓名
3.成績

- 電腦提供使用者選擇所需功能
1.新增一筆資料 
2.刪除某筆資料(使用者輸入座號) 
3.查詢某筆資料(使用者輸入座號) 
4.查詢全部資料

## 程式碼
```java=
import java.io.*;
import java.util.Scanner;
class ClassWork_1110832059_1003 
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader buf;
		buf=new BufferedReader(new InputStreamReader(System.in));
		int DataNumber;
		String name;
		String grade;
		int findword;
		
		LinkedList list=new LinkedList();
		String input;
        while(1==1) 
		{
			System.out.print("Please insert the funtion you want to do: ");
            Scanner in = new Scanner(System.in);
            input = in.next();
            switch(input) {
                //輸入n新增資料
                case "n":
                    System.out.print("No: ");
                    DataNumber=Integer.parseInt(buf.readLine());
                    System.out.print("Name: ");
                    name=buf.readLine();
                    System.out.print("Score: ");
                    grade=buf.readLine();
                    list.insert(DataNumber,name,grade);
                    System.out.println("-------------");
                    break;
               //輸入d刪除該資料
                case "d":
                    System.out.print("Insert the number need to delete: ");
                    findword=Integer.parseInt(buf.readLine());
               
                    Node current=new Node(list.first.DataNumber,list.first.name,list.first.grade);
                    current.next=list.first.next;
                    while(current.DataNumber!=findword) 
						current=current.next;
                    list.delete(current);
                   
                    break;
               //輸入l輸出現在有的全部資料
                case "l":
                    System.out.println("=============");
                    System.out.println("Score List");
                    list.print();
                   
                    break;
				//離開程式
				case "b":		//輸入b則結束程式
					System.exit(1);
                //查詢該筆資料
				case "i":
					System.out.print("Insert the number need to find: ");
                    findword=Integer.parseInt(buf.readLine());
               
                    Node current2=new Node(list.first.DataNumber,list.first.name,list.first.grade);
                    current2.next=list.first.next;
                    while(current2.DataNumber!=findword) 
						current2=current2.next;
					list.contains(current2);
                   
                    break;
            }
           
        }
	}
}
class Node
{
	int DataNumber;
	String name;
	String grade;
	Node next;
	public Node(int DataNumber,String name,String grade){
		this.name=name;
		this.grade=grade;
		this.DataNumber=DataNumber;
		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.DataNumber+"             "+current.name+"             "+current.grade+"]");
			current=current.next;
		}
		System.out.println();
	}
	public void insert(int DataNumber,String name,String grade){
		Node newNode=new Node(DataNumber,name,grade);
		if(this.isEmpty())
		{
			first=newNode;
			last=newNode;
		}
		else
		{
			last.next=newNode;
			last=newNode;
		}
	}
	public void delete(Node delNode){
		Node newNode;
		Node tmp;
		if(first.DataNumber==delNode.DataNumber)
		{
			first=first.next;
		}
		else if(last.DataNumber==delNode.DataNumber)
		{
			System.out.println("["+delNode.DataNumber+"             "+delNode.name+"             "+delNode.grade+"]");
			newNode=first;
			while(newNode.next!=last)
				newNode=newNode.next;
			newNode.next=last.next;
			last=newNode;
		}
		else
		{
			newNode=first;
			tmp=first;
			while(newNode.DataNumber!=delNode.DataNumber)
			{
				tmp=newNode;
				newNode=newNode.next;
			}
			tmp.next=delNode.next;
		}
	}
	public void contains(Node conNode){
		if(conNode.DataNumber==conNode.DataNumber)
		{
			System.out.println("["+conNode.DataNumber+"             "+conNode.name+"             "+conNode.grade+"]");
		}
		
	}
}


```

## 結果圖示
![](https://i.imgur.com/k0Apbnr.png =50%x)

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