Professional Documents
Culture Documents
STACKS VS ARRAYS
An array is a contiguous block of memory. A stack is a first-in-last-out data structure with access only to the top
of the data.
Since many languages does not provide facility for stack, it is backed by either arrays or linked list.
The values can be added and deleted on any side from an array.
But in stack, insertion and deletion is possible on only one side of the stack. The other side is sealed. Eg: a[10] array a[10] - stack
STACK OPERATIONS
The bottom of a stack is a sealed end. Stack may have a capacity which is a limitation on the number of elements in a stack. The operations
on stack are
Push: Places an object on the top of the stack. Pop: Removes an object from the top of the stack.
a (i)stack
STACK OPERATIONS
B E D A C
STACK IMPLEMENTATION
Stack
languages. Stack is implemented using arrays or linked lists. Let S be a stack, n be the capacity, x be the element to be pushed, then push and pop will be given as
S[1:6]
Pop(S)
a e
d Top=5
Top=6
Pop(S)
Top=0
STACK APPLICATIONS
Recursion handling Evaluation of expression Conversion of infix to postfix expression Computation of postfix expression Parenthesis handling Backtracking Conversion of decimal to other number system
Maze tracer
Undo operations
RECURSION HANDLING
Without stack, recursion is difficult Compiler automatically uses stack data structure while handling recursion. All computer needs to remember for each active function call, values of arguments & local variables and the location of the next statement
starts executing the code for the internal method call. When that
method finally returns, we pop our place from the stack and continue executing.
Eg: int f(int n) { int k,r; if(n==0) return 0; k=n*n; r=f(n-1); Return k+r; } n k r 3 3 9 . 3 2 9 4 . . 3 2 1 9 4 1 . . .
n k r
3 2 1 9 4 1 . . 0
3 2 9 4 . 1
3 9 5
Ans: 14
PARENTHESIS CHECKING
Procedure check() Declare a character stack S. Now traverse the expression. a) If the current character is a starting bracket then push it to stack.
Eg: [a+(b*c)+{(d-e)}]
[ [ ( [ [ { Push [ Push ( ) and ( matches, Pop ( Push { Push ( matches, pop (
[ { (
[ { [
contain an operator.
(
A * B ) + C $
$
$ $ $ $ $ $
(
( ( ( * * AB AB* + + AB*C AB*C+ A
Push (
Print A Push(*) Print B Pop *,( print * Push + Print C Pop +
procedure eval(E)
x=getnextchar(E); case x:
Eg: AB*C+
Element A B * A A B 6
Stack
Action Push A Push B Pop A and B, A*B, push 6 Push C Pop C and 6, C+6, push 11 Pop
C + $
6 C 11
Result:11
BACKTRACKING
Backtracking is a simple, elegant, recursive technique which can be put to a variety of uses. You start at the root of a tree, the tree probably has some good and bad leaves. You want to get to a good leaf. At each node, you choose one of its children to move to, and you keep this up in a stack until you get to a leaf. Suppose you get to a bad leaf. You can backtrack to continue the search for a good leaf by revoking your most recent choice, and trying out the next option in that set of options. If you run out of options, revoke the choice that got you here, and try another choice at that node. If you end up at the root with no options left, there are no good leaves to be
found.
Eg:
Starting at Root, your options are A and B. You choose A. At A, your options are C and D. You choose C. C is bad. Go back to A. At A, you have already tried C, and it failed. Try D. D is bad. Go back to A. At A, you have no options left to try. Go back to Root. At Root, you have already tried A. Try B. At B, your options are E and F. Try E. E is good.
At last the elements are popped out of the stack to give the result.
Eg. 84 1010100 in binary
124 in octal
54 in hexadecimal
84 / 2 = 42 42 / 2 = 21 21 / 2 = 10 10 / 2 = 5 5/2=2 2/2=1
0 0 1 0 1 0 1
push
1
0 1 0 pop 1010100
1
0 0
84 / 8 = 10 10 / 8 = 1
4 2 1
push
1 2 4
pop
124
84 / 16 = 5
4 5
push
5 4
pop 54
MAZE TRACER
All our mazes will be two-dimensional arrays of n rows and n columns. Each row, column cell is either open, or blocked by an internal wall. From any open cell, you may move left, right, up, or down to an adjacent empty cell. To solve a maze, you must find a path of open cells from a given start cell to a specified end cell. By default, you should assume that the start cell is in position (0,0).
Sample: SOOOOO HHHHOH HOOOOH HOHHHH HOHOOO HOOOHE
The path will be: [(0,0),(0,1),(0,2),(0,3),(0,4), (1,4),(2,4),(2,3), (2,2),(2,1),(3,1), (4,1),(5,1),(5,2),(5,3),(4,3),(4,4), (4,5),(5,5)]
0 0 0 0
Push start (0,0)
5 4 3 2 1 0 0 0 0 0 0
Pop
1 4 3 2 1 0 0 0 0 0 0
4 4 3 2 1 0
4 4 4 3 2 1 0
0 0 0 0
Push
Push
2 2 2 2 1
1 2 3 4 4
4 3 2 2 2 2 1 0 0 0 0 0
1 1 1 2 3 4 4 4 3 2 1 0
Push
4 4 4 5 5 5 4 3 2 2 2 2 1 0
5 4 3 3 2 1 1 1 1 2 3 4 4 4
5 4 4 4 5 5 5 4 3 2 2 2 2 1 0 0 0 0 start 0
5 5 4 3 3 2 1 1 1 1 2 3 4 4 4 3 2 1 0
Push
End
0
0 0 0 0
4
3 2 1 0
Push
0 0
0 0
3 2
1 0
Push
UNDO OPERATION
An
Undo groups are stored on a stack, with the oldest groups at the
bottom and the newest at the top. The undo stack is unlimited by default, but you can restrict it.
When the stack exceeds the maximum, the oldest undo groups are
dropped from the bottom.
UNDO OPERATION
Initially, both stacks are empty. Recording undo operations adds to the undo stack, but the redo stack remains empty until undo is performed.
pull the operations off the redo stack, apply them to the objects, and
push them back onto the undo stack. The redo stacks contents last as long as undo and redo are performed
Thank you!