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

推荐订阅源

Engineering at Meta
Engineering at Meta
D
Docker
IT之家
IT之家
博客园_首页
罗磊的独立博客
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
美团技术团队
Y
Y Combinator Blog
博客园 - 聂微东
量子位
阮一峰的网络日志
阮一峰的网络日志
GbyAI
GbyAI
Microsoft Security Blog
Microsoft Security Blog
博客园 - Franky
Martin Fowler
Martin Fowler
Jina AI
Jina AI
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
月光博客
月光博客
G
Google Developers Blog
B
Blog
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿

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);
    }
}