/*
‫نمير‬ ‫حيدر‬ ‫عمران‬
‫اﻷثنين‬ 27 ‫أبريل‬
*/
import java.util.NoSuchElementException;
public class test1 {
public static void main(String[] args) {
//‫مثال‬ ‫عﻠﻰ‬ ‫متغير‬ ‫من‬ ‫نوع‬ linkedList ‫يبين‬ ‫عمﻠية‬ ‫اﻷنشاء‬ ‫و‬
//‫اﻷضافة‬ ‫و‬ ‫الحذف‬ ‫و‬ ‫عمﻠية‬ ‫عرض‬ ‫العناصر‬ ‫و‬ ‫طباعتها‬
linkedList<String> linkedList = new linkedList<>();
//‫اﻷضافة‬
linkedList.add("1");
linkedList.add(null);
linkedList.add("3");
linkedList.add("4");
linkedList.add(0,"deleted element");
//‫الحذف‬
linkedList.delete(4);
//‫أستدعاء‬ ‫العناصر‬ ‫و‬ ‫طباعتها‬
for (int i = 0; i < linkedList.size; i++) {
System.out.println(linkedList.get(i));
}
}
/**
* class node
* ‫هذا‬ ‫الكﻼس‬ ‫يمث‬‫ل‬ ‫ال‬(node) ‫المستخدمة‬ ‫فﻲ‬ ‫خوارزمية‬ ‫ال‬(inkedList)
* ‫وتحتوي‬ ‫عﻠﻰ‬ ‫متغيرين‬ ‫ﻛما‬ ‫يﻠﻲ‬
* data --> (node)‫تحمل‬ ‫بيانات‬ ‫ال‬
* next --> ‫القادم‬ (node)‫تﺆﺷر‬ ‫عﻠﻰ‬ ‫ال‬
* @param <E> ‫نوع‬ ‫البيانات‬ ‫المستخدم‬ ‫خﻼل‬ ‫أستدعاء‬ ‫ال‬(class) ‫مثل‬ int ‫او‬ String <--
*/
public static class node<E> {
E data;
node<E> next;
//constructor --> ‫يستخدم‬ ‫لتغيير‬ ‫و‬ ‫تحديد‬ ‫قيم‬ ‫المتغيرات‬ ‫داخﻠه‬
public node(E data, node<E> next) {
this.data = data;
this.next = next;
}
}
/**
* class linkedList
* ‫الخوارزمية‬ ‫الكامﻠة‬ ‫ﻻنشاء‬ ‫ال‬(linkedList) ‫و‬ ‫ادارتها‬ ‫و‬ ‫تنفيذ‬ ‫العمﻠيات‬ ‫الشائعة‬ ‫عﻠيها‬
* ‫و‬ ‫ما‬ ‫يﻠﻲ‬ ‫مﺠموعة‬ ‫من‬ ‫العمﻠيات‬ ‫المطبقة‬ ‫عﻠﻰ‬ ‫ال‬(linkedList) ‫فﻲ‬ ‫هذه‬ ‫الخوارزمية‬
*
* get(int index) --> (index)‫المختار‬ ‫حسﺐ‬ ‫ا‬‫ل‬ (node)‫ترﺟﻊ‬ ‫قيمة‬ ‫البيانات‬ ‫الموﺟودة‬ ‫داخل‬ ‫ال‬
* getFirst() --> (linkedList)‫فﻲ‬ ‫ال‬ (node) ‫ترﺟﻊ‬ ‫بيانات‬ ‫أول‬
* gentlest() --> (linkedList)‫فﻲ‬ ‫ال‬ (node) ‫ترﺟﻊ‬ ‫بيانات‬ ‫أخر‬
* ---------------------------------------------------------------------
* add(E val) --> ‫ﺟديد‬ (node) ‫أضافة‬
* add(int index, E val) --> ‫ﺟديد‬ ‫فﻲ‬ ‫أي‬ ‫مكان‬ ‫محدد‬ (node) ‫أضافة‬
* addLast(E val) --> ‫ﺟديد‬ ‫فﻲ‬ ‫النهاية‬ (node) ‫أضافة‬
* addFirst(E val) --> ‫ﺟديد‬ ‫فﻲ‬ ‫البداية‬ (node) ‫أضافة‬
* ---------------------------------------------------------------------
* deleteFirst() --> ‫مسﺢ‬ ‫اول‬ ‫عنﺼر‬
* deleteLast() --> ‫مسﺢ‬ ‫أخر‬ ‫عنﺼر‬
* delete(int index) --> ‫مسﺢ‬ ‫من‬ ‫أي‬ ‫مكان‬
* ---------------------------------------------------------------------
* indexChecker(int index) --> ‫لمعرفة‬ ‫أذا‬ ‫ﻛان‬ ‫صالﺢ‬ ‫لﻸستخدام‬ ‫أم‬ ‫ﻻ‬(‫مستخدمة‬ ‫فﻲ‬ ‫أﻛثر‬ ‫من‬ ‫موضﻊ‬ ‫فﻲ‬
‫الخوارزمية‬ ‫مثل‬ ‫اﻷضافة‬ ‫و‬ ‫الحذف‬) (index)‫تختبر‬ ‫ال‬
* node(int index) --> (index)‫حسﺐ‬ ‫قيمة‬ ‫ال‬ (node) ‫تعيد‬ ‫أي‬
*
* ‫المتغيرات‬ ‫الرئيسية‬ ‫فﻲ‬ ‫الخوارزمية‬
* first --> (linkedList)‫محدد‬ ‫يستخدم‬ ‫لتحديد‬ ‫أول‬ ‫عنﺼر‬ ‫فﻲ‬ ‫ال‬
* last --> (linkedList)‫محدد‬ ‫يستخدم‬ ‫لتحديد‬ ‫أخر‬ ‫عنﺼر‬ ‫فﻲ‬ ‫ال‬
* size --> (linkedList)‫عدد‬ ‫عناصر‬ ‫ال‬
* @param <E> ‫نوع‬ ‫البيانات‬ ‫المستخدم‬ ‫خﻼل‬ ‫أستدعاء‬ ‫ال‬(class) ‫مثل‬ int ‫او‬ String <--
*/
public static class linkedList<E> {
transient node<E> first;
transient node<E> last;
int size;
public E get(int index) {
node<E> n = node(index);
return n.data;
}
public E getFirst() {
node<E> f = first;
if (f == null) {
throw new NoSuchElementException();
}
return f.data;
}
public E getLast() {
node<E> l = last;
if (l == null) {
throw new NoSuchElementException();
}
return l.data;
}
public void add(E val) {
addLast(val);
}
public void add(int index, E val) {
if (index == size) {
addLast(val);
} else if (index == 0) {
addFirst(val);
} else {
indexChecker(index);
if (first == null && last == null) {
first = new node<>(val, null);
} else if (first != null && last == null) {
last = new node<>(val, null);
first.next = last;
} else if (first != null && last != null) {
node<E> node = node(index);
node<E> prevNode = node(index - 1);
node<E> newNode = new node<>(val, node);
prevNode.next = newNode;
}
size++;
}
}
public void addLast(E val) {
if (first == null && last == null) {
first = new node<>(val,null);
} else if (first != null && last == null) {
last = new node<>(val, null);
first.next = last;
} else if (first != null && last != null) {
node<E> temp = last;
last = new node<>(val, null);
temp.next = last;
}
size++;
}
public void addFirst(E val) {
if (first == null && last == null) {
first = new node<>(val,null);
} else if (first != null && last == null) {
last = first;
first = new node<>(val, last);
} else if (first != null && last != null) {
node<E> temp = first;
first = new node<>(val, temp);
}
size++;
}
public void deleteFirst() {
node<E> temp = first;
first = first.next;
temp = null;
size--;
}
public void deleteLast() {
node<E> prev = node(size - 2);
prev.next = null;
size--;
}
public void delete(int index) {
if (index == 0) {
deleteFirst();
} else if (index == size - 1) {
deleteLast();
} else {
node<E> node = node(index);
node<E> prevNode = node(index - 1);
prevNode.next = node.next;
size--;
}
}
private void indexChecker(int index) {
if (!(index >= 0 && index < size)) {
throw new IndexOutOfBoundsException("at index of: " + index);
}
}
private node<E> node(int index) {
indexChecker(index);
node<E> x;
x = first;
for (int i = 0; i < index; i++) {
x = x.next;
}
return x;
}
}
}

Linked list proj

  • 1.
    /* ‫نمير‬ ‫حيدر‬ ‫عمران‬ ‫اﻷثنين‬27 ‫أبريل‬ */ import java.util.NoSuchElementException; public class test1 { public static void main(String[] args) { //‫مثال‬ ‫عﻠﻰ‬ ‫متغير‬ ‫من‬ ‫نوع‬ linkedList ‫يبين‬ ‫عمﻠية‬ ‫اﻷنشاء‬ ‫و‬ //‫اﻷضافة‬ ‫و‬ ‫الحذف‬ ‫و‬ ‫عمﻠية‬ ‫عرض‬ ‫العناصر‬ ‫و‬ ‫طباعتها‬ linkedList<String> linkedList = new linkedList<>(); //‫اﻷضافة‬ linkedList.add("1"); linkedList.add(null); linkedList.add("3"); linkedList.add("4"); linkedList.add(0,"deleted element"); //‫الحذف‬ linkedList.delete(4); //‫أستدعاء‬ ‫العناصر‬ ‫و‬ ‫طباعتها‬ for (int i = 0; i < linkedList.size; i++) { System.out.println(linkedList.get(i)); } } /** * class node * ‫هذا‬ ‫الكﻼس‬ ‫يمث‬‫ل‬ ‫ال‬(node) ‫المستخدمة‬ ‫فﻲ‬ ‫خوارزمية‬ ‫ال‬(inkedList) * ‫وتحتوي‬ ‫عﻠﻰ‬ ‫متغيرين‬ ‫ﻛما‬ ‫يﻠﻲ‬ * data --> (node)‫تحمل‬ ‫بيانات‬ ‫ال‬ * next --> ‫القادم‬ (node)‫تﺆﺷر‬ ‫عﻠﻰ‬ ‫ال‬ * @param <E> ‫نوع‬ ‫البيانات‬ ‫المستخدم‬ ‫خﻼل‬ ‫أستدعاء‬ ‫ال‬(class) ‫مثل‬ int ‫او‬ String <-- */ public static class node<E> { E data; node<E> next; //constructor --> ‫يستخدم‬ ‫لتغيير‬ ‫و‬ ‫تحديد‬ ‫قيم‬ ‫المتغيرات‬ ‫داخﻠه‬ public node(E data, node<E> next) { this.data = data; this.next = next; } } /** * class linkedList * ‫الخوارزمية‬ ‫الكامﻠة‬ ‫ﻻنشاء‬ ‫ال‬(linkedList) ‫و‬ ‫ادارتها‬ ‫و‬ ‫تنفيذ‬ ‫العمﻠيات‬ ‫الشائعة‬ ‫عﻠيها‬ * ‫و‬ ‫ما‬ ‫يﻠﻲ‬ ‫مﺠموعة‬ ‫من‬ ‫العمﻠيات‬ ‫المطبقة‬ ‫عﻠﻰ‬ ‫ال‬(linkedList) ‫فﻲ‬ ‫هذه‬ ‫الخوارزمية‬ * * get(int index) --> (index)‫المختار‬ ‫حسﺐ‬ ‫ا‬‫ل‬ (node)‫ترﺟﻊ‬ ‫قيمة‬ ‫البيانات‬ ‫الموﺟودة‬ ‫داخل‬ ‫ال‬ * getFirst() --> (linkedList)‫فﻲ‬ ‫ال‬ (node) ‫ترﺟﻊ‬ ‫بيانات‬ ‫أول‬ * gentlest() --> (linkedList)‫فﻲ‬ ‫ال‬ (node) ‫ترﺟﻊ‬ ‫بيانات‬ ‫أخر‬ * --------------------------------------------------------------------- * add(E val) --> ‫ﺟديد‬ (node) ‫أضافة‬ * add(int index, E val) --> ‫ﺟديد‬ ‫فﻲ‬ ‫أي‬ ‫مكان‬ ‫محدد‬ (node) ‫أضافة‬ * addLast(E val) --> ‫ﺟديد‬ ‫فﻲ‬ ‫النهاية‬ (node) ‫أضافة‬ * addFirst(E val) --> ‫ﺟديد‬ ‫فﻲ‬ ‫البداية‬ (node) ‫أضافة‬ * --------------------------------------------------------------------- * deleteFirst() --> ‫مسﺢ‬ ‫اول‬ ‫عنﺼر‬ * deleteLast() --> ‫مسﺢ‬ ‫أخر‬ ‫عنﺼر‬ * delete(int index) --> ‫مسﺢ‬ ‫من‬ ‫أي‬ ‫مكان‬ * ---------------------------------------------------------------------
  • 2.
    * indexChecker(int index)--> ‫لمعرفة‬ ‫أذا‬ ‫ﻛان‬ ‫صالﺢ‬ ‫لﻸستخدام‬ ‫أم‬ ‫ﻻ‬(‫مستخدمة‬ ‫فﻲ‬ ‫أﻛثر‬ ‫من‬ ‫موضﻊ‬ ‫فﻲ‬ ‫الخوارزمية‬ ‫مثل‬ ‫اﻷضافة‬ ‫و‬ ‫الحذف‬) (index)‫تختبر‬ ‫ال‬ * node(int index) --> (index)‫حسﺐ‬ ‫قيمة‬ ‫ال‬ (node) ‫تعيد‬ ‫أي‬ * * ‫المتغيرات‬ ‫الرئيسية‬ ‫فﻲ‬ ‫الخوارزمية‬ * first --> (linkedList)‫محدد‬ ‫يستخدم‬ ‫لتحديد‬ ‫أول‬ ‫عنﺼر‬ ‫فﻲ‬ ‫ال‬ * last --> (linkedList)‫محدد‬ ‫يستخدم‬ ‫لتحديد‬ ‫أخر‬ ‫عنﺼر‬ ‫فﻲ‬ ‫ال‬ * size --> (linkedList)‫عدد‬ ‫عناصر‬ ‫ال‬ * @param <E> ‫نوع‬ ‫البيانات‬ ‫المستخدم‬ ‫خﻼل‬ ‫أستدعاء‬ ‫ال‬(class) ‫مثل‬ int ‫او‬ String <-- */ public static class linkedList<E> { transient node<E> first; transient node<E> last; int size; public E get(int index) { node<E> n = node(index); return n.data; } public E getFirst() { node<E> f = first; if (f == null) { throw new NoSuchElementException(); } return f.data; } public E getLast() { node<E> l = last; if (l == null) { throw new NoSuchElementException(); } return l.data; } public void add(E val) { addLast(val); } public void add(int index, E val) { if (index == size) { addLast(val); } else if (index == 0) { addFirst(val); } else { indexChecker(index); if (first == null && last == null) { first = new node<>(val, null); } else if (first != null && last == null) { last = new node<>(val, null); first.next = last; } else if (first != null && last != null) { node<E> node = node(index); node<E> prevNode = node(index - 1); node<E> newNode = new node<>(val, node); prevNode.next = newNode; } size++; } }
  • 3.
    public void addLast(Eval) { if (first == null && last == null) { first = new node<>(val,null); } else if (first != null && last == null) { last = new node<>(val, null); first.next = last; } else if (first != null && last != null) { node<E> temp = last; last = new node<>(val, null); temp.next = last; } size++; } public void addFirst(E val) { if (first == null && last == null) { first = new node<>(val,null); } else if (first != null && last == null) { last = first; first = new node<>(val, last); } else if (first != null && last != null) { node<E> temp = first; first = new node<>(val, temp); } size++; } public void deleteFirst() { node<E> temp = first; first = first.next; temp = null; size--; } public void deleteLast() { node<E> prev = node(size - 2); prev.next = null; size--; } public void delete(int index) { if (index == 0) { deleteFirst(); } else if (index == size - 1) { deleteLast(); } else { node<E> node = node(index); node<E> prevNode = node(index - 1); prevNode.next = node.next; size--; } } private void indexChecker(int index) { if (!(index >= 0 && index < size)) { throw new IndexOutOfBoundsException("at index of: " + index); } } private node<E> node(int index) { indexChecker(index);
  • 4.
    node<E> x; x =first; for (int i = 0; i < index; i++) { x = x.next; } return x; } } }