Java Link List Implementation

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

Stay updated

Receive insights on tech, leadership, and growth.

Subscribe if you want to read posts like this

No spam. One email a month.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.