SlideShare a Scribd company logo
1 of 4
Download to read offline
/*
‫نمير‬ ‫حيدر‬ ‫عمران‬
‫اﻷثنين‬ 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;
}
}
}

More Related Content

What's hot

โปรแกรมย่อยและฟังก์ชั่นมาตรฐาน
โปรแกรมย่อยและฟังก์ชั่นมาตรฐานโปรแกรมย่อยและฟังก์ชั่นมาตรฐาน
โปรแกรมย่อยและฟังก์ชั่นมาตรฐานTipprapa Sungsinchai
 
Bài tập tuần 2
Bài tập tuần 2Bài tập tuần 2
Bài tập tuần 2Cong Nguyen
 
Bai tap tham khao CSPE
Bai tap tham khao CSPEBai tap tham khao CSPE
Bai tap tham khao CSPEmrcoffee282
 
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Loïc Knuchel
 
Prof.js
Prof.jsProf.js
Prof.jsuupaa
 
Void numero
Void numeroVoid numero
Void numeroOiga Lin
 
Java весна 2013 лекция 7
Java весна 2013 лекция 7Java весна 2013 лекция 7
Java весна 2013 лекция 7Technopark
 
An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]Eleanor McHugh
 
matrix operation using operator overloading
matrix operation using operator overloadingmatrix operation using operator overloading
matrix operation using operator overloadingmaria azam
 
Ejercicio8
Ejercicio8Ejercicio8
Ejercicio8jbersosa
 
Effective java 摘選條目分享 2 - 泛型
Effective java   摘選條目分享 2 - 泛型Effective java   摘選條目分享 2 - 泛型
Effective java 摘選條目分享 2 - 泛型Kane Shih
 

What's hot (16)

โปรแกรมย่อยและฟังก์ชั่นมาตรฐาน
โปรแกรมย่อยและฟังก์ชั่นมาตรฐานโปรแกรมย่อยและฟังก์ชั่นมาตรฐาน
โปรแกรมย่อยและฟังก์ชั่นมาตรฐาน
 
Bài tập tuần 2
Bài tập tuần 2Bài tập tuần 2
Bài tập tuần 2
 
Bai tap tham khao CSPE
Bai tap tham khao CSPEBai tap tham khao CSPE
Bai tap tham khao CSPE
 
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
 
Prof.js
Prof.jsProf.js
Prof.js
 
Void numero
Void numeroVoid numero
Void numero
 
Java весна 2013 лекция 7
Java весна 2013 лекция 7Java весна 2013 лекция 7
Java весна 2013 лекция 7
 
An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]
 
matrix operation using operator overloading
matrix operation using operator overloadingmatrix operation using operator overloading
matrix operation using operator overloading
 
1- Sourcecode Array
1- Sourcecode Array1- Sourcecode Array
1- Sourcecode Array
 
Tugas pw [10]
Tugas pw [10]Tugas pw [10]
Tugas pw [10]
 
Tugas pw [10]
Tugas pw [10]Tugas pw [10]
Tugas pw [10]
 
Ejercicio8
Ejercicio8Ejercicio8
Ejercicio8
 
Simulacion - Algoritmo congruencial cuadratico
Simulacion - Algoritmo congruencial cuadraticoSimulacion - Algoritmo congruencial cuadratico
Simulacion - Algoritmo congruencial cuadratico
 
Tugas pak putu
Tugas pak putuTugas pak putu
Tugas pak putu
 
Effective java 摘選條目分享 2 - 泛型
Effective java   摘選條目分享 2 - 泛型Effective java   摘選條目分享 2 - 泛型
Effective java 摘選條目分享 2 - 泛型
 

More from Dr. Ahmed J. Obaid

More from Dr. Ahmed J. Obaid (7)

Os lecture 7
Os lecture 7Os lecture 7
Os lecture 7
 
Os lecture 6
Os lecture 6Os lecture 6
Os lecture 6
 
Os lecture 5
Os lecture 5Os lecture 5
Os lecture 5
 
Operating System Lecture 4
Operating System Lecture 4Operating System Lecture 4
Operating System Lecture 4
 
Operating System Lecture 3
Operating System Lecture 3Operating System Lecture 3
Operating System Lecture 3
 
Operating System Lecture 2
Operating System Lecture 2Operating System Lecture 2
Operating System Lecture 2
 
Operating System Lecture 1
Operating System Lecture 1Operating System Lecture 1
Operating System Lecture 1
 

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(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);
  • 4. node<E> x; x = first; for (int i = 0; i < index; i++) { x = x.next; } return x; } } }