You are on page 1of 20

BY NsM [DATA STRUCTURES IN ARABIC]

Chapter Three

Linked lists data structure ‫هٌكل قوائم البٌانات المرتبطة‬


Static and Dynamic data structures ‫هُاكم انبُاَاث انثابخت وانذَُايُكُت‬

 A static data structure is an organization of collection of data in


memory that is fixed in size. This results in the maximum size
needing to be known in advance, as memory cannot be reallocated
at a later.

‫ َُخح عٍ هزا‬.‫هياكل البيانات الثابتة هٍ حُظُى نًدًىعه انبُاَاث فٍ انزاكشة انخٍ حى حكىٌ ثابخه فٍ انحدى‬
...‫ حُث ال ًَكٍ إعادة حخصُص انزاكشة فٍ ولج الحك‬، ‫انحذ األلصً نهحدى انزٌ َدب يعشفخه يسبمًا‬

Arrays are example of static data structure ‫المصفوفات هً مثال على هُاكم البٌانات الثابته‬

 A dynamic data structures , where in with the latter the size of the
structure can dynamically grow or shrink in size as needed.
‫ حُث فٍ انحدى األخُش ًَكٍ أٌ ًَُى حدى انهُكم أو َخمهص بشكم حُىٌ حسب‬، ‫هياكل البيانات الديناميكية‬
.‫انحاخت‬

Linked lists are example of dynamic data structure.

‫انمىائى انًشحبطت هٍ يثال عهً هٌاكل انبُاَاث انذَُايُكُت‬

What are different between linked lists and arrays?


‫يا انفشق بٍُ انمىائى انًشحبطت وانًصفىفاث ؟‬

1
‫‪BY NsM‬‬ ‫]‪[DATA STRUCTURES IN ARABIC‬‬

‫القوائم المرتبطه‬ ‫المصفوفات‬

‫ال حدى ثابج ؛ ًَُى عُصش واحذ فٍ ولج واحذ‬ ‫نذَها حدى ثابج يحذد يسبما‬

‫انعُاصش ًَكٍ اٌ حخضٌ فٍ أٌ يكاٌ‬ ‫َخى حخضٍَ عُاصش انًصفىفت بشكم‬


‫يخداوس‬

‫ال ًَكٍ انىصىل بسهىنت إنً انعُصش األول‪َ .‬هضو انمفض يٍ خالل خًُع‬ ‫سهىنت انىصىل إنً أٌ عُصش فٍ‬

‫انعُاصش انسابمه انخٍ حبذأ يٍ انبذاَت‬ ‫ولج ثابج‬

‫(‪Size=n×sizeof(element‬‬ ‫‪Size=n×sizeof(element(+‬‬
‫)‪n×sizeof(regerence‬‬

‫األدساج وانحزف بسُط وسشَع‬ ‫األدساج وانحزف فٍ يىلع يعٍُ يعمذ‬

‫َدب عهً كم عُصش حخضٍَ انعُصش وانًؤشش إنً انعُصش انخانٍ (راكشة‬ ‫فمط انعُاصش حخضٌ‬
‫إضافُت نًؤشش انخخضٍَ)‬

‫‪2‬‬
BY NsM [DATA STRUCTURES IN ARABIC]

What is linked list? ‫يا هٍ انمائًت انًشحبطت؟‬

A linked list is a collection of objects called nodes. Each node is linked to


its successor node in the list using a reference to the successor node.
‫ كم عمذة يشحبطت بانعمذة انخانُت فٍ انمائًت باسخخذاو‬.‫لائًت يشحبطت هٍ يدًىعت يٍ انكائُاث حسًً انعمذ‬
... ‫يشخع نهعمذة انخانُت‬

What is linked list element? ‫ما هو عنصر القائمة المرتبطة؟‬

The linked list is sequence of nodes arranged one after another, with each node
connected to the next by a "link" (like a chain) each node contain:

"‫ يع حىصُم كم عمذة باِخشة بىاسطت "سابط‬، ‫انمائًت انًشحبطت هٍ حسهسم انعمذ يشحبت واحذة حهى األخشي‬
:ً‫(يثم سهسهت) ححخىٌ كم عمذة عه‬

1) a single element - stored object or value(We call this part Data)


)‫ كائٍ أو لًُت يخضَت (َسًٍ هزا اندضء بُاَاث‬- ‫عُصش واحذ‬
2) links - reference to one or both neighboring nodes(We call this next)
)ٍ‫ إشاسة إنً أحذ انعمذحٍُ انًداوسحٍُ أو كالهًا (َسًٍ هزا انخان‬- ‫انشوابط‬

Types of linked lists ‫انواع القوائم المرتبطة‬


•Singly Linked List : Singly linked lists contain nodes which have a data part as well
as an address part (next) which points to the next node in sequence of nodes.

‫ ححخىٌ انمىائى انًشحبطت األحادٌه عهً انعمذ انخٍ ححخىٌ عهً خضء يٍ انبُاَاث‬:‫قائمة المرتبطه األحادية‬
.‫باإلضافت إنً خضء انعُىاٌ (انخانٍ) انزٌ َشُش إنً انعمذة انخانُت فٍ حسهسم انعمذ‬

3
BY NsM [DATA STRUCTURES IN ARABIC]

•Doubly Linked List : In a doubly linked list, each node (except he first and last
node) contains two links the first link points to the previous node and the next
link points to the next node in the sequence.

ً‫ ححخىٌ كم عمذة (باسخثُاء انعمذة األونً واألخُشة) عه‬، ‫فٍ لائًت يشحبطت انثُائُت‬: ‫قائمة المرتبطة الثنائية‬
.‫سابطٍُ َشُش انشابط األول إنً انعمذة انسابمت وانشابط انخانٍ َشُش إنً انعمذة انخانُت فٍ انخسهسم‬

Types of linked lists ‫أنواع القوائم المرتبطة‬


•Circular Linked List : In the circular linked list the last node of the list contains the
address of the first node.

‫ فٍ انمائًت انًشحبطت انذائشَت ححخىٌ انعمذة األخُشة يٍ انمائًت عهً عُىاٌ انعمذة‬: ‫قائمة المرتبطة الدائرية‬
..ً‫األون‬

Basic Operations ‫العملٌات األساسٌة‬


Following are the basic operations supported by an list.

..‫فٌما ٌلً العملٌات األساسٌة التً تدعمها القائمة‬

1.Insert in beginning: add an element at the beginning of the list.

.‫ إضافة عنصر فً بداٌة القائمة‬:‫إدراج فً البداٌة‬

2.Insert in the end : add an element in the end of the list.

4
BY NsM [DATA STRUCTURES IN ARABIC]

.‫ إضافة عنصر فً نهاٌة القائمة‬:‫إدراج فً النهاٌة‬

3.Insert after element : add an element after an item of the list.

.‫ إضافة عنصر بعد عنصر فً القائمة‬:‫إدراج بعد عنصر‬

4.Insert before element: add an element before an item of the list.

.‫ إضافة عنصر قبل عنصر القائمة‬:‫إدراج قبل العنصر‬

5.Delete first element: delete the element at the beginning of the list.

.‫ حذف العنصر فً بداٌة القائمة‬:‫حذف العنصر األول‬

6.Delete last element: delete the element from the end of the list.
‫ حزف انعُصش يٍ َهاَت انمائًت‬:‫حزف انعُصش األخُش‬

7.Delete and insert specific element.‫حذف وإدراج عنصر محدد‬

8.Display forward: displaying complete list in forward manner.

.‫ عرض قائمة كاملة بطرٌقة إلى األمام‬: ‫عرض الى األمام‬

9.Display backward: displaying complete list in backward manner.


.‫ عشض لائًت كايهت بطشَمت إنً انىساء‬:‫عرض إنً انىساء‬

Singly linked lists ‫قائمة المرتبطه األحادية‬


To construct linked list we need define two class:
: ٍُ‫ َحخاج إنً ححذَذ كالس‬، ‫إلَشاء لائًت يشحبطت‬
1. node class ‫كالس العقد‬
2. singly linked list class(SLL)‫كالس القائمة المرتبطة األحادٌه‬

class Node {
int data;
node next;
node(int data) {
this.data = data; } }

5
BY NsM [DATA STRUCTURES IN ARABIC]

class SLL {
node head;
public SLL() {
this.head = null;

}
// methods
...
}
}
In the following steps for create linked list with data(22, 33, 44, 55,66)
66،55 ، 44 ، 33 ، 22( ‫فً الخطوات التالٌة إلنشاء قائمة مرتبطة بالبٌانات‬

node head = new node(22);


head.next = new node(33);
head.next.next = new node(44);
head.next.next.next = new node(55);
head.next.next.next.next = new node(66);
or
head = new node(22);
node p= head;
p.next = new node(33);
p = p.next;
p.next = new node(44);
p = p.next;
p.next = new node(55);
p = p.next;
p.next = new node(66);

In the following figure shown some operation on singly linked list :

‫فً الشكل التالً ٌظهر بعض العملٌات على قائمة مرتبطة احادٌه‬

6
BY NsM [DATA STRUCTURES IN ARABIC]

7
BY NsM [DATA STRUCTURES IN ARABIC]

Some code segments in java to processing singly linked list

‫بعض مقاطع الكود فً جافا لمعالجة قائمة مرتبطة احادٌة‬

// method to print elements of singly linked list ‫طريقة لطباعة عناصر قائمة مرتبطة احادٌة‬

public void printSLL() {


for (node p = head; p != null; p = p.next) {
System.out.print(p.data + " ");
}
}

// method for adding new node ‫طريقة إلضافة عقدة جديدة‬

void addLast1 ( int value){

node newNode = new node(value);


node p = head;
if (head == null)
head = newNode;

else {
while (p.next != null)
p = p.next;
p.next = newNode;
}
}

8
BY NsM [DATA STRUCTURES IN ARABIC]

// method to delete first element ‫طريقة لحذف العنصر األول‬

//method to delete element after element in the list


‫طريقة لحذف عنصر بعد عنصر في القائمة‬

// method to add an element after an item of the list

ً‫طرٌقة إلضافة عنصر بعد عنصر ف‬


‫القائمة‬

9
BY NsM [DATA STRUCTURES IN ARABIC]

Doubly linked lists ‫قائمة مرتبطة ثنائية‬


In this type of linked list each node contains a pair of references. One reference
points to the node that precedes the node (prior) and the other points to the
node that follows the node (next).

‫ ٌشٌر مرجع واحد إلى العقدة‬.‫ تحتوي كل عقدة على زوج من المراجع‬، ‫فً هذا النوع من القائمة المرتبطة‬
)ً‫التً تسبق العقدة (السابقة) والنقاط األخرى إلى العقدة التً تتبع العقدة (التال‬

To construct double linked list we need define two class:

::::‫ نحتاج إلى تعرٌف كالسٌن‬، ‫إلنشاء قائمة ثنائية مرتبطة‬

Advantages & Disadvantages ‫المزاٌا و السلبٌات‬

Advantages: ‫مزاٌا‬

•Can be traversed in either direction ٍُ‫ًَكٍ اخخُاصها فٍ كال االحداه‬


•Some operations, such as deletion and inserting before a node, become easier
‫ أسهل‬، ‫ مثل الحذف واإلدراج قبل العقدة‬، ‫تصبح بعض العملٌات‬

10
BY NsM [DATA STRUCTURES IN ARABIC]

Disadvantages: ‫السلبٌات‬

•Requires more space ‫َخطهب يساحت أكبش‬


•List manipulations are slower (because more links must be changed)
)‫معالجة القائمة أبطأ (ألنه ٌجب تغٌٌر المزٌد من الروابط‬

•Greater chance of having bugs (because more links must be manipulated)


)‫فشصت أكبش نىخىد أخطاء (ألَه َدب يعاندت انًضَذ يٍ انشوابط‬

11
BY NsM [DATA STRUCTURES IN ARABIC]

H.W. Write code segments in java for the following operations:

:‫ للعملٌات التالٌة‬java ً‫واجب بٌتً اكتب مقاطع الكود ف‬

1.Add an element at the beginning of the list. ‫إضافة عنصر فً بداٌة القائمة‬

2.Add an element in the end of the list. ‫إضافة عنصر فً نهاٌة القائمة‬

3.Add an element after an item of the list. ‫إضافة عنصر بعد عنصر من القائمة‬

4.Add an element before an item of the list.‫إضافة عنصر قبل عنصرمن القائمة‬

5.Delete the element at the beginning of the list.‫حذف العنصر فً بداٌة القائمة‬

6.Delete the element from the end of the list.‫حذف العنصر من نهاٌة القائمة‬

Circular linked lists ‫قوائم مرتبطة دائرية‬


in this type of linked list: ‫فً هذا النوع من القائمة المرتبطة‬

Last node references the first node ‫تشٌر العقدة األخٌرة إلى العقدة األولى‬

Every node has a successor ‫كل عقدة لها خلٌفة‬


No node in a circular linked list contains null)‫ال توجد عقدة في قائمة مرتبطة دائرية تحتوي فارغ (خاليه‬

12
BY NsM [DATA STRUCTURES IN ARABIC]

Both singly linked list and doubly linked list can be made into as circular linked list:

:‫ٌمكن إدراج كل من القائمة المرتبطة احادٌة والقائمة الثنائٌة كقائمة مرتبطة دائرٌه‬

Singly linked list as circular, the next pointer of the last node points to the first
node.

.‫ ٌشٌر المؤشر التالً للعقدة األخٌرة إلى العقدة األولى‬، ‫القائمة األحادٌه المرتبطة كما دائرٌة‬

Doubly linked list as circular, the next pointer of the last node points to the first
node and the previous pointer of the first node points to the last node making the
circular in both directions.

‫ ٌشٌر المؤشر التالً للعقدة األخٌرة إلى العقدة األولى وٌشٌر المؤشر السابق‬، ‫قائمة مرتبطة ثنائٌه كما دائرٌة‬
.‫للعقدة األولى إلى العقدة األخٌرة مما ٌجعلها دائرٌة فً كال االتجاهٌن‬

To construct circular linked list we need define two class:

:‫ نحتاج إلى تعرٌف كالسٌن‬، ‫إلنشاء قائمة دائرٌة مرتبطة‬

1. node class ‫كالس العقدة‬

2. circular linked list class(CLL) ‫كالس قائمة مرتبطة دائرٌة‬

13
BY NsM [DATA STRUCTURES IN ARABIC]

H.W. Write code segments in java for the following operations:

:‫ للعملٌات التالٌة‬java ً‫واجب بٌتً اكتب مقاطع الكود ف‬

•Add an element at the beginning of the list. ‫إضافت عُصش فٍ بذاَت انمائًت‬
•Add an element in the end of the list. ‫إضافت عُصش فٍ َهاَت انمائًت‬
•Add an element after an item of the list. ‫إضافت عُصش بعذ عُصش يٍ انمائًت‬
•Add an element before an item of the list. ‫إضافت عُصش لبم عُصش انمائًت‬
•Delete the element at the beginning of the list. ‫احزف انعُصش فٍ بذاَت انمائًت‬
•Delete the element from the end of the list. ‫احزف انعُصش يٍ َهاَت انمائًت‬

14
BY NsM [DATA STRUCTURES IN ARABIC]

15
BY NsM [DATA STRUCTURES IN ARABIC]

16
BY NsM [DATA STRUCTURES IN ARABIC]

17
BY NsM [DATA STRUCTURES IN ARABIC]

18
BY NsM [DATA STRUCTURES IN ARABIC]

19
BY NsM [DATA STRUCTURES IN ARABIC]

‫بالتوفٌق‬

20

You might also like