Professional Documents
Culture Documents
Module 2
Module 2
Module 2
MODULE 2
<--TOP
Availaible at VTU HUB (Android App)
POP
D <--TOP
STACK
EMPTY STACK UNDERFLOW
Availaible at VTU HUB (Android App)
Push Algorithm
PUSH(STACK, TOP, MAXSTK, ITEM)
This procedure pushes an ITEM onto a stack
1.[ Stack already filled? ]
If TOP = MAXSTK, then: print: OVERFLOW, and return
2. Set TOP := TOP + 1 [ Increases TOP BY 1]
3. Set STACK[ TOP ] := ITEM [ Inserts ITEM in new TOP
position ]
4. Return
int IsFull( ) {
if ( top >= capacity – 1)
return 1;
return 0;
} Availaible at VTU HUB (Android App)
Void Push(int item)
{
If(IsFull( ))
Stackfull( );
Top++;
* stack=item;
return;
}
Stackfull( )
{
Stack=realloc(2*capacity*size of (*stack));
Capacity*=2;
}
A*B+C------[AB*]+C= AB*C+
------ [*AB]+C = +*ABC
Postfix Expression
2/3–42*+
Token
Operand,
6 PUSH onto
stack
Postfix Expression
2/3–42*+
Postfix Expression
/3–42*+
Token
Operand,
2 PUSH onto
stack
Postfix Expression
/3–42*+
2
6
Postfix Expression
3–42*+
Token
Operator, POP
/ 2 top elements
from the stack
2
and store it in
6 opnd2 and
opnd1
Availaible at VTU HUB (Android App)
stack
Postfix Expression
3–42*+
opnd1 opnd2
6 2
Postfix Expression
3–42*+
Evaluate
opnd1 opnd2
opnd1
6 2 operator
opnd2
Postfix Expression
3–42*+
Postfix Expression
3–42*+
Postfix Expression
–42*+
Token
Operand,
3 PUSH onto
stack
Postfix Expression
–42*+
3
3
Postfix Expression
42*+
Token
Operator, POP
- 2 top elements
from the stack
3
and store it in
3 opnd2 and
opnd1
Availaible at VTU HUB (Android App)
stack
Postfix Expression
42*+
Evaluate
opnd1 opnd2
opnd1
3 3 operator
opnd2
Postfix Expression
42*+
Postfix Expression
42*+
Postfix Expression
2*+
Token
Operand,
4 PUSH onto
stack
Postfix Expression
2*+
4
0
Postfix Expression
*+
Token
Operand,
2 PUSH onto
stack
4
0
Postfix Expression
*+
2
4
0
Postfix Expression
+
Token
Operator, POP
2 * 2 top elements
from the stack
4
and store it in
0 opnd2 and
opnd1
Availaible at VTU HUB (Android App)
stack
Postfix Expression
+
Evaluate
opnd1 opnd2
opnd1
4 2 operator
opnd2
Postfix Expression
+
Postfix Expression
+
8
0
Postfix Expression
Token
Operator, POP
+ 2 top elements
from the stack
8
and store it in
0 opnd2 and
opnd1
Availaible at VTU HUB (Android App)
stack
Postfix Expression
Evaluate
opnd1 opnd2
opnd1
0 8 operator
opnd2
Postfix Expression
Postfix Expression
postfixVect
infixVect
a+b-c)*d–(e+f)
postfixVect
infixVect
+b-c)*d–(e+f)
postfixVect
a
infixVect
b-c)*d–(e+f)
postfixVect
a
+
(
infixVect
-c)*d–(e+f)
postfixVect
ab
+
(
infixVect
c)*d–(e+f)
postfixVect
ab+
-
(
infixVect
)*d–(e+f)
postfixVect
ab+c
-
(
infixVect
*d–(e+f)
postfixVect
ab+c-
infixVect
d–(e+f)
postfixVect
ab+c-
infixVect
–(e+f)
postfixVect
ab+c-d
infixVect
(e+f)
postfixVect
ab+c–d*
infixVect
e+f)
postfixVect
ab+c–d*
(
-
infixVect
+f)
postfixVect
ab+c–d*e
(
-
infixVect
f)
postfixVect
+ ab+c–d*e
(
-
infixVect
)
postfixVect
+ ab+c–d*ef
(
-
infixVect
postfixVect
ab+c–d*ef+
infixVect
postfixVect
ab+c–d*ef+-
MODULE 2
PART-2
int IsFull( ) {
if ( top >= capacity – 1)
return 1;
return 0;
} Availaible at VTU HUB (Android App)
Void Push(int item)
{
If(IsFull( ))
Stackfull( );
Top++;
Stack[Top]=item;
return;
}
Stackfull( )
{
Stack=realloc(2*capacity*size of (*stack));
Capacity*=2;
}
Reversing a list
Reversing Data: We can use stacks to reverse data.
(example: files, strings) rsum=0 num=25652 while
num!=0
Very useful for finding palindromes. Reminder=num%10= 2
num=num/10=0
Pseudocode: Stack=2,5,6,5,2
while(!empty()) x=pop()
1) read (data) rsum =rsum*10+x
2) loop (data not EOF and stack not full) =25652
1) push (data)
2) read (data)
3) Loop (while stack notEmpty)
1) pop (data)
2) print (data)
The problem with this code is that it will print the binary
number backwards. (ex: 19 becomes 11001000 instead of 00010011. )
To remedy this problem, instead of printing the digit right away, we can
push it onto the stack. Then after the number is done being converted, we
pop the digit out of the stack and print it.
Goal seeking
Decision Analysis
Initially, all the disks are on the first peg called source peg in proper order of
size, that is the largest on the bottom and the smallest on top.
The goal is to move all the disks to the third peg called destination peg using the
second one as an auxiliary, if necessary.
We can move only one disk at a time, and it is forbidden to place a larger disk on
top of a smaller one.
A B C D
front rear
Availaible at VTU HUB (Android App)
Data structure for Queue
#define MAXQ 25
typedef struct {
int front, rear;
char item[MAX];
}QUEUE;
QUEUE q;
q.rear = -1;
q.front = -1;
[3] [4]
C D
[2] [5]
B E
A F
[6]
[1] G
[0] [7]
Front=0, Rear=7
A B C D E F G
Front=0, Rear=7
Availaible at VTU HUB (Android App)
Implementation of Circular Queue
Operations
When the array is viewed as a circle, each array position has a next and a
previous position. The position next to MAX-QUEUE-SIZE -1 is 0,
and the position that precedes 0 is MAX-QUEUE-SIZE -1.
When the queue rear is at MAX_QUEUE_SIZE-1, the next element is
inserted at position 0.
In circular queue, the variables front and rear are moved from their
current position to the next position in clockwise direction. This
may be done using code
if (rear = = MAX_QUEUE_SIZE-1)
rear = 0;
Else
rear++;
This can be done by using the modulus operator, which computes
remainders.
(rear +1) % MAX_QUEUE_SIZE
Availaible at VTU HUB (Android App)
[3] [4]
C D
[2] [5]
B E
A F
[6]
[1] G
[0] [7]
Front=0, Rear=7
A B C D E F G
Front=0 Rear= 7
FRONT REAR
1 2 3 4 5 6
2 2
1 A
1 3 2 B C X
0 0 3
4 F D E
5 1
5 G
4 4
1 0
AVAIL 5
2 A 2 6
3 K 4 0
START 7
4 T 1 2
5 1
6 B 3 8
7 X 1 4
8 C 4 3
bottommost bottommost
stack 1 stack 2
All stacks are empty and divided into roughly equal segments.
element delete(int i)
{ /* remove top element from the ith stack */
if (top[i] == boundary[i])
return stack_empty(i);
return memory[top[i]--];
}
55
Availaible at VTU HUB (Android App)
The top[i] == boundary[i+1] condition in push implies only
that a particular stack ran out of memory, not that the entire
memory is full. But still there may be a lot of unused space
between other stacks in array memory as shown in Figure.
Therefore, create an error recovery function called stackFull ,
which determines if there is any free space in memory. If there
is space available, it should shift the stacks so that space is
allocated to the full stack.