Operations in Stack

Stack operations may involve initializing the stack, using it and then de-initializing it.

Apart from these basic stuffs, a stack is used for the following two primary operations:

  • Push: Pushing (storing) an element on the stack.
  • Pop: Removing (accessing) an element from the stack.

To use a stack efficiently, we need to check the status of stack as well.
For the same purpose, the following functionality is added to stacks:

  • Peek: Get the top data element of the stack, without removing it.
  • IsFull: Check if stack is full.
  • IsEmpty: Check if stack is empty.

At all times, we maintain a pointer to the last PUSHed data on the stack.

As this pointer always represents the top of the stack, hence named top.
The top pointer provides top value of the stack without actually removing it.


Peek

Algorithm of peek() function:

begin procedure peek

if top more than 1
	return item from top
else
	return stack empty
endif

end procedure


IsFull

Algorithm of isfull() function:

begin procedure isfull

if top equals to MAXSIZE
	return true
else
	return false
endif

end procedure


IsEmpty

Algorithm of isempty() function:

begin procedure isempty

if top less than 1
	return true
else
	return false
endif

end procedure


Push

Algorithm of push() function:

begin procedure push

if top less than MAXSIZE
	increase value of top
	add item to top
else  
	return stack full
endif

end procedure


Pop

Algorithm of pop() function:

begin procedure pop

if top more than 1
	remove item from top
	decreaase value of top
else
	return stack empty
endif

end procedure


Introduction to Stack Previous Next Implementation of Stack Operations