Stack
Stack
This example allows you to perform operations from one end only, like
when you insert and remove new books from the top of the stack.
It means insertion and deletion in the stack data structure can be done
only from the top of the stack. You can access only the top of the stack
at any given point in time.
Inserting a new element in the stack is termed a push operation.
Removing or deleting elements from the stack is termed pop operation.
Following operations can be performed in stack:
push: Inserts a new element at the top of the stack, above its current top
element.
pop: Removes the top element on the stack, thereby decrementing its
size by one.
isEmpty: Returns true if the stack is empty, i.e., its size is zero;
otherwise, it returns false.
isFull: Returns true if the stack is full, i.e., its size has reached
maximum allocated capacity; otherwise, it returns false.
peek: Returns the top element present in the stack without modifying
the stack.
size: Returns the count of elements present in the stack.
Push Operation
Push operation involves inserting new elements in the stack. Since you
have only one end to insert a unique element on top of the stack, it
inserts the new element at the top of the stack.
Pop Operation
Pop operation refers to removing the element from the stack again since you have
only one end to do all top of the stack. So removing an element from the top of the
stack is termed pop operation.
isFull()
isFull function is used to check whether or not a stack is empty.
The implementation of the isFull() function is as follows:
Bool isFull()
{
if(top == maxsize)
return true;
else
return false;
}
isEmpty()
Bool isEmpty()
{
if(top = = -1)
return true;
else
return false;
}
Push Operation
Pop Operation
void pop()
if (top < 0) {
return;
void display()
{
if (top < 0)
}}
To reverse a word - Put all the letters in a stack and pop them out.
Because of the LIFO order of stack, you will get the letters in reverse
order.
In compilers - Compilers use the stack to calculate the value of
expressions like 2 + 4 / 5 * (7 - 9) by converting the expression to prefix
or postfix form.
In browsers - The back button in a browser saves all the URLs you
have visited previously in a stack. Each time you visit a new page, it is
added on top of the stack. When you press the back button, the current
URL is removed from the stack, and the previous URL is accessed.