Stack Class Notes
Stack Class Notes
What is a Stack?
A stack is a linear structure in which items may be added or removed only at one end.
Everyday examples of such a structure: a stack of dishes, a stack of pennies and a stack of
folded towels. Observe that an item may be added or removed only from the top of any of
the stacks. This means, in particular, that the last item to be added to a stack is the first item
to be removed. Accordingly, stacks are also called last-in first-out (LIFO) lists. Other names
used for stacks are "piles" and "push-down lists."
Definitions:
A stack is a list of elements in which an element may be inserted or deleted only at one
end, called top of the stack. This means, in particular, that elements are removed from a
stack in the reverse order of that in which they were inserted into the stack.
Special terminology is used for two basic operations associated with stacks:
Postponed Decisions
Stacks are frequently used to indicate the order of the processing of data when certain steps
of the processing must be postponed until other conditions are fulfilled. This is illustrated as
follows.
Suppose that while processing some project A we are required to move on to project B,
whose completion is required in order to complete project A. Then we place the folder
containing the data of A onto a stack, as pictured in Fig. (a), and begin to process B.
However, suppose that while processing B we are led to project C, for the same reason. Then
we place B on the stack above A, as pictured in Fig. (b), and begin to process C.
Furthermore, suppose that while processing C we are likewise led to project D. Then we
place C on the stack above B, as pictured in Fig. (c), and begin to process D.
On the other hand, suppose we are able to complete the processing of project D. Then the
only project we may continue to process is project C, which is on top of the stack. Hence we
remove folder C from the stack, leaving the stack as pictured in Fig. (d), and continue to
process
C. Similarly, after completing the processing of C, we remove folder B from the stack,
leaving the stack as pictured in Fig. (e), and continue to process B. Finally, after completing
Algorithm for deleting an element from a stack using Array [Algorithm for POP
operation using Array]:-
Pop ( ):
Description: Here STACK is an array with MAX locations. TOP points to the top
most element.
1. If (TOP == -1) Then [Check for underflow]
2. Print: Underflow
void main()
{
int choice; char ch;
do
{
clrscr();
printf("\n1. PUSH ");
printf("\n2. POP ");
printf("\n3. TRAVERSE ");
printf("\nEnter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: printf("\nThe deleted element is %d",pop());
break;
case 3: traverse();
break;
default: printf("\nYou Entered Wrong Choice");
}
printf("\nDo You Wish To Continue (Y/N)");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='Y' || ch=='y');
}
Algorithm for deleting an element from a stack using Linked List [Algorithm for POP
operation using Linked List]:-
void main()
{
struct node *top = NULL ;
int ch, item;
do
{
printf("\n\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("\nEnter value to be inserted: ");
scanf("%d", &item);
push(&top, item);
break;
case 2:
item=pop(&top);
printf ( "\n Item popped: %d", item) ;
break;
case 3:
display(&top);
break;
case 4:
exit(0);
We translate, step by step, the following infix expressions into Polish notation using brackets
[ ] to indicate a partial translation:
(A + B) *C= [+AB]*C =
*+ABC A + (B*C) = A +
[*BC] = +A*BC
(A + B) / (C - D) = [+ AB] / [ - CD] = / + AB - CD
The fundamental property of Polish notation is that the order in which the operations are to
be performed is completely determined by the positions of the operators and operands in the
expression. Accordingly, one never needs parentheses when writing, expressions in Polish
notation.
Reverse Polish notation refers to the analogous notation in which the operator symbol is
placed after its two operands:
AB+, CD-, EF*, GH/
Again, one never needs parentheses to determine the order of the operations in any
arithmetic expression written in reverse Polish notation. This notation is frequently called
postfix (or suffix) notation, whereas prefix notation is the term used for Polish notation,
discussed in the preceding paragraph.
The computer usually evaluates an arithmetic expression written in infix notation in two
steps. First, it converts the expression to postfix notation, and then it evaluates the postfix
expression. In each step, the stack is the main tool that is used to accomplish the given task.
Algorithm for Converting Infix to Postfix Notation:-
Let Q be an arithmetic expression written in infix notation. Besides operands and
operators, Q may also contain left and right parentheses. We assume that the operators in Q
consist only of exponentiations (↑), multiplications (*), divisions (/), additions (+) and
subtractions (-), and that they have the usual three levels of precedence as given below:
1. exponentiations (↑)
2. multiplications (*), divisions (/)
3. additions (+) and subtractions (-)
We also assume that operators on the same level, including exponentiations, are
Q: A+(B*C- (D/E↑F)*G)*H
Evaluate ( ):
Description: Here P is a postfix expression and this algorithm evaluates it.
notation: P : 5, 6, 2, +, *, 12, 4, /, -, )
1. (A-B) * (D/E)
2. (A + B ^ D) / ( E – F ) + G
3. A * ( B + D ) / E – F * ( G + H / K)
4. 12 / ( 7 – 3 ) + 2 * ( 1 + 5 )
5. (( A + B) * D) ^ (E – F)
6. (A-B) /((D+E) * F)
7. ((A+B)/D) $ ((E – F) * G)
8. 5 + 3 $ 2 – 8 / 4 * 3 +6
9. 6 + 2 $ 3 + 9 / 3 - 4 * 5
10. 4 $ 2 * 3 - 3 + 8 / 4 / ( 1 + 1 )
11. ( A + B ^ D) / ( E – F) + G
12. ( A + B ) * ( C $ ( D – E ) + F ) / G $ ( H – J )
NOTE :- After converting the infix to postfix expression. Evaluate the postfix
expression for expression number 4, 8, 9, 10.
Given Expression is : 5 + 3 $ 2 – 8 / 4 * 3 +6
1 6 Empty 6
2 + + 6
3 3 + 36
4 * +* 36
5 4 +* 436
6 / + */ 436
7 8 + */ 8436
8 - +- */8436
9 2 +- 2*/8436
10 $ +-$ 2*/8436
11 3 +-$ 32*/8436
12 + +-+ $32*/8436
13 5 +-+ 5$32*/8436
2 3 6,3
3 4 6 , 3, 4
4 8 6 , 3, 4, 8
5 / 6 , 3, 2
6 * 6, 6
7 2 6, 6, 2
8 3 6, 6, 2, 3
9 $ 6, 6, 9
10 5 6, 6, 9, 5
11 + 6, 6, 14
12 - 6, 8
13 + 14 [Final Vaue]
void main( )
{
struct stack s, temp, final;
int i ;
clrscr( ) ;
initstack ( &s ) ; initstack ( &temp ) ; initstack ( &final ) ;
push ( &s, 11 ) ; push ( &s, 23 ) ; push ( &s, -8 ) ; push ( &s, 16 ) ;
push ( &s, 27 ) ; push ( &s, 14 ) ; push ( &s, 20 ) ; push ( &s, 39 ) ;
push ( &s, 2 ) ; push ( &s, 15 ) ;
printf("Elements of Source Stack are:\n");
for(i=s.top; i >= 0; i--)
{
printf("%d\t",s.arr[i]);
}
while(s.top >= 0)
{
i = pop ( &s ) ;
push ( &temp, i ) ;
}
printf("Elements of Intermediate Stack are:\n");
for(i=temp.top; i >= 0; i--)
{
printf("%d\t", temp.arr[i]);
}
while(temp.top >= 0)
{
i = pop ( &temp ) ;
push ( &final, i ) ;
}
printf("Elements of Destination Stack are:\n");
for(i=final.top; i >= 0; i--)
{
Instructor: Deepak Sharma (9873600359) Page 15
printf("%d\t",final.arr[i]);
}
getch( ) ;
}