# Stack: How to Implement and Manipulate It in Different Languages

## Introduction

A stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO (Last In First Out) or FILO (First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first, comes out last.

A stack can be visualized as a pile of objects, such as plates, books, or cards. The object that is placed on the top of the pile is the first one to be removed, and the object that is placed at the bottom of the pile is the last one to be removed. The top of the stack is the only position where we can access, insert, or delete an element.

## stack

Stacks are useful for many applications, such as evaluating expressions, function calls, backtracking, and undo/redo operations. These applications require storing and retrieving data in a reverse order, which can be easily achieved using stacks.

## Basic operations

A stack supports four basic operations: push, pop, peek, and isEmpty. Some implementations may also support a fifth operation: isFull. These operations are defined as follows:

Push: This operation adds an element to the top of the stack. If the stack is full, then it is said to be an overflow condition.

Pop: This operation removes and returns the element from the top of the stack. If the stack is empty, then it is said to be an underflow condition.

Peek: This operation returns the element from the top of the stack without removing it. It can be used to check the current element in the stack.

IsEmpty: This operation returns true if the stack is empty, else false. It can be used to check whether there are any elements in the stack or not.

IsFull: This operation returns true if the stack is full, else false. It can be used to check whether there is any space left in the stack or not.

## Implementation

A stack can be implemented using different data structures, such as arrays or linked lists. The choice of data structure depends on various factors, such as memory usage, performance, and ease of implementation. Here we will discuss two common ways of implementing a stack: using array and using linked list.

## Using array

An array is a fixed-size container that stores elements in a contiguous block of memory. To implement a stack using an array, we need to maintain two variables: an array to store the elements and an integer to keep track of the top index of the stack. The top index initially points to -1, indicating that the stack is empty. When we push an element into the stack, we increment the top index and store the element at that position in the array. When we pop an element from the stack, we return the element at the top index and decrement it. The peek operation simply returns the element at the top index without changing it. The isEmpty operation checks whether the top index is -1 or not. The isFull operation checks whether the top index is equal to the size of the array minus one or not.

The advantage of using an array to implement a stack is that it is simple and fast. The disadvantage is that it has a fixed size and cannot grow or shrink dynamically. If we try to push an element into a full stack or pop an element from an empty stack, we will encounter an overflow or underflow error respectively.

## Using linked list

A linked list is a dynamic-size container that stores elements in nodes that are linked together by pointers A linked list is a dynamic-size container that stores elements in nodes that are linked together by pointers. To implement a stack using a linked list, we need to maintain a pointer to the head node of the list, which represents the top of the stack. The head pointer initially points to null, indicating that the stack is empty. When we push an element into the stack, we create a new node with the element as its data and the head pointer as its next pointer, and then update the head pointer to point to the new node. When we pop an element from the stack, we return the data of the head node and update the head pointer to point to its next node. The peek operation simply returns the data of the head node without changing it. The isEmpty operation checks whether the head pointer is null or not. The isFull operation is not applicable for a linked list implementation, as it can grow or shrink dynamically depending on the available memory.

The advantage of using a linked list to implement a stack is that it has no fixed size and can accommodate any number of elements. The disadvantage is that it requires extra space for storing the pointers and it may be slower than an array implementation due to pointer manipulation and memory allocation/deallocation.

[Ahrefs Keywords Explorer](^1^): This tool runs on a database of billions of keywords and lets you filter for keywords with low search volume and traffic potential. You can also use the "Questions" toggle to find long-tail keywords in the form of questions.

[Keyword Tool](^2^): This tool uses Google Autocomplete to generate over 750 long-tail keyword suggestions for any search term. You can also filter by language, country, and platform.

[Simple Long-Tail Keyword Generator](^3^): This tool allows you to enter your primary keywords and generate a list of keyword permutations based on modifiers, prefixes, and suffixes.

stack overflow error

stack exchange sites

stack data structure in java

stack vs heap memory

stack on gun safe

stack and tilt golf swing

stack the states game

stack skills review

stack on sentinel 18 gun safe

stack on 14 gun cabinet

stack effect in buildings

stack social coupon code

stack of books clipart

stack on elite 40 gun safe

stack on 22 gun safe

stack on tactical gun cabinet

stack of money png

stack of pancakes drawing

stack on biometric safe

stack on 8 gun cabinet

stack of papers gif

stack of books cake

stack on quick access safe

stack of money emoji

stack of books svg

stack on fire resistant safe

stack of pancakes calories

stack of money tattoo

stack of books transparent background

stack on 24 gun safe with electronic lock

stack of money clipart black and white

stack of books vector free download

stack on total defense 40 gun safe

stack of pancakes clipart black and white

stack of money wallpaper hd

stack on 10 gun cabinet dimensions

stack of books coloring page

stack on 18 gun convertible cabinet review

stack of money cake topper

stack on 36 gun safe weight

stack of books png images

stack on 16 gun safe with combination lock

stack of pancakes with syrup and butter

stack of money in hand drawing

stack on 22 gun security cabinet review

stack of books silhouette png

stack on 14 gun fire resistant security safe with electronic lock fs 14 mb e matte black review

## Examples

Now that we have learned how to implement a stack using different data structures, let us see some examples of how stacks can be used to solve various problems.

## Evaluation of expressions

A common application of stacks is to evaluate arithmetic expressions that involve operators and operands. For example, given an expression like "2 + 3 * 4 - 5 / (6 + 7)", how can we compute its value? One way is to use two stacks: one for storing operands and one for storing operators. We can scan the expression from left to right and perform the following steps:

If we encounter an operand, we push it into the operand stack.

If we encounter an operator, we compare its precedence with the top operator in the operator stack. If the current operator has higher precedence, we push it into the operator stack. If the current operator has lower or equal precedence, we pop two operands from the operand stack and one operator from the operator stack, apply the operator on the operands, and push the result back into the operand stack. We repeat this step until either the operator stack is empty or the current operator has higher precedence than the top operator in the operator stack.

If we encounter an opening parenthesis '(', we push it into the operator stack.

If we encounter a closing parenthesis ')', we pop operators from the operator stack and apply them on operands from the operand stack until we reach an opening parenthesis '('. We then pop and discard the opening parenthesis from the operator stack.

When we reach the end of the expression, we pop operators from the operator stack and apply them on operands from the operand stack until the operator stack is empty. The final value in the operand stack is the result of the expression.

For example, let us evaluate the expression "2 + 3 * 4 - 5 / (6 + 7)" using this method. We will use two stacks: S1 for operands and S2 for operators. The table below shows the steps and the state of the stacks at each step.

Step Expression S1 S2 Action --- --- --- --- --- 1 2 + 3 * 4 - 5 / (6 + 7) Scan from left to right 2 2 + 3 * 4 - 5 / (6 + 7) 2 Push operand into S1 3 2 + 3 * 4 - 5 / (6 + 7) 2 + Push operator into S2 4 2 + 3 * 4 - 5 / (6 + 7) 2,3 + Push operand into S1 5 2 + 3 * 4 - 5 / (6 + 7) 2,3 +,* Push operator into S2 6 2 + 3 * 4 - 5 / (6 + 7) 2,3,4 +,* Push operand into S1 7 2 + 3 * 4 - 5 / (6 + 7) 2,12 - Pop two operands and one operator from stacks, apply *, push result into S1, push operator into S2 8 2 + 3 * 4 - 5