البرمجة

تعلم تنفيذ LinkedList في Java

Linked lists are fundamental data structures in programming. They consist of nodes, where each node contains a data element and a reference (or link) to the next node in the sequence. In Java, you can implement a basic singly linked list with the following functionalities: traversal, insertion, and deletion. Here’s a simple implementation to get you started:

java
class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; public LinkedList() { this.head = null; } // Insertion at the end of the linked list public void insert(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } // Traversal public void traverse() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); } // Deletion by value public void delete(int value) { if (head == null) { return; } if (head.data == value) { head = head.next; return; } Node current = head; while (current.next != null && current.next.data != value) { current = current.next; } if (current.next != null) { current.next = current.next.next; } } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); // Insert some elements list.insert(1); list.insert(2); list.insert(3); // Traverse the list System.out.println("Linked List:"); list.traverse(); // Delete an element list.delete(2); // Traverse again after deletion System.out.println("Linked List after deletion:"); list.traverse(); } }

This is a basic implementation to get you started. You can further enhance it by adding error handling, more insertion methods (e.g., at the beginning or at a specific position), and other operations.

المزيد من المعلومات

بالطبع! يُعتبر الرابط المفتوح (Linked List) من أبسط وأساسيات هياكل البيانات، ويُستخدم بشكل واسع في البرمجة. يتكون الرابط المفتوح من عناصر تُعرف بالعُقد (Nodes)، وتتكون كل عُقدة من قيمة (Data) ورابط (Link) إلى العُقدة التالية في الرابط المفتوح.

الفائدة الرئيسية للرابط المفتوح هي قدرته على تغيير حجمه بسهولة، حيث يمكن إضافة وحذف العُقد بسرعة، بالإضافة إلى قدرته على التعامل مع البيانات بشكل ديناميكي.

عند إنشاء رابط مفتوح في جافا، يتم تعريف العُقدة (Node) التي تحتوي على البيانات ورابط إلى العُقدة التالية. يتم إنشاء فئة خاصة لرابط المفتوح تحتوي عادة على رأس (Head) للرابط المفتوح كنقطة البداية.

تُستخدم عملية الإدراج لإضافة عُقدة جديدة إلى نهاية الرابط المفتوح، حيث يتم التنقل من رأس الرابط المفتوح إلى نهاية الرابط المفتوح حتى يتم الوصول إلى العُقدة الأخيرة، ثم يتم إنشاء عُقدة جديدة وربطها بالعُقدة الأخيرة.

عملية الحذف تتطلب البحث عن العُقدة التي تحتوي على القيمة المطلوبة، ثم تعديل الرابط لتجاوز العُقدة المحذوفة والربط بالعُقدة التالية.

عملية العرض (Traversal) تتطلب البدء من رأس الرابط المفتوح والتنقل عبر العُقد حتى نهاية الرابط المفتوح، ويتم عرض قيم العُقد أثناء التنقل.

زر الذهاب إلى الأعلى