3. What is Linear Data Structure
In linear data structure, data is arranged
in linear sequence.
In linear data structure elements are
placed in contiguous(together in
sequence) memory location.
1/2/2019 Prepared: Muhammad Farhan 3
4. WHAT Is
A stack 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.
1/2/2019 Prepared: Muhammad Farhan 4
6. Operations that can be performed on
STACK:
PUSH.
POP.
1/2/2019 Prepared: Muhammad Farhan 6
7. PUSH : It is 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.
In stack related algorithms TOP initially point 0, index of elements
in stack is start from 1, and index of last element is MAX.
1/2/2019 Prepared: Muhammad Farhan 7
8. ALGORITHM OF INSERTION IN STACK:
(PUSH)
Insertion( a, top, item,max)
Let ‘a’ be the stack. ‘top’ is an integer variable
that indicate the top of the stack. Then the
algorithm to push the item onto the stack is as
follows:
1.If top=max then
print ‘STACK OVERFLOW’
else
2.top=top+1 end if
3.a[top]=item
4.Exit
1/2/2019 Prepared: Muhammad Farhan 8
9. ALGORITHM OF DELETION IN STACK: (POP)
Deletion( a, top, item)
Let ‘a’ be the stack. ‘top’ is an integer variable
that indicate the top of the stack. Then the
algorithm to pop the item from the the stack is
as follows:
1. If top=0 then
print ‘STACK UNDERFLOW’
else
3. Delete a[top] end if
4. top=top-1
5. Exit
1/2/2019 Prepared: Muhammad Farhan 9
10. APPLICATIONS OF STACKS ARE:
1. Back/Forward option in browsers.
2. Expression Evaluation
1/2/2019 Prepared: Muhammad Farhan 10
11. Evaluating arithmetic
expressions:
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+
1/2/2019 Prepared: Muhammad Farhan 11
12. Infix to Postfix conversion
1. The infix expression is scanned from left to right until end of
the expression
2. Whenever an operand is encountered, it is added to the
output
3. Each time an operator is read, the stack is repeatedly poped
and operands are passed to the output, until an operator is
reached that has a lower precedence than the most recently
read operator . The most recently read operator is then
pushed onto the stack .
4. When end of the infix expression is reached, all operators
remaining in the stack are popped and passed to the output
in the same sequence.
13. 5. Parentheses can be used in the infix expression but these
are used in the postfix expression. During conversion
process, parentheses are treated as operators that have
higher precedence than any other operator. The left
parenthesis is pushed into the stack when encountered.
6. The right parentheses is never pushed to the stack . The
left parentheses is popped only when right parenthesis is
encountered . The parentheses are not passed to the
output postfix expression, they are discarded.
7. When end of expression is reached , then all operators
from stack are popped and added to the output