Professional Documents
Culture Documents
Class XII
Computer Science (Notes)
STACKS
A stack is a LIFO structure and physically it can be implemented as an array or as a
linked list. In a stack insertions and deletions occur at the top only. An insertion in a stack
is called pushing and a deletion from a stack is called popping.
S.1 Stack as an Array
As arrays are static data structures, space required for them must be predetermined
i.e., how many total elements will be existing together at any point of time must be
known beforehand.
Program to illustrate array implementation of stack.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class stack
{
int arr[20];
int top, max;
public:
stack ( )
{
top = -1;
max=19;
}
void push();
void pop( );
void display( );
};
void stack : : push()
{
if(top= =max)
cout<< \n Overflow;
else
{
int d;
cout <<enter element to be pushed \n;
cin >> d;
top++;
arr[top]=d;
cout << d << PUSHED INTO STACK.;
}
}
1
Stack / Queue
Example 1:
Convert X: A + ( B * C (D / E F ) * G) * H into postfix form showing stack
status after every step in tabular form.
Solution
Symbol Scanned
1.
A
2.
+
3.
(
4.
B
5.
*
6.
C
7.
8.
(
9.
D
10.
/
11.
E
12.
13.
F
14.
)
15.
*
16.
G
17.
)
18.
*
Stack
(
(+
(+(
(+(
(+(*
(+(*
(+((+(-(
(+(-(
(+(-(/
(+(-(/
(+(-(/
(+(-(/
(+((+(-*
(+(-*
(+
( +*
Expression V
A
A
A
AB
AB
ABC
ABC*
ABC*
ABC*D
ABC*D
ABC*DE
ABC*DE
ABC *DEF
ABC*DEF/
ABC*DEF/
ABC*DEF/G
ABC*DEF/G*ABC*DEF/G*5
Stack / Queue
H
)
(+*
ABC*DEF/G*-H
ABC*DEF/G*-H* +
Example 2:
Convert the expression (TRUE && FALSE) ||! (FALSE || TRUE) to postfix
expression. Show the contents of the stack at every step.
Sol. Recall order of operations is
NOT (highest)
AND
OR (lowest)
(TRUE && FALSE) ||! (FALSE || TRUE) ]
Adding ] to the end of expression and inserting [ to the beginning of stack.
Scanning from Left to Right
6
Stack / Queue
Example 3:
Translate the following infix expression into its equivalent postfix expression:
((AB)*(D/E))/(F*G*H)
Sol. AB DE /* FG * H * /
S.5 Evaluation of a postfix expression
1. Read the next element
2. If element is an operand. Push the element in the stack.
3. If element is operator then pop two operands from the stack. Evaluate the
expression formed by the two operands and the operator.
4. If elements are covered then POP the result else goto step 1
Example 1: 5 6 2 + * 12 4 / Ans. 37
Example 2: true false true NOT false true OR NOT AND OR AND
Ans. false
Example 3: 6 20 * Ans. -112
Example :
Evaluate the expression 5 6 2 + * 12 4 / - in tabular form showing stack status after
every step.
Solution
Example :
Evaluate the following expression in postfix form using a stack and show the contents
of the stack after execution of each operation :
true false true NOT false true OR NOT AND OR AND
Sol. False
7
Stack / Queue
8
Stack / Queue
10
Stack / Queue
}while (ch!=Q;
}
Q3 CIRCULAR QUEUE
# include<iostream.h>
# include <conio.h>
#include<stdio.h>
# include<process.h>
struct employee
{
int emp_no;
char emp_name[25];
float salary;
};
void read(employee &s)
{
cout<<\nENTER the employee number: :;
cin>>s.emp;
cout<<\nENTER the employee number: :;
gets(s.emp_name);
cout<< nENTER THE SALARY: :;
cin>>s.salary;
}
void disp(employee &s)
{
cout<<\n\NEMPLOYEE NO:<<s.emp_no;
cout<<\nEMPLOYEE NAME:;
puts(s.emp_name);
cout<<SALARY:<<s.salary;
}
12
Stack / Queue
class queue
{
employee a[20];
int front, rear, max;
public:
queue( )
{
front=rear=-1;
max=19;
}
void add( );
void del( );
void display( );
int isfull( );
int isempty( );
};
int queue: :isfull( )
{
if((front= =rear+1) | | (front= =0) &&(rear= =max)))
return 1;
else
return 0;
}
void queue: :add( )
{
if(isfull( ))
cout<<\n queue overflow! !;
else
{
employee temp;
read(temp);
if(isempty( ))
front = rear = 0;
else
if(rear= =max)
rear=0;
else
rear++;
a[rear]=temp;
}
}
13
Stack / Queue
14
Stack / Queue
void main( )
{
int ch;
char c;
queue ob;
do
{
clrscr( );
cout << \n#######MENU######;
cout << \n1.ADD A RECORD IN QUEUE.;
cout << \n2.REMOVE A RECORD FROM QUEUE.;
cout << \n3.DISPLAY THE QUEUE.;
cout << \n4.QUIT.;
cout << \n ENTER YOUR CHOICE : :;
cin >> ch;
switch (ch)
{ case 1 : ob.add( );
break;
case 2 : ob.del( );
break;
case 3 : ob.display( );
break;
case 4 : exit(0);
default : cout << \n\n WRONG CHOICE !!!;
}
cout << \n\n\n DO YOU WANT TO CONTINUE?;
cin >> c;
} while ( ( c = = y ) | | ( c = = Y ) );
getch( );
}
15
Stack / Queue