Stacks · المكدسات (Stacks)
What is an ADT?
- An Abstract Data Type (ADT) is a collection of data plus a set of operations on it.
- You use the operations and ignore how it is built inside.
- A stack, a queue, and a linked list are all ADTs.
ما هو ADT؟
- النوع التجريبي للبيانات (ADT) هو مجموعة بيانات بالإضافة إلى مجموعة من العمليات عليها.
- تستخدم العمليات وتجاهل كيفية بنائها داخليًا.
- المكدس، الطابور، و القائمة المرتبطة هي جميعها أنواع ADT.
A stack is LIFO
- A stack is Last In, First Out (LIFO).
- The last item you add is the first one you take off.
- Think of a stack of plates: you take the top plate first.
المكدس LIFO
- المكدس هو آخر يدخل، أول يخرج (LIFO).
- آخر عنصر تضيفه هو الأول الذي تخرجه.
- فكر في مكدس الأطباق: تأخذ الصحن العلوي أولاً.
push and pop with a list
- We can build a stack from a Python list.
- push =
stack.append(x)— add to the end (the top). - pop =
stack.pop()— remove and return the end (the top).
push و pop مع قائمة
- يمكننا بناء مكدس من قائمة بايثون.
- push =
stack.append(x)— أضف إلى النهاية (الأعلى). - pop =
stack.pop()— أزل وأعد القيمة النهائية (الأعلى).
stack = []
stack.append("a")
stack.append("b")
print(stack.pop())
print(stack)
peek and empty
- peek at the top without removing it:
stack[-1]. - A stack is empty when
len(stack) == 0. - Popping an empty stack is an error, so check first.
peek و empty
- peek (اطلعل) على العنصر العلوي دون إزالته:
stack[-1]. - يكون المكدس فارغًا عندما
len(stack) == 0. - إزالة عنصر من مكدس فارغ يعتبر خطأ، لذا تحقق أولاً.
stack = [10, 20, 30]
print(stack[-1]) # peek the top
print(len(stack) == 0) # is it empty?
In Cambridge pseudocode
- The exam builds a stack from an array plus a
toppointer (an index).
في الرمز الوهمي لكامبريدج
- يبني الامتحان مكدسًا من مصفوفة بالإضافة إلى مؤشر
top(الفهرس).
DECLARE stack : ARRAY[1:10] OF INTEGER
DECLARE top : INTEGER
top ← 0 // 0 means empty
// push value
top ← top + 1
stack[top] ← value
// pop into value
value ← stack[top]
top ← top - 1
Common mistakes
- A stack is last-in, first-out: push to the top, pop from the top.
- Check it is not empty before you pop.
أخطاء شائعة
- المكدس last-in, first-out: push to the top, pop from the top.
- تحقق من أنه ليس فارغًا قبل pop.
Now you try
- Use a list as your stack (
appendto push,popto remove). - Press Check answer to test your code.
الآن جرب بنفسك
- استخدم قائمة كمكدس (
appendلـ push،popللإزالة). - اضغط على تحقق من الإجابة لاختبار الكود الخاص بك.
A stack is LIFO · المكدس يعمل بنظام LIFO
A stack pushes and pops at one end — last in, first out. · يقوم المكدس بالدفع والإخراج من طرف واحد — آخر يدخل، أول يخرج.
Start with an empty stack. Push 1, then 2, then 3. Then pop once, storing the removed value in top. (top should be 3 and the stack should be [1, 2].) · ابدأ بمكدس فارغ. ادفع 1، ثم 2، ثم 3. ثم أخرج مرة واحدة، واحفظ القيمة المحذوفة في top. (يجب أن يكون top هو 3 والمكدس يجب أن يكون [1, 2].)
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Use a stack to reverse the list items. Push every item onto a stack, then pop them all into result. (result should be [3, 2, 1].) · استخدم مكدساً لـ عكس القائمة items. ادفع كل عنصر onto مكدس، ثم أخرجهم جميعاً إلى result. (يجب أن يكون result هو [3, 2, 1].)
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write top(stack) that returns the top item (the last one) without removing it. If the stack is empty, return None. · اكتب top(stack) التي ترجع العنصر العلوي (الآخر) دون إزالته. إذا كان المكدس فارغاً، ارجع None.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.