The stack has multiple types. In this section, we will discuss the first type, which represents the stack as an array that operates based on the LIFO (Last In, First Out) principle — the last element inserted is the first one to be removed. اخر عنصر بيدخل هو اول عنصر بيخرج

الستاك هو عبارة عن وعاء وبيستوعب مجموعة من العناصر وكل عنصر يكون مرتب فوق العنصر اللي قبله. وقت كنا نعرف المصفوفة نعطيها حجم معين والستاك أيضا نفس الشي ولكن بخواص محددة

Array and Stack

Array and Stack

Adding an element to the stack

Adding an element to the stack

Printing the first element

Printing the first element


🔍 Storing, accessing, deleting elements in Stack

طيب شو اللي بساعدنا بعملية الأضافة، كيف بدنا نضمن انو نضيف بعد اخر عنصر وليس قبله؟ بالستاك في شي اسمه top وهو مؤشر أو متغير يقلنا وين نحنا حاليا بالستاك، بحيث بس بدنا نضيف عنصر نضمن بعد اضافته انو نغير مكان تأشيره لاخر عنصر

top is the index of the stack. Initial value of the top = -1 when the stack is empty.

printing the first element with top pointer

printing the first element with top pointer

Adding more elements, now the stack is full

Adding more elements, now the stack is full

Adding an element, and top moves towards up

Adding an element, and top moves towards up

When we delete element, the last added one will be deleted automatically, and top will decrease it’s value.

When we delete element, the last added one will be deleted automatically, and top will decrease it’s value.

وقت حذف أو طباعة أي عنصر رح يتأثر فيها أول عنصر على قمة الستاك، واللي هو اخر واحد تم اضافته على الستاك.

التوب مكانها ما بيتخربط او ما عندها فرصة مثلا بدل ما تنزل لل20 تروح لل15. يعني مثلا لو بدنا نوصل لاول عنصر تم اضافته على الستاك لازم نمسح كل اللي قبله لنوصله. مافي طريقة تانية


🔥 Applications on Stack


Array-Based Implementation of ADT (Abstract Data Type) Stack

  1. push(val) لادخال قيمة
  2. pop() لحذف اخر عنصر تم اضافته على الستاك
  3. top or getTop() لنرجع قيمة العنصر اللي هو بقمة الستاك