Linked lists · القوائم المرتبطة (Linked lists)
Nodes joined by links
- A linked list is a chain of nodes.
- Each node holds a piece of data and a link to the next node.
- The last node links to
None, which marks the end.
العقد المتصلة بالروابط
- القائمة المرتبطة هي سلسلة من العقد.
- كل عقدة تحتوي على جزء من البيانات و رابط للعقدة التالية.
- تربط العقدة الأخيرة بـ
None، مما يشير إلى النهاية.
A node is a small record
- We can store one node in a dictionary:
{"data": ..., "next": ...}. "data"holds the value;"next"holds the next node (orNone).- The first node is called the head of the list.
العقدة سجل صغير
- يمكننا تخزين عقدة واحدة في قاموس:
{"data": ..., "next": ...}. - يحتوي
"data"على القيمة؛ يحتوي"next"على العقدة التالية (أوNone). - تُسمى العقدة الأولى رأس القائمة.
second = {"data": "b", "next": None}
first = {"data": "a", "next": second}
print(first["data"])
print(first["next"]["data"])
Walk the list
- Start at the head and follow each
"next"link. - Stop when you reach
None. - This visiting of every node is called traversal.
المشي عبر القائمة
- ابدأ من الرأس وتابع كل رابط
"next". - توقف عندما تصل إلى
None. - يسمى زيارة كل عقدة التجول (traversal).
head = {"data": 1, "next": {"data": 2, "next": None}}
node = head
while node is not None:
print(node["data"])
node = node["next"]
Why a linked list?
- You can insert or remove a node by changing links — no shifting of items.
- An array would have to move every item after the change.
- But a linked list has no index: to reach item 5 you must walk from the head.
لماذا القائمة المرتبطة؟
- يمكنك إدراج أو إزالة عقدة عن طريق تغيير الروابط — بدون تحريك العناصر.
- كان على المصفوفة نقل كل عنصر بعد التغيير.
- لكن القائمة المترابطة ليس لها فهارس: للوصول إلى العنصر 5 يجب أن تمشي من الرأس.
In Cambridge pseudocode
- The exam stores nodes in an array;
Nextis the index of the next node, andNULLmarks the end.
في الرمز الوهمي لكامبريدج
- يخزن الامتحان العقد في مصفوفة؛
Nextهو فهرس العقد التالي، وNULLيحدد النهاية.
TYPE Node
DECLARE Data : INTEGER
DECLARE Next : INTEGER // index of the next node, or NULL
ENDTYPE
current ← head
WHILE current <> NULL
OUTPUT list[current].Data
current ← list[current].Next
ENDWHILE
Common mistakes
- Never lose the
headpointer, or the whole list is unreachable. - The last node points to
None.
أخطاء شائعة
- لا تفقد مؤشر
headأبداً، أو ستصبح القائمة بأكملها غير قابلة للوصول. - العقدة الأخيرة تشير إلى
None.
Now you try
- A node is a dict
{"data": ..., "next": ...}. Follow"next"to walk the chain. - Press Check answer to test your code.
الآن جرب بنفسك
- العقدة هي قلمة ⟨dict⟩
{"data": ..., "next": ...}. اتبع"next"للمشي عبر السلسلة. - اضغط على تحقق من الإجابة لاختبار الكود الخاص بك.
Nodes linked by pointers · عقد مرتبطة بالمؤشرات
Each node points to the next; you insert/delete by re-linking. · كل عقدة تشير إلى التالية؛ تقوم بالإضافة/الحذف عن طريق إعادة الربط.
Build a linked list 1 → 2 → 3 using dictionaries. Make the three nodes, link each to the next, and point head at the first one. The last node's "next" must be None. · قم ببناء قائمة مرتبطة 1 → 2 → 3 باستخدام القواميس. اصنع العقد الثلاث، واربط كل منها بالتي تليها، وشوّه head نحو الأولى. يجب أن تكون قيمة "next" للعقدة الأخيرة هي None.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Walk the linked list starting at head and add up every node's "data". Store the total in total (the answer is 60). · تجول في القائمة المرتكة بدءاً من head واجمع قيم "data" لكل عقدة. احفظ المجموع في total (الجواب هو 60).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write length(head) that returns how many nodes are in the linked list. An empty list (head is None) has length 0. · اكتب length(head) التي ترجع عدد العقد في القائمة المرتبطة. القائمة الفارغة (حيث head يساوي None) طولها 0.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.