0% found this document useful (0 votes)
65 views35 pages

Elementary Data "Structures": - Arrays - Lists - Stacks - Queues - Trees

The document summarizes common data structures including arrays, lists, stacks, queues and trees. It then focuses on stacks, describing them as last-in, first-out data structures where only the top element can be accessed. Stacks have push and pop operations. Examples of stack applications include checking balanced parentheses, infix/postfix/prefix notation conversion, and recursion. Implementing stacks involves declaring a character or integer array and tracking the top index.

Uploaded by

Sahil Nagar
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)
65 views35 pages

Elementary Data "Structures": - Arrays - Lists - Stacks - Queues - Trees

The document summarizes common data structures including arrays, lists, stacks, queues and trees. It then focuses on stacks, describing them as last-in, first-out data structures where only the top element can be accessed. Stacks have push and pop operations. Examples of stack applications include checking balanced parentheses, infix/postfix/prefix notation conversion, and recursion. Implementing stacks involves declaring a character or integer array and tracking the top index.

Uploaded by

Sahil Nagar
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/ 35

Elementary Data “Structures”

head
• Arrays
• Lists
• Stacks 2
1

• Queues 3 4
7 8
• Trees 5 6

F R

In some languages these are basic data types – in others they need
to be implemented
1
Stacks
Stack
• A stack is a data structure that stores data in such a way that the last piece of data stored, is the first one
retrieved

• also called last-in, first-out

• Only access to the stack is the top element

• consider trays in a cafeteria

• to get the bottom tray out, you must first remove all of the elements above

• This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element
which is placed (inserted or added) last, is accessed first. In stack terminology, insertion
operation is called PUSH operation and removal operation is called POP operation.
Implementation
Implementation
Exercise: Stacks
• Describe the output of the following series of stack operations
• Push(8)
• Push(3)
• Pop()
• Push(2)
• Push(5)
• Pop()
• Pop()
• Push(9)
• Push(1)
Applications of Stack
• Check for balanced parentheses in an expression.
• Infix, Postfix and Prefix Interconversion.
• Recursion
Check for balanced parentheses in an expression
Steps:
• Declare a character stack S.
• Now traverse the expression 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 stack and
if the popped character is the matching starting bracket then fine else parenthesis are
not balanced.

• After complete traversal, if there is some starting bracket left in stack then
“not balanced”
Exercise:
• Check if following parentheses is balanced or not?
[{({}[]({})}]
Infix, Postfix and Prefix Interconversion
/
Postfix Evaluation
Infix to Prefix Conversion
➢Step 1: Reverse the infix expression i.e. A+B*C will become C*B+A.
Note while reversing each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘.

➢Step 2: Obtain the postfix expression of the modified expression i.e.


CB*A+.

➢Step 3: Reverse the postfix expression. Hence in our example prefix is


+A*BC.
Example : (A – B / C) * (A / K - L)
Infix to Prefix : (A – B / C) * (A / K - L) (Reverse ) Find Postfix : (L – K / A) * (C / B -A )
Expression Stack Output
( (
L ( L
- (- L
K (- LK
/ (-/ LK
A (-/ LKA
) Empty LKA/-
* * LKA/-
( *( LKA/-
C *( LKA/- C
/ *(/ LKA/- C
B *(/ LKA/- CB
- *(- LKA/- CB/
A *(- LKA/- CB/A
) * LKA/- CB/A-
Empty LKA/- CB/A-*
Example : (A – B / C) * (A / K - L)
Prefix : *-A/BC-/AKL
Postfix to Infix Conversion
Prefix to Infix Conversion
Postfix to Prefix Conversion
Prefix to Postfix Conversion

You might also like