A stack is an abstract data type or ordered collection of items where the addition of new items and the removal of existing items always takes place at the same end. This end is commonly referred to as the "top". The end opposite the top is known as the "base".
The base of the stack is significant since items stored in the stack that are closer to the base represent those that have been in the stack the longest. The most recently added item is the one that is in position to be removed first. This ordering principle is sometimes called LIFO, last-in first-out. It provides an ordering based on length of time in the collection. Newer items are near the top, while older items are near the base.
/* Push */ If (TOP == MAX) Then [Check for overflow] Print: Overflow Else Set TOP = TOP + 1 [Increment TOP by 1] Set STACK[TOP] = ITEM [Assign ITEM to top of STACK] Print: ITEM inserted [End of If] Exit /* Pop */ If (TOP == 0) Then [Check for underflow] Print: Underflow Else Set ITEM = STACK[TOP] [Assign top of STACK to ITEM] Set TOP = TOP - 1 [Decrement TOP by 1] Print: ITEM deleted [End of If] Exit
Learn more about Stack