Stacks in Data Structures

21 September, 2018

Related Blogs

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.   1     The two basic operation associated with stack are
  1. “Push” is the term used to insert an element into a stock.
  2. “Pop” is the term used to delete an element into a stack.
  1. A stack of dishes
  2. A stack of coins
  3. A stack of folded towels
  4. A stack of bills
  5. 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.
Top =Top-1;
  • A variable “max size “, which gives the maximum number of elements that can be held by the stack.