Tuesday, September 20, 2016

Implementation of stack (Dynamic and Static)

Stack can be implemented in two ways:

  • Dynamic or linked list implementation
  • Static or array implementation  

Static implementation of Stack:

                                     In Static implementation of Stack, the size of the stack should be predefined or declared, hence it is called static because of it's static size. In this implementation, we must know the number of data elements that are going to be inserted in the stack, or else:

If there are more number of data elements than our defined size, then the data cannot be inserted into the stack, hence loss of data.

If there are less number of data elements than our defined size, then it will result to the wastage of memory space.

Hence, We can implement this way if the number of data to be inserted in the stack is well-defined and constant.

Dynamic implementation of stack: 

                                        In this implementation of stack, the size of the stack is not pre-defined during the compile time. As we insert and remove elements from the stack, size of the stack can grow and shrink accordingly. Memory space are allocated to an data element only when the data is inserted and is deallocated when the data is popped out of the stack. Due to this, The dynamic implementation is effective in case if the number of data to be entered is not well-defined.

No comments:

Post a Comment