Navneet Singh Once a wise man said me, 'words are mighter than a sword'. Living up to the wise man wisdom.

Introduction To Stacks

Introduction To Stacks

In this blog post we will cover the following concepts

  • Introduction to stacks
  • Basic operations on stacks (Push And Pop)
  • Pseudocode related to stacks
  • Complexity of operations

Introduction To Stacks

In order to explain what a stack is I’ll be taking a real time example. Consider a large number of trays are placed on over the other. Now you want to add your tray to the already placed trays, so you’ll put one at the top. Now suppose you want to take a tray from the collection, you’ll take the topmost available tray. This is how stacks work.

Stacks uses array as data structure which are dynamic sets in which the element deleted from the set is the last one inserted and a new element added is added at the end of the array. It follows LIFO property, i.e the last element added is the first element to get out of the array.

Basic Operations On Stacks

The insert operation on a stack is often called PUSH, and the delete operation is called POP. Consider the following example.

In the above example the new array element is inserted into last position using PUSH operations and deleted from the last position using POP operation.

Pseudocode

In the above pseudocode we implemented what we learnt. If we want to delete the last element of stack, we use POP operation, in which we check if the stack is empty or not. If its not empty the last element is deleted and last index is shifted to the previous index. Similarly, if we push a value to the stack, the last index is moved to the next index and new element is added at that place.

Complexity Of Stacks

Each of the stack operation takes O (1) time.

That’s all folks, we are done with the basic operations of stack. In the next blog we are going to have a look at Queues and its basic opeartions.

comments powered by Disqus