惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
B
Blog
月光博客
月光博客
博客园 - 【当耐特】
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Jina AI
Jina AI
博客园 - Franky
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Last Week in AI
Last Week in AI
B
Blog RSS Feed
H
Help Net Security

Yusuf Aytas

When Code Is Cheap, Does Quality Still Matter? Why Crouching Tiger, Hidden Dragon Is a Masterpiece Why We Ignore Advice The Mirror Is Part of the Machine When Too Many Maps Overlap on One Person The Work Runs on Different Maps Your Work Introduces You Trial By Fire The Dude Why Headcount Math Lies Capacity Is the Roadmap The Roadmap Is Not the System Torres del Paine W Trek Escaping Status Theater Incentives Drive Everything Scaling Culture Without Dilution What Good Looks Like Why Airport Security Feels Random Why Politics Appear How to Work with Me The Janus Protocol Multi-Horizon Delivery Framework What Good Execution Looks Like Managing Your Manager Why Kingdom of Heaven’s Director’s Cut Is Better AI Broke Interviews Most of What We Call Progress Managers Have Been Vibe Coding All Along Stop Wasting Brainpower Why Over-Engineering Happens
Java Link List Implementation
Yusuf Aytas · 2008-10-31 · via Yusuf Aytas

Published · 3 min read

Linked List (bağlı liste), verileri bellek üzerinde dinamik şekilde saklamamızı sağlayan temel veri yapılarından biridir. Dizilerde eleman sayısı sabitken, bağlı listeler ihtiyaç oldukça büyüyüp küçülebilir. Her eleman (Node), kendi verisini ve bir sonraki elemanın adresini tutar. Böylece ekleme, silme ve araya eleman yerleştirme gibi işlemler dizilere göre çok daha esnek yapılabilir.

Aşağıdaki örnek, Java’da basit bir tek yönlü bağlı liste (singly linked list) yapısının nasıl oluşturulacağını gösterir. Bu yapıda:

  • add() → Listenin sonuna eleman ekler
  • insert() → Belirtilen index’e eleman yerleştirir
  • delete() → İstenilen index’teki elemanı siler
  • toString() → Tüm listeyi metin olarak döndürür

Bu uygulama, bağlı listelerin çalışma mantığını kavramak için ideal bir temel örnektir.

public class LinkList {

    static Node head;  // Renamed 'list' to 'head' for clarity
    static int size = 0;

    // Inner class for Node
    static class Node {
        String data;  // Directly storing string data for simplicity
        Node next;

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

    // Method to add a new node
    public void add(String str) {
        Node node = new Node(str);
        if (head == null) {
            head = node;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = node;
        }
        size++;
    }

    // Method to delete a node at a given index
    public void delete(int index) {
        if (index >= size || index < 0) {
            throw new IndexOutOfBoundsException("Index out of bounds");
        }
        if (index == 0) {
            head = head.next;
            return;
        }
        Node current = head;
        for (int i = 0; i < index - 1; i++) {
            current = current.next;
        }
        current.next = current.next.next;
        size--;
    }

    // Method to insert a node at a given index
    public void insert(int index, String str) {
        if (index > size || index < 0) {
            throw new IndexOutOfBoundsException("Index out of bounds");
        }
        Node node = new Node(str);
        if (index == 0) {
            node.next = head;
            head = node;
        } else {
            Node current = head;
            for (int i = 0; i < index - 1; i++) {
                current = current.next;
            }
            node.next = current.next;
            current.next = node;
        }
        size++;
    }

    // Method to convert linked list to string
    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();
        Node current = head;
        while (current != null) {
            str.append(current.data).append("\t");
            current = current.next;
        }
        return str.toString();
    }

    public static void main(String[] args) {
        LinkList list = new LinkList();
        list.add("Hello");
        list.add("World");
        list.insert(1, "Java");
        list.delete(2);

        System.out.println(list);
    }
}