This document discusses stacks as a linear data structure that follows the LIFO (Last-In First-Out) principle. Key points include:
- Stacks allow insertion and deletion from one end only (the top) through PUSH and POP operations.
- Common applications of stacks include reversing strings, checking validity of expressions with nested parentheses, and converting infix notation to postfix notation for arithmetic expressions.
- The document provides examples and algorithms for common stack operations like insertion, deletion, and display.
Overview of linear and non-linear data structures, focusing on arrays, queues, and stacks.
Definition of linear data structures - data arranged in a sequence, can be traversed easily.
Definition and characteristics of stacks as LIFO data structures. Operations like push and pop defined, along with their impact on 'Top'.
Examples of stack operations, including PUSH for inserting and POP for deleting items.
Algorithms for insertion (PUSH), deletion (POP), and display operations in stacks are illustrated.
Practical uses for stacks: reversing strings, checking valid expressions, and evaluating arithmetic expressions.Explanation of infix and postfix notation alongside the process of converting infix expressions to postfix.
Closing remarks on the topic of stacks and their significance in data structures.
What is LinearData Structure
In linear data structure, data is arranged
in linear sequence.
Data items can be traversed in a single
run.
In linear data structure elements are
accessed or placed in contiguous(together
in sequence) memory location.
4.
WHAT Is
Astack is called a last-in-first-out (LIFO)
collection. This means that the last thing
we added (pushed) is the first thing that
gets pulled (popped) off.
A stack is a sequence of items that are
accessible at only one end of the sequence.
5.
Definition of Stack:
Stacksin Data Structures is a linear type
of data structure that follows the LIFO
(Last-In-First-Out) principle and allows
insertion and deletion operations from
one end of the stack data structure, that
is top.
If any Element inserted then Top will be
increased by 1 & if any Element deleted
then Top will be decreased by 1.
Top=Top+1 (Insertion)
Top=Top-1 (Deletion)
PUSH : Itis used to insert items into the stack.
POP: It is used to delete items from stack.
TOP: It represents the current location of data
in stack.
9.
ALGORITHM OF INSERTIONIN
STACK: (PUSH)
1. Insertion(a,top,item,max)
2. If top=max then
print ‘STACK OVERFLOW’
exit
else
3. top=top+1
end if
4. a[top]=item
5. Exit
10.
ALGORITHM OF DELETIONIN
STACK: (POP)
1. Deletion(a,top,item)
2. If top=0 then
print ‘STACK UNDERFLOW’
exit
else
3. item=a[top]
end if
4 . top=top-1
5. Exit
11.
ALGORITHM OF DISPLAYIN
STACK:
1.Display(top,i,a[i])
2.If top=0 then
Print ‘STACK EMPTY’
Exit
Else
3.For i=top to 0
Print a[i]
End for
4.exit
12.
APPLICATIONS OF STACKSARE:
I. Reversing Strings:
• A simple application of stack is reversing strings.
To reverse a string , the characters of string are
pushed onto the stack one by one as the string
is read from left to right.
• Once all the characters
of string are pushed onto stack, they are
popped one by one. Since the character last
pushed in comes out first, subsequent pop
operation results in the reversal of the string.
13.
For example:
To reversethe string ‘REVERSE’ the string is
read from left to right and its characters are
pushed . LIKE:
onto a stack.
14.
II. Checking thevalidity of an expression
containing nested parenthesis:
• Stacks are also used to check whether a given
arithmetic expressions containing nested
parenthesis is properly parenthesized.
• The program for checking the validity of an
expression verifies that for each left parenthesis
braces or bracket ,there is a corresponding
closing symbol and symbols are appropriately
nested.
III. Evaluating arithmeticexpressions:
INFIX notation:
The general way of writing arithmetic
expressions is known as infix notation.
e.g, (a+b)
PREFIX notation:
e.g, +AB
POSTFIX notation:
e.g: AB+