In this tutorial, we will discuss about stacks in data structures.
A stack is an ordered list in which items may be added or removed only at one end, called the “top” of the stack.
The last item to be added to a stack is the first item to be removed .Accordingly, stacks are also called Last- in-first- out (LIFO) lists or First- in –out (FIFO) lists.
The two basic operation associated with stack are
- “Push” is the term used to insert an element into a stock.
- “Pop” is the term used to delete an element into a stack.
- A stack of dishes
- A stack of coins
- A stack of folded towels
- A stack of bills
- A railway system for shunting cars.
- Stack may be represented in a computer in various ways, usually by means of a one –dimensional array and linked list.
- “Top” is variable or a pointer, which points top most element of the stack.
- Initially when the stack is empty, “Top has a value of “-1” and when the stack contains a single element.
- “Top” has a value of “0” and when the stack contains two elements, “Top” has value of “1” and so on.
- The following is the condition to test the stack is full or not( Top== maxsize-1)
- Each time a new element is inserted in the stack, the top is incremented by “one” before the elements is placed on the stack.
- The following is the condition to kept the stack is empty or not.
- The “Top “is decremented by one each time a deletion is made from the stack.
- A variable “max size “, which gives the maximum number of elements that can be held by the stack.