Skip to content · ⁨الانتقال إلى المحتوى⁩

Data Types and Structures · ⁨أنواع البيانات وهياكلها⁩

A-Level Computer Science · ⁨A-Level علوم الحاسوب⁩ · Topic 10 · ⁨الموضوع 10⁩

Video lesson for this topic · ⁨درس فيديو لهذا الموضوع⁩ Open the video page · ⁨افتح صفحة الفيديو⁩
17:40

أنواع البيانات والهياكل

كل قيمة يخزنها برنامجك تحتاج إلى نوع بيانات — واختيار النوع المناسب مهم. افترض أنك تخزن ما إذا كان عنصرًا متوفرًا في المخزون. يمكنك كتابة كلمة نعم…

English narration · English + 中文 subtitles burned in · ⁨سرد باللغة الإنجليزية · ترجمة مدمجة بالإنجليزية + الصينية⁩

10.1

Choosing data types · ⁨اختيار أنواع البيانات⁩

Syllabus · ⁨المنهج⁩
English
Candidates should be able to: Notes and guidance
Select and use appropriate data types for a problem solution including integer, real, char, string, Boolean, date (pseudocode will use the following data types: INTEGER, REAL, CHAR, STRING, BOOLEAN, DATE, ARRAY, FILE)
Show understanding of the purpose of a record structure to hold a set of data of different data types under one identifier Write pseudocode to define a record structure
Write pseudocode to read data from a record structure and save data to a record structure
العربية
يجب أن يكون المرشحون قادرين على: ملاحظات وإرشادات
اختيار واستخدام أنواع البيانات المناسبة لحل مشكلة ما بما في ذلك عدد صحيح، عدد حقيقي، حرف، سلسلة، منطقي، تاريخ (سيستخدم الشيفرة الوهمية أنواع البيانات التالية: INTEGER, REAL, CHAR, STRING, BOOLEAN, DATE, ARRAY, FILE)
إظهار فهم لغرض هيكل السجل لحفظ مجموعة من بيانات أنواع مختلفة تحت معرف واحد كتابة شيفرة وهمية لتعريف هيكل سجل
كتابة شيفرة وهمية لقراءة بيانات من هيكل سجل وحفظ بيانات إلى هيكل سجل

Source: Cambridge International syllabus · ⁨المصدر: منهج كامبريدج الدولي⁩

English

Every variable needs a data type 数据类型 — the kind of value it holds and the operations allowed:

  • INTEGER — a whole number (42, -7). For counts, indexes, IDs.
  • REAL — a number with a fractional part (3.14). For money, measurements.
  • STRING — characters in quotes ("Hello"). For text.
  • CHAR — a single character ('A').
  • BOOLEAN — TRUE or FALSE. For flags.
  • DATE — a calendar date.

Pick the smallest precise type that fits: INTEGER for whole counts, BOOLEAN for flags (not the strings "yes"/"no").

The "give the appropriate data type" tables are decided by how the value is used: the average mark of a class is REAL (it has a fractional part); an email address is STRING; the number of students is INTEGER; whether a student has paid is BOOLEAN; a date of birth is DATE; an array index is always INTEGER; a single grade letter is CHAR; a phone number is a STRING, because it starts with 0 and is never used in arithmetic. A BOOLEAN is used for a flag with only two states: whether a search has found its target, whether a member has paid, whether a seat is booked. For the identifier table, the variable name must be meaningful too: NumberOfPeople, not n.

العربية

كل متغير يحتاج إلى نوع بيانات — نوع القيمة التي يحملها والعمليات المسموحة:

  • INTEGER — عدد صحيح (42، -7). للعدود، الفهارس، المعرفات.
  • REAL — عدد مع جزء عشري (3.14). للأموال، القياسات.
  • STRING — أحرف بين علامات اقتباس ("Hello"). للنصوص.
  • CHAR — حرف واحد ('A').
  • BOOLEAN — TRUE أو FALSE. للمؤشرات.
  • DATE — تاريخ تقويم.

اختر أصغر نوع دقيق يناسب: INTEGER للعدود الصحيحة، BOOLEAN للمؤشرات (ليس النصوص "yes"/"no").

تحديد "إعطاء النوع المناسب" يعتمد على كيفية استخدام القيمة: متوسط العلامات لفصل دراسي هو REAL (له جزء عشري)؛ عنوان البريد الإلكتروني هو STRING؛ عدد الطلاب هو INTEGER؛ ما إذا دفع طالب هو BOOLEAN؛ تاريخ الميلاد هو DATE؛ فهرس مصفوفة هو دائماً INTEGER؛ حرف علامة واحد هو CHAR؛ رقم الهاتف هو STRING، لأنه يبدأ بـ 0 ولا يُستخدم أبداً في العمليات الحسابية. يُستخدم BOOLEAN للمؤشر الذي له حالتان فقط: ما إذا وجد البحث هدفه، ما إذا دفع عضو، ما إذا تم حجز مقعد. لجدول التعريفات، يجب أن يكون اسم المتغير دليلاً أيضاً: NumberOfPeople، لا n.

10.1

Records · ⁨السجلات⁩

English

A record 记录 (a record structure 记录结构) holds several fields of different types under one name — useful when several values describe one thing.

This defines the type TStockItem; declare variables of it:

Use dot notation to reach each field 字段:

Use a record when values always belong together (a customer, a stock item); use separate variables for unrelated values.

Worked example. A club stores, for each student, a student ID (a string), a name, a date of birth and up to three club numbers (integers). Write pseudocode to declare the record type, an array to hold $3000$ students, and a statement that stores a name in the first element.

The marks: TYPE with the identifier and ENDTYPE; each field declared with a suitable type; the array declared with its bounds and OF Student; the field reached with the index and a dot. A "state the error in the record declaration" question usually points at a missing ENDTYPE, a field with no type, or a field declared as a STRING that must hold arithmetic. Two conventions score marks on their own: an unused element is marked with a value that cannot be real data (an empty string, -1, an ID of 0), and it is good practice to use the same marker everywhere so that every module can recognise an unused slot; an unused club field is 0. The benefits of an array of records, for a "state three benefits": all the data for one entity is held under one identifier; the fields can have different data types; one array replaces several parallel arrays that would have to be kept in step; the whole set can be processed by one loop or passed as one parameter; and adding a field changes the type definition only. For one customer the suitable structure is a record (fields of different types under one name); for all customers it is an array of records.

العربية

السجل (هيكل السجل) يحتوي على عدة حقول من أنواع مختلفة تحت اسم واحد — مفيد عندما تصف عدة قيم شيئاً واحداً.

TYPE TStockItem
    DECLARE ItemID : INTEGER
    DECLARE Category : STRING
    DECLARE ItemCost : REAL
    DECLARE InStock : BOOLEAN
ENDTYPE

هذا يحدد النوع TStockItem؛ أعلن متغيرات منه:

DECLARE Item1 : TStockItem
DECLARE Items : ARRAY[1:100] OF TStockItem

استخدم Notation النقطة للوصول إلى كل حقل:

Item1.Category ← "Fruit"
OUTPUT Item1.Category, " costs ", Item1.ItemCost

استخدم سجلًا عندما تكون القيم دائمًا مرتبطة معًا (عميل، صنف مخزون)؛ استخدم متغيرات منفصلة للقيم غير ذات الصلة.

مثال محلول. يخزن نادٍ لكل طالب: رقم تعريف للطالب (سلسلة نصية)، واسم، وتاريخ ميلاد، وحتى ثلاثة أرقام للأندية (أعداد صحيحة). اكتب خوارزمية وهمية لإعلان نوع السجل، ومصفوفة لتخزين $3000$ طالب، وعبارة تخزن اسماً في العنصر الأول.

TYPE Student
    DECLARE StudentID : STRING
    DECLARE Name : STRING
    DECLARE DateOfBirth : DATE
    DECLARE Club : ARRAY[1:3] OF INTEGER
ENDTYPE

DECLARE Membership : ARRAY[1:3000] OF Student
Membership[1].Name ← "Li Wei"

التقييمات: TYPE بالمعرف وENDTYPE؛ يُعلن كل حقل بنوع مناسب؛ تُعلن المصفوفة بحدودها وOF Student؛ يُصل إلى الحقل باستخدام الفهرس ونقطة. عادةً ما تشير أسئلة "اذكر الخطأ في إعلان السجل" إلى缺失 ENDTYPE، أو حقل بدون نوع، أو حقل مُعلن كـ STRING يجب أن يحتوي على قيم حسابية. هناك اصطلاحان يحصلان على علامات بمفردهما: العنصر غير المستخدم يتم تمييزه بقيمة لا يمكن أن تكون بيانات حقيقية (سلسلة فارغة، -1، أو معرف 0)، ومن الممارسات الجيدة استخدام نفس الرمز دائمًا حتى تتمكن أي وحدة برمجية من التعرف على الخانة غير المستخدمة؛ الحقل غير المستخدم للنادي هو 0. فوائد مصفوفة من السجلات، لـ "اذكر ثلاث فوائد": جميع بيانات الكيان الواحد محفوظة تحت معرف واحد؛ يمكن للحقول أن يكون لها أنواع بيانات مختلفة؛ تستبدل مصفوفة واحدة عدة مصفوفات متوازية كان لا بد من الحفاظ عليها في تزامن؛ يمكن معالجة المجموعة بأكملها بواسطة حلقة واحدة أو تمريرها كمعامل واحد؛ وإضافة حقل يغير تعريف النوع فقط. لعميل واحد، البنية المناسبة هي سجل (حقول بأنواع مختلفة تحت اسم واحد)؛ ولجميع العملاء فهي مصفوفة من السجلات.

سجل A TStockItem مرسوم كطبقة من أربعة حقول تحت اسم واحد — ItemID (INTEGER)، Category (STRING)، ItemCost (REAL)، InStock (BOOLEAN) — يُصل إليها باستخدام نقطة مثل Item1.Category
يحوي السجل عدة حقول بأنواع مختلفة تحت اسم واحد
Explore · ⁨استكشف⁩

A record groups fields under one name · ⁨السجل يجمع الحقول تحت اسم واحد⁩

A record bundles related fields together. Each field is a named label you reach with dot notation — Item1.Category — not by a numeric index. · ⁨يجمع السجل الحقول ذات الصلة معاً. كل حقل هو تسمية يمكنك الوصول إليها باستخدام تدوين النقطة — Item1.Category — وليس بفهرس رقمي.⁩

Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
English العربية
array/əˈreɪ/ المصفوفة (array)
record/ˈrekɔːd/ سجل
record structure/ˈrekɔːd ˈstrʌktʃə/ بنية السجل
10.2

Arrays · ⁨المصفوفات⁩

Syllabus · ⁨المنهج⁩
English
Candidates should be able to: Notes and guidance
Use the technical terms associated with arrays Including index, upper bound and lower bound
Select a suitable data structure (1D or 2D array) to use for a given task
Write pseudocode for 1D and 2D arrays
Write pseudocode to process array data Sort using a bubble sort Search using a linear search
العربية
يجب أن يكون المرشحون قادرين على: ملاحظات وإرشادات
استخدام المصطلحات التقنية المرتبطة بـ المصفوفات بما في ذلك الفهرس، الحد الأعلى و الحد الأدنى
اختر بنية بيانات مناسبة (مصفوفة 1D أو مصفوفة 2D) لاستخدامها في مهمة معينة
اكتب كوداً زائفاً لمصفوفات 1D ومصفوفات 2D
كتابة الشيفرة الوهمية لمعالجة بيانات المصفوفة الفرز باستخدام فرز الفقاعات البحث باستخدام البحث الخطي

Source: Cambridge International syllabus · ⁨المصدر: منهج كامبريدج الدولي⁩

English

An array 数组 is an ordered collection of items of the same type, under one name, reached by an index 索引.

  • element 元素 — one item in the array.
  • bounds 边界 — the lowest and highest valid indices.
  • dimension 维度 — 1-D (a list), 2-D (a table), etc.
  • lower bound 下界 and upper bound 上界 — the first and last valid index; the number of elements is upper bound minus lower bound plus one, and for a 2-D array the product of the two counts.

So in ThisArray[n] ← 42 the array has one dimension, the index is the variable n (an INTEGER), and the element at that index receives 42. Before an array can be declared you need its data type as well as its bounds. To declare $120$ values that may include a decimal place: DECLARE Data : ARRAY[1:120] OF REAL; a $150$-row, two-column table of strings: DECLARE Data : ARRAY[1:150, 1:2] OF STRING, which has $300$ elements. The benefits of an array over separate variables, for a two-mark explain: one identifier instead of thirty; the elements can be processed by a loop with the index as the counter; the size is easy to change; and the whole set can be passed to a module as one parameter. An array can also replace a chain of selection statements: DaysInMonth[Month] looks up the answer directly instead of twelve IF clauses, which is shorter, faster to write and easier to maintain.

1-D arrays

Process every element with a FOR loop:

2-D arrays (2D array)

The first index is the row, the second the column. Use nested loops to visit every cell. Use 1-D for a single sequence, 2-D for two natural dimensions (a grid, rows × columns).

Common operations

A linear search 线性查找 checks each element until found:

To find a sum, count, maximum or minimum, set a running variable then sweep through:

A bubble sort 冒泡排序 puts an array in order: pass through it comparing each adjacent pair and swapping any that are out of order; repeat the passes until one pass makes no swaps.

Paper 2 asks for these algorithms both as pseudocode and as steps in words, and sometimes in their "efficient" form:

  • Largest value: set Largest to the first element; for each remaining element, if it is bigger than Largest, store it in Largest; after the loop output Largest. For the position of the largest, keep a second variable that stores the index each time Largest changes.
  • Linear search returning a position: set FoundAt ← -1 before the loop (a value that can never be a valid index, so it means "not found"); loop through the array; when the element matches, store the index and leave the loop; after the loop test FoundAt.
  • Count or output the non-blank elements: compare each element with the marker for an unused element ("" or -1) and count or output only those that differ.
  • Remove an item: find its index by a linear search; move every later element one place towards the start, so the gap closes; mark the last element as unused (or decrease the count).
  • Insert into a sorted array: find the first index whose element is larger; move that element and every later one one place towards the end; store the new value in the gap.
  • Efficient bubble sort: a Swapped flag so that the passes stop as soon as a pass makes no swap, and an upper limit that falls by one each pass because the largest value has already reached the end.

The marks are for the outer loop that repeats until no swaps, the flag set inside the IF, the three-line swap with a temporary variable, and the shrinking limit. A sort in "steps" (stepwise refinement) is: repeat until sorted; on each pass compare adjacent pairs; swap a pair that is out of order; after each pass the largest unsorted value is at the end. Two 1-D arrays of records or of parallel data are processed with one loop and one index; a 2-D array needs a nested loop, the outer over rows and the inner over columns, and a search in one row fixes the row index and loops over the column.

العربية

المصفوفة هي مجموعة مرتبة من العناصر من نفس النوع، تحت اسم واحد، يُصل إليها عن طريق فهرس.

  • عنصر — عنصر واحد في المصفوفة.
  • الحدود — أصغر وأكبر فهرس صالح.
  • البُعد — 1-D (قائمة)، 2-D (جدول)، إلخ.
  • الحد الأدنى والحد الأعلى — المؤشر الأول والأخير الصالح؛ عدد العناصر هو الحد الأعلى ناقص الحد الأدنى زائد واحد، وفي مصفوفة 2-A بعدد الأبعاد هو حاصل ضرب العددين معاً.

لذلك في ThisArray[n] ← 42 تحتوي المصفوفة على بُعد واحد، والفهرس هو المتغير n (وهو INTEGER)، والعنصر عند ذلك الفهرس يستقبل 42. قبل الإعلان عن مصفوفة تحتاج إلى نوع البيانات الخاص بها بالإضافة إلى حدودها. للإعلان عن $120$ قيم قد تتضمن فاصلة عشرية: DECLARE Data : ARRAY[1:120] OF REAL؛ جدولاً من $150$ صفوف وعمودين من النصوص: DECLARE Data : ARRAY[1:150, 1:2] OF STRING، والذي يحتوي على $300$ عناصر. فوائد المصفوفة على المتغيرات المنفصلة، لـ "اشرح بعلمتين": معرف واحد بدلاً من ثلاثين؛ يمكن معالجة العناصر بواسطة حلقة باستخدام الفهرس كعداد؛ الحجم سهل التغيير؛ ويمكن تمرير المجموعة بأكملها إلى وحدة برمجية كمعامل واحد. يمكن للمصفوفة أيضًا استبدال سلسلة من عبارات الاختيار: DaysInMonth[Month] يبحث عن الإجابة مباشرة بدلاً من اثني عشر IF شرطاً، وهو أقصر، وأسرع في الكتابة، وأسهل في الصيانة.

مصفوفات 1-A

DECLARE Names : ARRAY[1:5] OF STRING
Names[3] ← "Cara"
OUTPUT Names[3]

عالج كل عنصر باستخدام حلقة FOR:

FOR i ← 1 TO 5
    OUTPUT Names[i]
NEXT i
صف من الخلايا المفهرسة باسم myList، بفهارس من 0 إلى 8 مع تحديد الحد الأدنى (أول فهرس) والحد الأعلى (آخر فهرس)
مصفوفة 1-A (قائمة) بمؤشرات وحدود

مصفوفات 2-A (مصفوفة 2-A)

DECLARE Grid : ARRAY[1:3, 1:4] OF INTEGER
Grid[2, 3] ← 99

المؤشر الأول هو الصف، والثاني هو العمود. استخدم حلقات متداخلة لزيارة كل خلية. استخدم مصفوفة 1-A لتسلسل واحد، ومصفوفة 2-A لأبعاده طبيعية (شبكة، صفوف × أعمدة).

شبكة بحجم 3×4 بفهارس للصفوف والأعمدة؛ الخلية في الصف 2 والعمود 3 محددة بلون مختلف
مصفوفة 2-A (جدول) بمؤشرات صف وعمود

عمليات شائعة

البحث الخطي يتحقق من كل عنصر حتى يتم العثور عليه:

FOR i ← 1 TO n
    IF A[i] = Target THEN
        OUTPUT "Found at ", i
    ENDIF
NEXT i

لإيجاد مجموع أو عدّ أو قيمة قصوى أو دنيا،设定 متغير تراكمي ثم مر عبر المصفوفة:

Max ← A[1]
FOR i ← 2 TO n
    IF A[i] > Max THEN
        Max ← A[i]
    ENDIF
NEXT i

ترتيب الفقاعات يضع المصفوفة في ترتيب: مر خلالها مقارنة كل زوج مجاور وتبديل أي منهما غير مرتب؛ كرر المرور حتى لا يقوم أي مرور بتبديلات.

يسأل Paper 2 عن هذه الخوارزميات كخوارزميات وهمية وكـ خطوات بكلمات، وأحياناً بصيغتها "الكفاءة":

  • أكبر قيمة:设定 Largest للعنصر الأول؛ لكل عنصر متبقي، إذا كان أكبر من Largest، احفظه في Largest؛ بعد الحلقة أخرج Largest. بالنسبة للموقع الأكبر، احتفظ بمتغير ثانٍ يحفظ الفهرس في كل مرة يتغير فيها Largest.
  • بحث خطي يعيد موقعاً:设定 FoundAt ← -1 قبل الحلقة (قيمة لا يمكن أن تكون فهرساً صحيحاً، لذا تعني "لم يتم العثور")؛ مر عبر المصفوفة؛ عندما يطابق العنصر، احفظ الفهرس واخرج من الحلقة؛ بعد الحلقة اختبر FoundAt.
  • عد أو أخرج العناصر غير الفارغة: قارن كل عنصر برمز العنصر غير المستخدم ("" أو -1) وعد أو أخرج فقط تلك التي تختلف.
  • حذف عنصر: ابحث عن فهرسه باستخدام بحث خطي؛ حرّك كل عنصر لاحق خطوة نحو البداية، بحيث تغلق الفجوة؛ ضع علامة على العنصر الأخير بأنه غير مستخدم (أو قلل العداد).
  • الإدراج في مصفوفة مرتبة: ابحث عن أول فهرس يكون عنصره أكبر؛ حرّك ذلك العنصر وكل عنصر لاحق خطوة نحو النهاية؛ احفظ القيمة الجديدة في الفجوة.
  • ترتيب الفقاعات الفعال: علم Swapped حتى تتوقف المرورات بمجرد أن لا يقوم أي مرور بتبديل، وحد أقصى ينقص بمقدار واحد في كل مرور لأن القيمة الكبرى وصلت بالفعل إلى النهاية.
REPEAT
    Swapped ← FALSE
    FOR Index ← 1 TO Limit - 1
        IF Data[Index] > Data[Index + 1] THEN
            Temp ← Data[Index]
            Data[Index] ← Data[Index + 1]
            Data[Index + 1] ← Temp
            Swapped ← TRUE
        ENDIF
    NEXT Index
    Limit ← Limit - 1
UNTIL Swapped = FALSE

الدرجات مخصصة للحلقة الخارجية التي تتكرر حتى لا تكون هناك تبديلات، والعلم الذي يُضبط داخل IF، وعملية التبديل المكونة من ثلاثة أسطر باستخدام متغير مؤقت، والحد المتناقص. عملية الترتيب بـ"خطوات" (تحسين تدريجي) هي: التكرار حتى يتم الترتيب؛ في كل تمرير مقارنة الأزواج المجاورة؛ تبديل الزوج غير المرتب بشكل صحيح؛ بعد كل تمرير تكون أكبر قيمة غير مرتبة في النهاية. تُعالج مصفوفتان 1-A من السجلات أو البيانات المتوازية بحلقة واحدة ومؤشر واحد؛ تحتاج مصفوفة 2-A إلى حلقة متداخلة، خارجية للصفوف وداخلية للأعمدة، والبحث في صف واحد يثبت مؤشر الصف ويحلّق عبر العمود.

مرور واحد من فرز الفقاعات على 5، 2، 8، 1: مقارنة 5 و 2 والتبديل لإعطاء 2، 5، 8، 1؛ مقارنة 5 و 8 (مرتبة بالفعل)؛ مقارنة 8 و 1 والتبديل لإعطاء 2، 5، 1، 8، وبالتالي تصل أكبر قيمة 8 إلى النهاية
مرور واحد من فرز الفقاعات: يتم مقارنة الأزواج المجاورة وتبديلها، مما يدفع أكبر قيمة إلى النهاية
Explore · ⁨استكشف⁩

A 2-D array · ⁨مصفوفة أبعاد 2-D⁩

Pick a row and column to read one element — how a grid of data is stored and indexed. · ⁨اختر صفًا وعمودًا لقراءة عنصر واحد — كيف يتم تخزين البيانات الفهرسة.⁩

Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
English العربية
index/ˈɪndeks/ فهرس
bubble sort/ˈbʌbl sɔːt/ فرز الفقاعات
10.3

Files · ⁨الملفات⁩

Syllabus · ⁨المنهج⁩
English
Candidates should be able to: Notes and guidance
Show understanding of why files are needed
Write pseudocode to handle text files that consist of one or more lines
العربية
يجب أن يكون المرشحون قادرين على: ملاحظات وإرشادات
إظهار فهم لسبب الحاجة إلى الملفات
كتابة الشيفرة الوهمية للتعامل مع ملفات النص التي تتكون من سطر واحد أو أكثر

Source: Cambridge International syllabus · ⁨المصدر: منهج كامبريدج الدولي⁩

English

A file 文件 is data stored on secondary storage 辅助存储器, kept between program runs. Variables in RAM disappear when the program ends, so to save data permanently (high scores, records, settings) the program writes to a file. Files also let programs share data and restart from a saved state.

A text file 文本文件 holds one or more lines of readable characters; programs read and write text files line by line. Open a file before use and close it after:

EOF tests the end of file 文件结束 before reading. To write:

Always close every file — otherwise buffered writes may be lost and other programs may be locked out.

Why files (two marks): the data is kept after the program ends, so it is available the next time the program runs; it can be shared with other programs; and it can hold more than fits in memory. The characteristic of a text file that lets a program work through it is that it is a sequence of lines, read one after another from the start. The three modes: READ to read from the start; WRITE to create a new file, which deletes any existing contents, so it cannot be used to add to a file; APPEND to add lines at the end of an existing file. Test EOF before every read, and open the file only once, even when several modules use it.

Worked example. Write pseudocode for a procedure LastLines(FileName : STRING) that outputs the last three lines of a text file, in order.

Each new line pushes the previous three along, so when the file ends the three variables hold its last three lines; a file with fewer lines outputs empty strings. To output the first five lines, count the lines read and stop the loop at five or at EOF, whichever comes first; a file that is empty is detected by EOF being TRUE immediately after opening.

Fields in a line. A text file holds strings, so a record is written as one line with its fields joined by a separator 分隔符 character, and each number or Boolean converted with NUM_TO_STR (and read back with STR_TO_NUM, or by comparing with "TRUE"). Choose a separator that can never appear in the data: a comma or | for names and numbers, never a space when a name may contain one. If a field may contain any character, the separator can be confused with data; the fix is to put each field on its own line, or to write the field's length before it. One item per line is simple to read back but uses more lines and makes a record harder to see as a unit. Reading a file whose lines are in a known order (ascending by an ID) allows the search to stop as soon as a larger ID is read, instead of reading to the end. A save file that is created each time the game is saved needs a meaningful filename, for instance the player's name and the date and time, so that any earlier save can be restored.

العربية

الملف هو بيانات مخزنة على الذاكرة الثانوية، محفوظة بين تشغيلات البرنامج. تختفي المتغيرات في الذاكرة العشوائية عند انتهاء البرنامج، لذا لحفظ البيانات بشكل دائم (أعلى الدرجات، السجلات، الإعدادات)، يكتب البرنامج إلى ملف. تتيح الملفات أيضًا للبرامج مشاركة البيانات وإعادة البدء من حالة محفوظة.

تختفي المتغيرات في الذاكرة العشوائية عند انتهاء البرنامج، لكن الملف على القرص محفوظ بين التشغيلات، لذا يحفظ البرنامج البيانات ويحمّلها منه *تختفي المتغيرات في الذاكرة العشوائية عند انتهاء البرنامج؛ يبقى الملف على القرص محفوظًا بين التشغيلات

ملف نصي يحتوي على سطر واحد أو أكثر من الأح可读 characters؛ تقرأ البرامج وتكتب الملفات النصية سطراً بسطر. افتح ملفاً قبل استخدامه وأغلقه بعده:

OPENFILE "data.txt" FOR READ      // or FOR WRITE, FOR APPEND
WHILE NOT EOF("data.txt") DO
    READFILE "data.txt", LineString
    OUTPUT LineString
ENDWHILE
CLOSEFILE "data.txt"

EOF يختبر نهاية الملف قبل القراءة. للكتابة:

OPENFILE "log.txt" FOR WRITE
FOR i ← 1 TO 100
    WRITEFILE "log.txt", "Event " & i
NEXT i
CLOSEFILE "log.txt"

دائماً أغلق كل ملف — وإلا قد تُفقد الكتابات المخزنة المؤقتة وقد تُستبعد برامج أخرى.

لماذا الملفات (درجتان): تُحفظ البيانات بعد انتهاء البرنامج، لذا تكون متاحة في المرة التالية لتشغيل البرنامج؛ يمكن مشاركتها مع برامج أخرى؛ ويمكنها استيعاب أكثر مما يتسع له الذاكرة. خاصية الملف النصي التي تسمح للبرنامج بالعمل عليه هي أنه تسلسل من الأسطر، تُقرأ واحداً تلو الآخر من البداية. الأوضاع الثلاثة: READ للقراءة من البداية؛ WRITE لإنشاء ملف جديد، وهو يمسح أي محتويات موجودة، لذا لا يمكن استخدامه لإضافة ملف؛ APPEND لإضافة أسطر في نهاية ملف موجود. اختبر EOF قبل كل قراءة، وافتح الملف مرة واحدة فقط، حتى عندما تستخدمه عدة وحدات.

مثال محلل. اكتب خوارزمياً زائفا لإجراء LastLines(FileName : STRING) يقوم بإخراج آخر ثلاثة أسطر من ملف نصي، بنفس ترتيبها.

PROCEDURE LastLines(BYVAL FileName : STRING)
    DECLARE LineX, LineY, LineZ : STRING
    LineX ← ""
    LineY ← ""
    LineZ ← ""
    OPENFILE FileName FOR READ
    WHILE NOT EOF(FileName) DO
        LineX ← LineY
        LineY ← LineZ
        READFILE FileName, LineZ
    ENDWHILE
    CLOSEFILE FileName
    OUTPUT LineX
    OUTPUT LineY
    OUTPUT LineZ
ENDPROCEDURE

يدفع كل سطر جديد الأسطر الثلاثة السابقة للأمام،所以当文件结束时,三个变量保存其最后三行;行数较少的文件输出空字符串。若要输出前五行,计数已读取的行数并在五处或 EOF 处停止循环, whichever comes first; a file that is empty is detected by EOF being TRUE immediately after opening.

حقول في سطر. يحتوي ملف نصي على نصوص، لذا يُكتب السجل كسطر واحد مع ربط حقوله بواسطة رمز فاصل، وتُحوّل كل رقم أو قيمة منطقية باستخدام NUM_TO_STR (وتُقرأ مرة أخرى باستخدام STR_TO_NUM، أو بالمقارنة مع "TRUE"). اختر فاصلاً لا يمكن أن يظهر أبداً في البيانات: الفاصلة أو | للأسماء والأرقام، ولا تستخدم المسافة أبداً إذا كان الاسم قد يحتوي على مسافة. إذا could field قد يحتوي على أي حرف، قد يتشتت الفاصل مع البيانات؛ الحل هو وضع كل حقل في سطره الخاص، أو كتابة طول الحقل قبله. عنصر واحد لكل سطر سهل القراءة مرة أخرى ولكنه يستخدم المزيد من الأسطر ويجعل من الصعب رؤية السجل كوحدة. قراءة ملف تكون أسطره بترتيب معروف (تصاعدياً حسب معرف) يسمح بإيقاف البحث بمجرد قراءة معرف أكبر، بدلاً من القراءة حتى النهاية. ملف حفظ يتم إنشاؤه كل مرة يتم فيها الحفظ يحتاج إلى اسم ملف ذو معنى، على سبيل المثال اسم اللاعب والتاريخ والوقت، بحيث يمكن استعادة أي حفظ سابق.

سطر واحد من ملف نصي، 1023,Ali,12.50,TRUE، مقسم عند فاصل الفاصلة إلى الحقول الأربعة لسجل صنف الأسهم، مع التحويل الذي يحتاجه كل حقل: STR_TO_NUM للحقول الرقمية، والنص كما هو، والمقارنة مع TRUE لل布尔值 *سطر واحد من ملف نصي هو سجل واحد: حقوله موصولة بفاصل، ومحول لأنواعها عند قراءته مرة أخرى

Explore · ⁨استكشف⁩

Handling a file: open → use → close · ⁨التعامل مع ملف: فتح → استخدام → إغلاق⁩

Step through the lifecycle every file follows. The two easy-to-forget parts are testing EOF while reading in a loop, and always closing at the end. · ⁨مرّ في دورة حياة كل ملف. الجزآن اللذان يُنسيهما الناس بسهولة هما اختبار EOF أثناء القراءة في حلقة، والإغلاق دائماً في النهاية.⁩

Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
English العربية
field/fiːld/ حقل
element/ˈelɪmənt/ عنصر
bounds/baʊndz/ حدود
file/faɪl/ ملف
secondary storage/ˈsekəndəri ˈstɔːrɪdʒ/ التخزين الثانوي
10.4

Abstract Data Types (ADTs) · ⁨أنواع البيانات المجردة (ADTs)⁩

Syllabus · ⁨المنهج⁩
English
Candidates should be able to: Notes and guidance
Show understanding that an ADT is a collection of data and a set of operations on those data
Show understanding that a stack, queue and linked list are examples of ADTs Describe the key features of a stack, queue and linked list and justify their use for a given situation
Use a stack, queue and linked list to store data Candidates will not be required to write pseudocode for these structures, but they should be able to add, edit and delete data from these structures
Describe how a queue, stack and linked list can be implemented using arrays
العربية
يجب أن يكون المرشحون قادرين على: ملاحظات وإرشادات
إظهار فهم أن الـ ADT هو مجموعة من البيانات ومجموعة من العمليات على تلك البيانات
إظهار فهم أن المكدس، الطابور و القائمة المترابطة أمثلة على الأنواع المجردة للبيانات وصف الميزات الرئيسية لـ المكدس، الطابور و القائمة المترابطة وتبرير استخدامها لموقف معين
استخدام المكدس، الطابور و القائمة المترابطة لتخزين البيانات لن يُطلب من المرشحين كتابة شيفرة وهمية لهذه الهياكل، لكنهم يجب أن يتمكنوا من إضافة وتعديل وحذف البيانات من هذه الهياكل
وصف كيفية تنفيذ الطابور، المكدس و القائمة المترابطة باستخدام المصفوفات

Source: Cambridge International syllabus · ⁨المصدر: منهج كامبريدج الدولي⁩

English
Linked list: insert by rewiring pointers
Stack vs queue: LIFO and FIFO

An Abstract Data Type 抽象数据类型 (ADT) is a collection of data plus operations on it, defined by what it does, not how it is stored. The user works only through the operations; the implementation is hidden, so it can change without affecting code that uses the ADT. Know three: stack, queue, linked list.

The one-mark definition: an ADT is a collection of data together with a set of operations on that data. A stack, a queue, a linked list, a binary tree and an array are all ADTs. To justify a choice: a queue when items must be handled in the order they arrived (print jobs, key presses, customers in a shop), because it is first in, first out; a stack when the most recent item must be handled first (undo, going back through web pages, reversing an order, the return addresses of nested calls), because it is last in, first out; a linked list when items are inserted and deleted in the middle of an ordered sequence often, because only pointers change and nothing has to be shifted. To compare a stack and a queue: both are linear structures of items with an order, both are implemented with an array and pointers, and both need a check for full before adding and for empty before removing; a stack has one pointer and adds and removes at the same end, a queue has two pointers and adds at one end and removes at the other.

Stack

A stack 栈 works in LIFO 后进先出 order (Last In, First Out). Operations: push 入栈 (add to the top), pop 出栈 (remove from the top), peek (look at the top), and tests for empty/full. Uses: undo history, function-call return addresses, expression parsing, backtracking.

Worked example. A stack of characters holds, from the bottom, 'P', 'N', 'Z', 'X', 'Y', 'W', with the top-of-stack pointer at 'W' (memory location 202 of 200–207). The operations POP, POP, PUSH 'A', PUSH 'B', POP are performed. What is on the stack, and where does the pointer point?

The two pops remove 'W' then 'Y'; the pushes add 'A' then 'B' in their places; the last pop removes 'B'. The stack now holds 'P', 'N', 'Z', 'X', 'A' and the pointer is at 'A', location 203. The value that has been on the stack longest is the bottom item, 'P'; at most five further pops are possible before the stack is empty, and a pop on an empty stack is an error, which is why Pop() tests for empty first. A Push() function that returns TRUE on success first tests whether the pointer is at the top of the array (full) and returns FALSE if so. The array elements need no initialising before use, because the pointer alone says which elements are in use.

Queue

A queue 队列 works in FIFO 先进先出 order (First In, First Out). Operations: enqueue 入队 (add to the rear), dequeue 出队 (remove from the front), and tests for empty/full. Uses: print spooling, scheduling, breadth-first search, buffering.

To describe adding an item: check that the queue is not full; store the item at the position given by the end-of-queue pointer; increment the end pointer (and the count). To describe removing: check that the queue is not empty; read the item at the front pointer; increment the front pointer (and decrement the count). State the convention you use: if the end pointer marks the next free space, front and end pointers being equal means the queue is empty; if it marks the last item, equal pointers mean one item. In a linear queue the front pointer only ever moves forward, so cells behind it are wasted; that is what the circular queue below fixes. The two features of a queue to state: items are added at the rear and removed from the front, so the first item added is the first removed.

Linked list

A linked list 链表 stores data as a sequence of nodes 节点. Each node holds a value and a pointer 指针 to the next node; a head pointer marks the start, and the last node's pointer is a sentinel (e.g. NULL). Operations: insert, delete, search, and traverse 遍历 (visit each node in order). Its advantage over an array is cheap insertion/deletion (just adjust pointers); its disadvantage is slow random access (you must follow pointers from the head).

Adding a node in order (four marks): traverse the list from the head, following the pointers, until the node before the position is found (the last node whose value is smaller); take a free node and store the new value in it; set the new node's pointer to the address the previous node pointed to; set the previous node's pointer to the new node. If the new value belongs at the front, the head pointer is changed instead. Deleting a node: find the node before it, and set that node's pointer to the address the deleted node pointed to, so the list bypasses it; the freed node returns to the free list. Compared with a 1-D array, inserting or deleting in a linked list needs no shifting of the other items, and the list can grow until memory runs out; the cost is the extra pointer stored with every item, and that reaching the $n$th item means following $n$ pointers, since there is no direct index.

العربية

*قائمة مرتبطة: إدراج عن طريق إعادة توصيل المؤشرات

*STACK vs QUEUE: LIFO و FIFO

نوع البيانات المجرد (ADT) هو مجموعة بيانات بالإضافة إلى عمليات عليها، مُعرّف بـ ما يفعله، وليس كيف يتم تخزينه. يعمل المستخدم فقط من خلال العمليات؛ التنفيذ مخفي، لذا يمكن أن يتغير دون التأثير على الكود الذي يستخدم ADT. تعرف على ثلاثة: STACK، QUEUE، القائمة المرتبطة.

تعريف الدرجة الواحدة: البنية المجردة للبيانات (ADT) هي مجموعة من البيانات معًا مع مجموعة من العمليات على تلك البيانات. كل من المكدس، والطابور، والقائمة المرتبطة، والشجرة الثنائية، والمصفوفة هي هياكل مجردة للبيانات. لتبرير الاختيار: الطابور عندما يجب معالجة العناصر حسب ترتيب وصولها (الوظائف المطبوعة، ضغطات المفاتيح، العملاء في متجر)، لأنه نظام أول وارد أولاً مخرج؛ المكدس عندما يجب معالجة أحدث عنصر أولاً (التراجع، التصفح العكسي عبر صفحات الويب، عكس ترتيب، عناوين الإرجاع للدعوات المتداخلة)، لأنه نظام آخر وارد أولاً مخرج؛ القائمة المرتبطة عندما يتم إدراج وحذف العناصر في وسط التسلسل المرتبط غالبًا، لأن المؤشرات فقط تتغير ولا شيء يحتاج إلى إزاحة. لمقارنة المكدس والطابور: كلاهما هياكل خطية للعناصر ذات ترتيب، وكلاهما يُنفذ باستخدام مصفوفة ومؤشرات، وكلاهما يتطلب التحقق من الامتلاء قبل الإضافة والتحقق من الفراغ قبل الإزالة؛ المكدس له مؤشر واحد ويضيف وينزع من نفس الطرف، والطابور له مؤشرين ويضيف من طرف وينزع من الطرف الآخر.

المكدس

يعمل المكدس بترتيب LIFO (آخر وارد، أول مخرج). العمليات: دفع (إضافة إلى الأعلى)، سحب (إزالة من الأعلى)، نظر (النظر إلى الأعلى)، واختبارات الفراغ/الامتلاء. الاستخدامات: سجل التراجع، عناوين إرجاع استدعاء الدوال، تحليل التعبير، التتبع العكسي.

مكدس مخزن في مصفوفة معروض في ثلاث حالات؛ يتحرك مؤشر القمم للأعلى بعد الدفع وللأسفل بعد السحب، بينما يظل قاعدة المكدس ثابتة
الدفع والسحب يغيران مؤشر القمم؛ مؤشر القاعدة يبقى مكانه

مثال محلول. مكدس من الأحرف يحتوي، من الأسفل، 'P'، 'N'، 'Z'، 'X'، 'Y'، 'W'، مع مؤشر قمة المكدس عند 'W' (مكان الذاكرة 202 من 200–207). يتم تنفيذ العمليات POP، POP، PUSH 'A'، PUSH 'B'، POP. ما الذي يوجد على المكدس، وأين يشير المؤشر؟

الإزالتان تسحبان 'W' ثم 'Y'؛ الإضافات تضيف 'A' ثم 'B' في أماكنها؛ الإزالة الأخيرة تزيل 'B'. يحتوي المكدس الآن على 'P'، 'N'، 'Z'، 'X'، 'A' والمؤشر عند 'A'، الموقع 203. القيمة التي كانت على المكدس لأطول فترة هي العنصر السفلي، 'P'؛ يمكن إجراء خمس إضافات كحد أقصى قبل أن يصبح المكدس فارغًا، والإزالة على مكدس فارغة هي خطأ، ولهذا السبب Pop() يختبر الفراغ أولاً. دالة Push() تُرجع TRUE عند النجاح تختبر أولاً ما إذا كان المؤشر في أعلى المصفوفة (امتلاء) وتُرجع FALSE إذا كان الأمر كذلك. لا تحتاج عناصر المصفوفة إلى تهيئة قبل الاستخدام، لأن المؤشر وحده يقول أي العناصر قيد الاستخدام.

كومة عالية من الكتب مرتبة مسطحة فوق بعضها البعض *كومة الكتب هي مكدس يمكنك رؤيته. يمكنك فقط إضافة أو أخذ كتاب من القمة، لذا فإن آخر كتاب تضعه هو الأول الذي تأخذه — هذا بالضبط LIFO

الطابور

يعمل الطابور بترتيب FIFO (أول وارد، أول مخرج). العمليات: إدراج في الطابور (إضافة إلى الخلف)، إخراج من الطابور (إزالة من الأمام)، واختبارات الفراغ/الامتلاء. الاستخدامات: طباعة الطوابع، الجدولة، البحث بالعرض، التخزين المؤقت.

طابور خطي مخزن في مصفوفة معروض في ثلاث حالات؛ إضافة العنصر تحرك مؤشر الخلف وإزالة العنصر تحرك مؤشر الأمام، تاركاً الخلية الأولى فارغة ومهدرة
الإدراج في الطابور يضيف من الخلف؛ الإخراج من الطابور يزيل من الأمام

لـ وصف إضافة عنصر: تحقق من أن الطابور ليس ممتلئاً؛ خزن العنصر عند الموقع المعطى بواسطة مؤشر نهاية الطابور؛ زِد المؤشر النهائي (عداد العد). لـ وصف الإزالة: تحقق من أن الطابور ليس فارغاً؛ اقرأ العنصر عند مؤشر الأمام؛ زِد مؤشر الأمام (ونقص العداد). حدد约定ية تستخدمها: إذا كان مؤشر النهاية يحدد المساحة الحرة التالية، فإن تساوي مؤشري الأمام والنهاية يعني أن الطابور فارغ؛ إذا كان يحدد آخر عنصر، فإن المؤشرات المتساوية تعني عنصراً واحداً. في الطابور الخطي يتحرك مؤشر الأمام دائماً للأمام، لذا تكون الخلايا خلفه مهدرة؛ وهذا ما يحله طابور الحلقات أدناه. ميزتان للطابور للتعبير عنهما: يتم إضافة العناصر من الخلف وإزالتها من الأمام، لذا فإن أول عنصر يتم إضافته هو أول عنصر يتم إزالته.

صف طويل جداً من الناس ينتظرون خلف بعضهم البعض، تمتد على طول جدار نحو الأفق *طابور من الناس هو طابور يمكنك رؤيته. تنضم إلى الخلف ويتم خدمتك من الأمام، لذا فإن من انتظر الأطول يتم خدمته أولاً — هذا بالضبط FIFO

القائمة المرتبطة

تخزن القائمة المرتبطة البيانات كتسلسل من العقد. كل عقدة تحمل قيمة ومؤشر إلى العقدة التالية؛ مؤشر الرأس يحدد البداية، ومؤشر آخر عقدة هو عتبة (مثل NULL). العمليات: الإدراج، الحذف، البحث، والمرور (زيارة كل عقدة بالترتيب). ميزتها مقارنة بالمصفوفة هي الإدراج/الحذف الرخيص (ضبط المؤشرات فقط)؛ وعيبها هو الوصول العشوائي البطيء (يجب اتباع المؤشرات من الرأس).

أربعة عقد في صف، كل منها يحمل قيمة وحقل مؤشر التالي؛ يشير مؤشر الرأس إلى العقدة الأولى ومؤشر العقدة الأخيرة هو NULL
قائمة مرتبطة: كل عقدة تشير إلى التالية

إضافة عقدة بالترتيب (أربع درجات): تجول القائمة من الرأس، متبعاً المؤشرات، حتى يتم العثور على العقدة التي تسبق الموقع (آخر عقدة قيمتها أصغر)؛ خذ عقدة حرة واحفظ القيمة الجديدة فيها؛ حدد مؤشر العقدة الجديدة بعنوان العقدة السابقة؛ حدد مؤشر العقدة السابقة بالعقدة الجديدة. إذا كانت القيمة الجديدة تنتمي في البداية، يتم تغيير مؤشر الرأس بدلاً من ذلك. حذف عقدة: ابحث عن العقدة التي تسبقها، وحدد مؤشر تلك العقدة بعنوان العقدة المحذوفة، بحيث تتجاوز القائمة عقدها؛ تعود العقدة المحررة إلى قائمة العقد الحرة. بالمقارنة مع مصفوفة 1-A، لا يتطلب الإدراج أو الحذف في القائمة المترابطة إزاحة العناصر الأخرى، ويمكن للقائمة أن تكبر حتى نفاد الذاكرة؛ التكلفة هي المؤشر الإضافي المخزن مع كل عنصر، وأن الوصول إلى العنصر $n$ يعني اتباع $n$ مؤشرات، حيث لا يوجد مؤشر مباشر.

Explore · ⁨استكشف⁩

A linked list: nodes joined by pointers · ⁨قائمة مترابطة: عقد متصلة بواسطة مؤشرات.⁩

Each node stores a value and a pointer to the next node. Inserting or deleting just re-links pointers — no items shift along, unlike an array. · ⁨كل عقدة تخزن قيمة ومؤشراً إلى العقدة التالية. الإضافة أو الحذف يعيد ربط المؤشرات فقط — لا تنزح العناصر، على عكس المصفوفة.⁩

Explore · ⁨استكشف⁩

Stacks and queues · ⁨الكدوس وال queues⁩

Push and pop. A stack is last-in-first-out; a queue is first-in-first-out — two key ADTs. · ⁨الدفع والإزالة. المكدس آخر دخول أول خروج؛ القائمة الانتظار أول دخول أول خروج — وهما بنيتان بيانات أساسيتان.⁩

Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
English العربية
stack/stæk/ مكدس
dimension/daɪˈmenʃn/ البُعد
push/pʊʃ/ دفع
separator/ˈsepəreɪtə/ فاصل
linked list/lɪŋkt lɪst/ قائمة مرتبطة
queue/kjuː/ صف
LIFO/ˈlaɪfəʊ/ LIFO
FIFO/ˈfaɪfəʊ/ FIFO.
pop/pɒp/ إزالة
Watch lesson · ⁨شاهد الدرس⁩
10.4

Implementing ADTs using arrays · ⁨تطبيق الأنواع抽象ية للبيانات باستخدام المصفوفات⁩

English

Stack using an array

Hold items in Stack[1:MaxSize] with an integer Top (0 when empty).

  • Push(x): if Top = MaxSize the stack is full (overflow 溢出); else Top ← Top + 1; Stack[Top] ← x.
  • Pop(): if Top = 0 the stack is empty (underflow 下溢); else return Stack[Top] and Top ← Top - 1.

Queue using a circular array

A simple queue lets Front and Rear march off the end, wasting the start. The fix is a circular array 循环数组 — when a pointer reaches MaxSize it wraps back to 1:

  • Enqueue(x): check full; else Rear ← (Rear MOD MaxSize) + 1; Queue[Rear] ← x.
  • Dequeue(): check empty; else return Queue[Front] and Front ← (Front MOD MaxSize) + 1.

Track a separate count to tell empty from full.

The algorithm for the end pointer, in words: if the count equals the size, report that the queue is full and stop; otherwise add one to the end pointer; if it is now past the last index, set it to the first index; store the item there and add one to the count. The declarations that a five-mark "describe the declaration and initialisation" answer lists: the array with its size and element type; a front pointer and an end pointer, both initialised to the first index (or the front to the first index and the end to the next free space); and a count of items, initialised to $0$.

For example, with MaxSize = 6: if Rear = 5, then (5 MOD 6) + 1 = 6, so the next item goes in cell 6; if Rear = 6, then (6 MOD 6) + 1 = 1, so the pointer wraps back to cell 1.

Linked list using an array

Use an array of records, each with a Next index:

A free list 空闲列表 chains the unused slots, just as the data list chains its used ones. To insert: take a slot from FreeListHead, set the new node's value and Next, and update the previous node's Next (or Head). To delete: unlink the node and return its slot to the free list. This gives the flexibility of a linked structure with the static allocation of an array.

Worked example. A linked list is held in a Data array and a Pointer array, with Start pointing to index 1. The list is 1 → 3 → 4 (index 1 holds D40, index 3 holds D32, index 4 holds D11, whose pointer is $\emptyset$); the free list starts at index 2 and continues 2 → 5. Insert D6 between D32 and D11.

Take the first free node, index 2, and set FreeStart to its pointer, 5; store D6 in Data[2]; set Pointer[2] to the value Pointer[3] held, which is 4; set Pointer[3] to 2. The list reads 1 → 3 → 2 → 4 and the free list is 5 → $\emptyset$. The answer to "how can the linked list be implemented" is exactly these parts: an array (or array of records) for the data, a parallel array for the pointers holding indices, a start pointer, a free-list pointer and a null value such as $-1$ for the end.

Worked example. A circular queue is held in an array of size 5 (indices 0 to 4) with Front = 3, Rear = 3 and one item stored. Two items are added, then two are removed. Where are the pointers, and why use a circular queue at all? Every move uses (pointer + 1) MOD size, so the pointers wrap. Adding twice moves Rear: $3 \rightarrow 4$, then $4 \rightarrow 0$ (because $(4+1) \bmod 5 = 0$), so Rear = 0 and three items are stored. Removing twice moves Front the same way: $3 \rightarrow 4$, then $4 \rightarrow 0$, leaving Front = 0 and one item. The wrap is the whole point: in a linear array queue the pointers march to the end and the freed space at the front is wasted even when the queue is empty. Remember a queue removes at the Front and adds at the Rear - a stack uses one pointer for both.

العربية

الرافعة باستخدام مصفوفة

احتفظ بالعناصر في Stack[1:MaxSize] مع عدد صحيح Top (0 عندما تكون فارغة).

  • Push(x): إذا Top = MaxSize فإن الرافعة ممتلئة (** Overflow**); وإلا Top ← Top + 1؛ Stack[Top] ← x.
  • Pop(): إذا Top = 0 فإن الكومة فارغة (انخفاض); وإلا ارجع Stack[Top] و Top ← Top - 1.

الطابور باستخدام مصفوفة دائرية

يُسمح للطابور البسيط بـ Front و Rear بالخروج من النهاية، مما يهدر البداية. الحل هو المصفوفة الدائرية — عندما يصل مؤشر إلى MaxSize يعيد wrapping إلى 1:

  • Enqueue(x): تحقق من الامتلاء; وإلا Rear ← (Rear MOD MaxSize) + 1; Queue[Rear] ← x.
  • Dequeue(): تحقق من الفراغ; وإلا ارجع Queue[Front] و Front ← (Front MOD MaxSize) + 1.

تتبع عدداً منفصلاً للتمييز بين الحالة الفارغة والممتلئة.

الخوارزمية لمؤشر النهاية، بالكلمات: إذا كان العداد يساوي الحجم، أفيد بأن الطابور ممتلئ وتوقف؛ وإلا أضف واحداً إلى مؤشر النهاية؛ إذا تجاوز الآن آخر فهرس، ضعه في أول فهرس؛ احفظ العنصر هناك وأضف واحداً إلى العداد. التصريحات التي تضمها إجابة "وصف التصريح والتهيئة" ذات الخمس علامات: المصفوفة بحجمها ونوع عناصرها؛ ومؤشر أمامي ومؤشر نهائي، كلاهما مُهيأ لأول فهرس (أو الأمامي لأول فهرس والنهائي للفضاء الحر التالي)؛ وعدد العناصر، مُهيأ لـ $0$.

على سبيل المثال، مع MaxSize = 6: إذا Rear = 5، فإن (5 MOD 6) + 1 = 6، لذا يتجه العنصر التالي إلى الخلية 6؛ إذا Rear = 6، فإن (6 MOD 6) + 1 = 1، لذا يعيد المؤشر wrapping إلى الخلية 1.

طابور دائري مخزن في مصفوفة؛ الخلايا الممتلئة تعبر عن آخر خلية لتعود إلى البداية، مع سهم منحني يُظهر إعادة wrapping المؤشر من آخر فهرس إلى الخلية 1
يعود طابور دائري المؤشرات إلى بداية المصفوفة

قائمة مرتبطة باستخدام مصفوفة

استخدم مصفوفة من السجلات، كل منها يحتوي على فهرس Next:

TYPE TNode
    DECLARE Value : INTEGER
    DECLARE Next : INTEGER     // index of the next node, or -1 for end
ENDTYPE

DECLARE Nodes : ARRAY[1:MaxSize] OF TNode
DECLARE Head : INTEGER         // index of first node, -1 if empty
DECLARE FreeListHead : INTEGER // first available free node

تربط القائمة الحرة بين Slots غير المستخدمة، تماماً كما تربط قائمة البيانات بين المستخدمة. للإدراج: خذ slot من FreeListHead، ضع قيمة العقدة الجديدة ومؤشر Next، وحديث مؤشر العقدة السابقة Next (أو Head). للحذف: افصل العقدة وأعد slotها إلى القائمة الحرة. هذا يمنحك مرونة البنية المرتبطة مع التخصيص الثابت للمصفوفة.

مصفوفة قيمة ومصفوفة Next الموازية لتنفيذ قائمة مرتبطة؛ مؤشر Head يربط العقد المستخدمة ومؤشر FreeListHead يربط Slots الحرة، كل منهما ينتهي بـ Next = -1
قائمة مرتبطة مخزنة في مصفوفة: مصفوفة بيانات ومصفوفة مؤشرات

مثال محلول. قائمة مرتبطة محفوظة في مصفوفة Data ومصفوفة Pointer، مع Start يشير إلى index 1. القائمة هي 1 → 3 → 4 (index 1 يحتوي على D40، index 3 يحتوي على D32، index 4 يحتوي على D11،whose pointer is $\emptyset$)؛ القائمة الحرة تبدأ عند index 2 وتستمر 2 → 5. أدخل D6 بين D32 و D11.

خذ أول عقدة حرة، index 2، وضع FreeStart على مؤشره، 5؛ احفظ D6 في Data[2]؛ ضع Pointer[2] على القيمة Pointer[3] المحتواة، وهي 4؛ ضع Pointer[3] على 2. تقرأ القائمة 1 → 3 → 2 → 4 والقائمة الحرة هي 5 → $\emptyset$. إجابة "كيف يمكن تنفيذ القائمة المرتبطة" هي بالضبط هذه الأجزاء: مصفوفة (أو مصفوفة من السجلات) للبيانات، مصفوفة موازية للمؤشرات تحمل الفهارس، مؤشر بداية، مؤشر قائمة حرة وقيمة فارغة مثل $-1$ للنهاية.

مثال محلول. طابور دائري محفوظ في مصفوفة بحجم 5 (مؤشرات 0 إلى 4) مع Front = 3، Rear = 3 وعنصر واحد مخزن. تم إضافة عنصرين، ثم إزالة اثنين. أين المؤشرات، ولماذا نستخدم طابوراً دائرياً على الإطلاق؟ كل حركة تستخدم (pointer + 1) MOD size، لذلك المؤشرات تلتف. الإضافة مرتين تحرك Rear: $3 \rightarrow 4$، ثم $4 \rightarrow 0$ (لأن $(4+1) \bmod 5 = 0$)، لذا Rear = 0 وثلاثة عناصر مخزنة. الإزالة مرتين تحرك Front بنفس الطريقة: $3 \rightarrow 4$، ثم $4 \rightarrow 0$، تاركاً Front = 0 وعنصر واحد. الالتفاف هو النقطة الأساسية: في طابور مصفوفة خطية تتقدم المؤشرات إلى النهاية والمكان المحرر في البداية مهدر حتى عندما يكون الطابور فارغاً. تذكر أن الطابور يزيل من الأمام ويضيف من الخلف - الكومة تستخدم مؤشراً واحداً لكلا الغرضين.

Explore · ⁨استكشف⁩

Implementing ADTs with arrays · ⁨تنفيذ ADTs باستخدام المصفوفات.⁩

FIFO · ⁨FIFO.⁩

A queue is first-in-first-out — enqueue at the back, dequeue from the front. · ⁨الطابور يعمل بنظام أول دخل أول خروج — إضافة في الخلف، وإزالة من الأمام.⁩

Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
English العربية
pointer/ˈpɔɪntə/ مؤشر
enqueue/enˈkjuː/ إضافة للصف
dequeue/diːˈkjuː/ إزالة من الصف
node/nəʊd/ عقدة
traverse/trəˈvɜːs/ استعراض
free list/friː lɪst/ قائمة حرة
overflow/ˌəʊvəˈfləʊ/ الانحياز (overflow)
underflow/ˌʌndəˈfləʊ/ انحدار
circular array/ˈsɜːkjʊlə əˈreɪ/ مصفوفة دائرية
10.4

Definitions the examiner accepts · ⁨التعريفات التي يقبلها المصحح⁩

English

A definition question is marked against fixed wording. Learn these exactly.

Term Definition
record a data structure that holds a set of data items (fields) of different data types under one identifier
array a data structure that holds a fixed number of elements of the same data type under one identifier, each accessed by an index
index the number that identifies one element of an array
upper bound, lower bound the largest and smallest valid index of an array
text file a file that stores data as lines of characters, which a program reads and writes one line at a time
abstract data type a collection of data together with a set of operations on that data
stack a list in which items are added to and removed from the same end, the top, so the last item added is the first removed (LIFO)
queue a list in which items are added at the rear and removed from the front, so the first item added is the first removed (FIFO)
linked list a list in which each node holds a data item and a pointer to the next node, with a start pointer to the first node
pointer a variable that holds the address (or index) of a node or of a position in a structure
linear search checking each element in turn from the first until the target is found or the end is reached
bubble sort repeated passes through the array comparing adjacent pairs and swapping those out of order, until a pass makes no swaps
العربية

السؤال التعريفي يتم تقييمه بناءً على صياغة ثابتة. احفظها بدقة.

مصطلح تعريف
record بنية بيانات تحتفظ بمجموعة من عناصر البيانات (حقول) من أنواع بيانات مختلفة تحت معرف واحد
array بنية بيانات تحتفظ بعدد ثابت من العناصر من نفس نوع البيانات تحت معرف واحد، يتم الوصول إلى كل منها بفهرس
index الرقم الذي يحدد عنصراً واحداً من مصفوفة
upper bound, lower bound أكبر وأصغر فهرس صالح لمصفوفة
text file ملف يخزن البيانات كسطور من الأحرف، يقرأه البرنامج ويكتبه سطرًا تلو الآخر
abstract data type مجموعة من البيانات جنباً إلى جنب مع مجموعة من العمليات على تلك البيانات
stack قائمة تُضاف إليها العناصر وتُسترد من نفس النهاية، الأعلى، sehingga last item added is the first removed (LIFO)
queue قائمة تُضاف إليها العناصر من الخلف وتُسترد من الأمام، sehingga first item added is the first removed (FIFO)
قائمة مترابطة قائمة يكون فيها كل عقدة تحمل عنصر بيانات ومؤشر إلى العقدة التالية، مع مؤشر بداية يشير إلى أول عقدة
مؤشر متغير يحمل عنوان (أو فهرس) لعقدة أو لموضع في بنية ما
بحث خطي فحص كل عنصر على التوالي بدءاً من الأول حتى يتم العثور على الهدف أو الوصول إلى النهاية
ترتيب الفقاعات مرور متكرر عبر المصفوفة بمقارنة الأزواج المجاورة وتبديل تلك التي ليست مرتبة، حتى يقوم أحد المرات بدون تبديل
Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
English العربية
data type/ˈdeɪtə taɪp/ نوع البيانات
lower bound/ˈləʊə baʊnd/ الحد الأدنى
upper bound/ˈʌpə baʊnd/ الحد الأعلى
linear search/ˈlɪnɪə sɜːtʃ/ البحث الخطي
text file/tekst faɪl/ ملف نصي
end of file/end ɒv faɪl/ نهاية الملف
Abstract Data Type/ˈæbstrækt ˈdeɪtə taɪp/ نوع البيانات المجرد
10.4

Exam tips · ⁨نصائح للامتحان⁩

English
  • Choose the right data structure and justify it (a record for mixed fields, a 2-D array for a grid).
  • Know how to implement a stack, queue and linked list with an array and pointers (top; front/rear; next).
  • Distinguish an ADT (its behaviour) from its implementation (array plus pointers).

Common mistakes

  • A record declaration without ENDTYPE, or fields without types. Every field is a DECLARE line with a type.
  • Reading past the end of a file, or writing with WRITE when the file must keep its contents. Test EOF before each read; use APPEND to add.
  • Writing a number to a text file without converting it. A file holds strings: NUM_TO_STR out, STR_TO_NUM back.
  • Forgetting the checks. Push and enqueue test for full first; Pop and dequeue test for empty first, and the answer says so.
  • Losing the rest of the list when inserting a node. Set the new node's pointer to the old next node before changing the previous node's pointer.
  • A linear search that never says "not found". Initialise the position to $-1$ and test it after the loop.
العربية
  • اختر الهيكل المناسب للبيانات وبرره ذلك (سجل لمجالات مختلطة، مصفوفة 2-A لشبكة).
  • تعرف كيفية تنفيذ المكدس، الطابور والقائمة المترابطة باستخدام مصفوفة ومؤشرات (الأعلى؛ الأمام/الخلف؛ التالي).
  • ميز بين التعريف المجرد للنوع (سلوكه) وبين تنفيذه (مصفوفة بالإضافة إلى مؤشرات).

أخطاء شائعة

  • إعلان سجل بدون ENDTYPE، أو حقول بدون أنواع. كل حقل هو سطر DECLARE يحتوي على نوع.
  • القراءة ما وراء نهاية الملف، أو الكتابة باستخدام WRITE عندما يجب أن يحافظ الملف على محتوياته. اختبر EOF قبل كل قراءة؛ استخدم APPEND للإضافة.
  • كتابة رقم في ملف نصي دون تحويله. يحتوي الملف على نصوص: NUM_TO_STR للخارج، STR_TO_NUM للخلف.
  • نسيان الفحوصات. Push و enqueue يختبران الامتلاء أولاً؛ Pop و dequeue يختبران الفراغ أولاً، والإجابة تذكر ذلك.
  • فقدان بقية القائمة عند إدخال عقدة. ضع مؤشر العقدة الجديدة في العقدة التالية القديمة قبل تغيير مؤشر العقدة السابقة.
  • بحث خطي لا يقول أبداً "لم يتم العثور". قم بتهيئة الموقع في $-1$ وختبره بعد الحلقة.

Interactive lessons on this topic · ⁨دروس تفاعلية حول هذا الموضوع⁩

Work through it step by step, with instant-check exercises. · ⁨ا-working عليه خطوة بخطوة، مع تمارين تحقق فوري.⁩

Past Papers · ⁨أوراق الامتحانات السابقة⁩

More topics in A-Level Computer Science · ⁨A-Level علوم الحاسوب⁩ · ⁨المزيد من المواضيع في A-Level Computer Science · ⁨A-Level علوم الحاسوب⁩⁩

Log in or create account · ⁨تسجيل الدخول أو إنشاء حساب⁩

IGCSE, A-Level & AP