0% found this document useful (0 votes)
7 views5 pages

Stack

A stack is a data structure that follows the Last-In-First-Out (LIFO) principle, where elements are added and removed from the top using push and pop operations. Key operations include checking if the stack is empty or full, peeking at the top element, and counting elements. Stacks have various applications such as balancing symbols, string reversal, UNDO/REDO functionality, and memory management.

Uploaded by

anmolkhatanauk52
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Stack

A stack is a data structure that follows the Last-In-First-Out (LIFO) principle, where elements are added and removed from the top using push and pop operations. Key operations include checking if the stack is empty or full, peeking at the top element, and counting elements. Stacks have various applications such as balancing symbols, string reversal, UNDO/REDO functionality, and memory management.

Uploaded by

anmolkhatanauk52
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

STACK

A stack is a basic data structure in computer science that adheres to the Last-In-First-Out (LIFO)
rule.

It is comparable to a stack of books where the last book added is the first one to be taken out.

A stack is a collection of components that can be accessed using the push and pop operations.

While the pop operation takes the top element out of the stack, the push operation adds an
element to the top of the stack.

The topmost element can also be examined without deleting it due to the peek procedure.

Working of Stack
Stack works on the LIFO pattern. As we can observe in the below figure there are five
memory blocks in the stack; therefore, the size of the stack is 5.

Suppose we want to store the elements in a stack and let's assume that stack is empty.
We have taken the stack of size 5 as shown below in which we are pushing the elements
one by one until the stack becomes full.

 When we perform the delete operation on the stack, there is only one way for entry and
exit as the other end is closed.
 It follows the LIFO pattern, which means that the value entered first will be removed last.
 In the above case, the value 5 is entered first, so it will be removed only after the deletion
of all the other elements.

** Reference from E –Source


Standard Stack Operations
The following are some common operations implemented on the stack:

o push(): When we insert an element in a stack then the operation is known as a


push. If the stack is full then the overflow condition occurs.
o pop(): When we delete an element from the stack, the operation is known as a
pop. If the stack is empty means that no element exists in the stack, this state is
known as an underflow state.
o isEmpty(): It determines whether the stack is empty or not.
o isFull(): It determines whether the stack is full or not.'
o peek(): It returns the element at the given position.
o count(): It returns the total number of elements available in a stack.
o change(): It changes the element at the given position.
o display(): It prints all the elements available in the stack.

PUSH operation
The steps involved in the PUSH operation is given below:

o Before inserting an element in a stack, we check whether the stack is full.


o If we try to insert the element in a stack, and the stack is full, then
the overflow condition occurs.
o When we initialize a stack, we set the value of top as -1 to check that the stack is
empty.
o When the new element is pushed in a stack, first, the value of the top gets
incremented, i.e., top=top+1, and the element will be placed at the new position
of the top.
o The elements will be inserted until we reach the max size of the stack.

** Reference from E –Source


POP operation
The steps involved in the POP operation is given below:

o Before deleting the element from the stack, we check whether the stack is empty.
o If we try to delete the element from the empty stack, then the underflow condition
occurs.
o If the stack is not empty, we first access the element which is pointed by the top
o Once the pop operation is performed, the top is decremented by 1, i.e., top=top-
1.

** Reference from E –Source


Applications of Stack

o Balancing of symbols: Stack is used for balancing a symbol. For example, we have
the following program: As we know, each program has an
opening and closing braces; when the opening braces come, we push the braces in
a stack, and when the closing braces appear, we pop the opening braces from the
stack. Therefore, the net value comes out to be zero. If any symbol is left in the
stack, it means that some syntax occurs in a program.

o String reversal: Stack is also used for reversing a string.


First, we push all the characters of the string in a stack until we reach the null
character. After pushing all the characters, we start taking out the character one by
one until we reach the bottom of the stack.
o UNDO/REDO: It can also be used for performing UNDO/REDO operations. For
example, we have an editor in which we write 'a', then 'b', and then 'c'; therefore,
the text written in an editor is abc. So, there are three states, a, ab, and abc, which
are stored in a stack. There would be two stacks in which one stack shows UNDO

** Reference from E –Source


state, and the other shows REDO state.
If we want to perform UNDO operation, and want to achieve 'ab' state, then we
implement pop operation.
o Recursion: The recursion means that the function is calling itself again. To
maintain the previous states, the compiler creates a system stack in which all the
previous records of the function are maintained.
o DFS(Depth First Search): This search is implemented on a Graph, and Graph uses
the stack data structure.
o Backtracking: If we are moving in a particular path, and we realize that we come
on the wrong way. In order to come at the beginning of the path to create a new
path, we have to use the stack data structure.
o Expression conversion: Stack can also be used for expression conversion. This is
one of the most important applications of stack. The list of the expression
conversion is given below:
o Infix to prefix
o Infix to postfix
o Prefix to infix
o Prefix to postfix
Postfix to infix

o Memory management: The stack manages the memory. The memory is assigned
in the contiguous memory blocks. The memory is known as stack memory as all
the variables are assigned in a function call stack memory.. When the function is
created, all its variables are assigned in the stack memory. When the function
completed its execution, all the variables assigned in the stack are released.

** Reference from E –Source

You might also like