Linked Lists from C to Java

C Pointer Concepts in Java」一文提到 Java 沒有指標型別 (pointer type) ,但有參照型別 (reference type) 的設計。在遇到須要處理鏈結清單 (linked list)、圖形 (graph) 等資料結構時,Java 如何透過參照型別,仍能達成類似的效果。本文將以「鏈結清單」為例,分別用 C 與 Java 實作,說明兩者的差異與對應。


🔹 C 語言:使用指標建立鏈結清單

/*
   1 -> 2 -> 3 NULL
*/
#include <stdio.h>
#include <stdlib.h>

// 定義節點結構
typedef struct Node {
    int data;  // 資料
    struct Node* next;  // 下一節點指標
} Node;

int main() {
    // 建立三個節點
    Node* head = (Node*)malloc(sizeof(Node));
    Node* second = (Node*)malloc(sizeof(Node));
    Node* third = (Node*)malloc(sizeof(Node));

    // 給值與連結
    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

    // 印出鏈結清單
    Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");

    return 0;
}
    其中,
  • Node* 是指標變數,指向記憶體中的節點。
  • 使用 malloc 配置記憶體,並用 -> 操作指標指向的內容。
  • 每個節點透過 next 指向下一個節點,形成鏈結。

🔹 Java 語言:使用參照建立鏈結清單

/*
   1 -> 2 -> 3 null
*/

// 定義節點結構
class Node {
    int data; // 資料
    Node next;  // 下一節點參照

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class LinkedListDemo {
    public static void main(String[] args) {
        // 建立三個節點
        Node head = new Node(1);
        Node second = new Node(2);
        Node third = new Node(3);

        // 給值與連結
        head.next = second;
        second.next = third;

        // 印出鏈結清單
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " -> ");
            current = current.next;
        }
        System.out.println("null");
    }
}
    其中,
  • Node 是一個類別,變數如 headsecond 是參照變數,指向物件。
  • 使用 new 建立物件,並透過 . 操作物件的屬性。
  • 雖然語法上沒有 *&,但物件的參照本質上就像 C 的指標。

🔄 對照總結:C 指標 vs Java 參照

概念C 語言Java 語言
記憶體操作明確使用 *&隱含於物件參照
記憶體配置mallocnew
節點連結指標指向下一節點物件參照指向下一節點
安全性容易出現記憶體錯誤自動垃圾回收記憶體管理 (GC)

以上說明 Java 雖然沒有指標型別,但仍能透過「參照型別」的設計,實現如鏈結清單這類需要動態記憶體與節點連結的資料結構。希望對於從 C 過渡到 Java 的學習者有幫助。

Installing Code::Blocks on Windows for APCS Preparation

APCS 為大學程式設計先修檢測 (Advanced Placement Computer Science),其檢測等級可供大學個人申請入學之用。各電腦相關科系APCS組都明訂要求的檢測等級,須符合其門檻才具備申請資格。其中,針對 C 及 C++ 程式檢測,考場提供 Linux 環境的輕量級 Code::Blocks 整合開發環境 (IDE) 供程式撰寫及執行之用。

考生若想在家裏 Windows 作業系統上熟悉 Code::Blocks,一種方法是下載考場 Linux 環境 .iso 檔,安裝 VirtualBox 虛擬機軟體,將 Linux 虛擬機跑起來,使用其桌面安裝好的Code::Blocks。另一種方法則是直接在 Windows 自行安裝 Code::Blocks。

本文將介紹後者,如何在 Windows 系統上安裝 Code::Blocks,並參考考場 Linux 虛擬機中的 Code::Blocks 進行設定,以獲得和考場 Linux 環境一樣的 Code::Blocks 寫程式體驗。


📥 Windows安裝 Code::Blocks

 🆚不推薦新版本:Code::Blocks 25.03 (含 MinGW 編譯器)

  • 下載連結:https://www.codeblocks.org/downloads/binaries/
    •   Binary releases
    •     Windows XP / Vista / 7 / 8.x /10
    •       Microsoft Windows (64 bit, default)
    • codeblocks-25.03mingw-setup.exe
  • 適用系統:Windows XP / Vista / 7 / 8.x / 10(64 位元)

     本版本內建 MinGW 編譯器,安裝後即可開始編寫與編譯 C/C++ 程式。但因為安裝時易受某些防毒軟體拒絕,若心有疑慮,不想關閉防毒軟體,建議不要安裝。

 ✅ 推薦舊版:Code::Blocks 20.03  (含 MinGW 編譯器)

      本版本也內建 MinGW 編譯器,安裝後即可開始編寫與編譯 C/C++ 程式。雖然是舊版,但目前為考場安裝版本,且不會受防毒軟體拒絕,故建議安裝。

⚙️ 設定編譯器支援 C++17 / C17

以下依據考場 Linux 虛擬機安裝組態,設定Windows Code::Blocks 20.03 的編譯器選項:

1️⃣ 開啟 Code::Blocks,進入:

Settings → Compiler...

2️⃣ 在「Global compiler settings」中,確認選擇的編譯器為:

Selected compiler: GNU GCC Compiler (default)

3️⃣ 點選「Compiler settings」→「Compiler Flags」,勾選以下選項,點選OK:

  • ✅  Have g++ follow the C++17 ISO C++ language standard [-std=c++17]
  • Have gcc follow the 2017 ISO C language standard [-std=c17]
  • Static linking [-static]
註: 以下為考場提供 .iso 檔安裝的 Linux Code::Blocks 20.03 組態
  • Have gcc follow the 2017 ISO C language standard [-std=c17]
  • Have gcc follow the C++17 ISO C++ language standard [-std=c++17]
  • Static linking [-static]


Linked Lists from C to Java

「 C Pointer Concepts in Java 」一文提到 Java 沒有指標型別 (pointer type) ,但有參照型別 (reference type) 的設計。在遇到須要處理鏈結清單 (linked list)、圖形 (graph) 等資料結構時,Java ...

總網頁瀏覽量