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) הוא קבוצת נתונים יחד עם סט של פעולות עליו.
- אתה משתמש בפעולות ומתעלם מכך שהקוד מבונה בפנים.
- מאגר (stack), תור (queue) ו-רשימה מקשרת (linked list) הם כולם ADTs.
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 עם רשימה
- ניתן לבנות מאגר ממטריצת Python.
- 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]. - המאגר הוא rakk כאשר
len(stack) == 0. - ביצוע pop על מאגר ריק הוא טעות, ולכן יש לבדוק קודם.
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.
טעויות נפוצות
- המאגר הוא היכנס אחרון, יציאה ראשונה: push לעליון, pop מעליון.
- בדוק שהוא לא ריק לפני ביצוע pop.
Now you try
- Use a list as your stack (
appendto push,popto remove). - Press Check answer to test your code.
כעת תנסו בעצמכם
- השתמש ברשימה כמאגר שלך (
appendלהוספה,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. דחוף כל פריט לערומה, ואז הוצא אותם כולם ל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. · לחץ על הרץ כדי לראות את התוצא כאן.