0% found this document useful (0 votes)
2 views

Intro of stack

The document discusses the stack data structure, which operates on a Last In First Out (LIFO) principle, detailing its implementation using arrays and linked lists. It explains the push and pop operations, including their algorithms and implementations, as well as the pros and cons of using arrays for stack implementation. Additionally, it covers the application of stacks in checking for balanced parentheses in expressions and lists various applications of stacks in data structures.

Uploaded by

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

Intro of stack

The document discusses the stack data structure, which operates on a Last In First Out (LIFO) principle, detailing its implementation using arrays and linked lists. It explains the push and pop operations, including their algorithms and implementations, as well as the pros and cons of using arrays for stack implementation. Additionally, it covers the application of stacks in checking for balanced parentheses in expressions and lists various applications of stacks in data structures.

Uploaded by

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

Data Structure(CS-6301)

Presented By:-
Dr Suraj Srivastava
Stack
• Stack is a linear data structure that follows the LIFO
(Last In First Out) principle, where it performs all
operations. It performs insertion and deletion
operations on the stack from only one end from the
top of the stack. Inserting a new element on the top of
the stack is known as push operation, and deleting a
data element from the top of the stack is known as pop
operation. You can perform the implementation of the
stack in memory using two data structures: stack
implementation using array and stack implementation
using linked-list.
Stack Implementation Using Array

• In Stack implementation using arrays, it forms


the stack using the arrays. All the operations
regarding the stack implementation using
arrays.
Procedure for Stack Implementation Using Array

Now, you will see how to do a stack implementation using an array:


Push Operation:
Adding an element on the top of the stack is termed a push operation.
Push operation has the following two steps:
• Increment the top variable of the stack so that it can refer to the next
memory location.
• Add a data element at the increment top position.
Stack data structure states an overflow condition when you try to insert
an element into the stack when complete.
Algorithm of push operation:
Push(S,data)
If (top=max-1)
Print(“stack overflow”)
Else
{
top=top+1;
S[top]=data;
}
Implementation of push operation:
void push( int data, int n)
{
If ( top == n)
printf(“overflow”);
else
{
top = top +1;
stack[top] = data;
}
}
Pop Operation:

Removing a data element from the stack data structure is called


a pop operation. The pop operation has two following steps:
1-When the user tries to delete the element from the empty
stack then the condition is said to be a Underflow
2-The topmost variable of the stack is stored in another
variable, and then the value of the top variable will be
decremented by one.
3-The pop operation returns the deleted element that was
stored in another variable as a result.
Stack data structure states an underflow condition when you
try to delete a data element when the stack is already empty.
Algorithm of pop operation:
Pop(S)
If (top=-1)
Print(“stack underflow”)
Else
{x=s[top]
Top=top-1;
Return x;
}
Implementation of pop operation:
int pop()
{
if (top == -1)
{
printf(“underflow condition”);
}
else
{
return stack[top--];
}
}
Pros and Cons of Stack Implementation Using Array

There are various pros and cons of a stack implementation using


an array, they are:
Pros:
• It requires no extra memory to store the pointers in stack
implementation using an array.
• More efficient in terms of time, compared to stack
implementation using linked-list.
Cons:
• The size of the stack is fixed, so it cannot increase and decrease
stack implementation using an array.
• Insertion and deletion in an array are quite difficult as it stores
the elements in consecutive memory locations.
Check for Balanced Brackets in an expression (well-formedness)

Given an expression string exp, write a program to examine


whether the pairs and the orders of “{“, “}”, “(“, “)”, “[“, “]”
are correct in the given expression.
Example:
Input: exp=“[()]{}{[()()]()}”
Output: Balanced
Explanation: all the brackets are well-formed
Input: exp = “[(])”
Output: Not Balanced
Explanation: 1 and 4 brackets are not balanced because
there is a closing ‘]’ before the closing ‘(‘
Check for Balanced Bracket expression using Stack:

The idea is to put all the opening brackets in the


stack. Whenever you hit a closing bracket,
search if the top of the stack is the opening
bracket of the same nature. If this holds then
pop the stack and continue the iteration. In the
end if the stack is empty, it means all brackets
are balanced or well-formed. Otherwise, they
are not balanced
Illustration:
Below is the illustration of the above approach.
Follow the steps mentioned below to implement the idea:

• Declare a character stack (say temp).


• Now traverse the string exp.
– If the current character is a starting bracket ( ‘(‘ or ‘{‘ or
‘[‘ ) then push it to stack.
– If the current character is a closing bracket ( ‘)’ or ‘}’ or
‘]’ ) then pop from the stack and if the popped character
is the matching starting bracket then fine.
– Else brackets are Not Balanced.
– After complete traversal, if some starting brackets are
left in the stack then the expression is Not balanced,
else Balanced.
What is the application of stack balanced parentheses?

• A stack is a data structure that stores elements in a last-in, first-out (LIFO)


order. It is used to check for balanced parentheses because it allows us to
keep track of opening and closing parentheses in an expression and ensure
that they are properly matched and nested.
• Therefore, a string containing bracket characters is said to be balanced if: A
matching opening bracket occurs to the left of each corresponding closing
bracket. Brackets enclosed within balanced brackets are also balanced. It
does not contain any non-bracket characters
• Following is the various Applications of Stack in Data Structure:
• Evaluation of Arithmetic Expressions.
• Backtracking.
• Delimiter Checking.
• Reverse a Data.
• Processing Function Calls.

You might also like