You are on page 1of 15

Delhi Public School Mathura Road

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

Delhi Public School Mathura Road

void stack: : pop( )


{
if(top= = -1)
cout<< \n Underflow;
else
{
int elt=arr[top];
top - -;
cout << elt << POPPED FROM STACK.;
}
}
void stack: : display( )
{
for (int i = top; i >= 0; i--)
cout << \n << arr[i];
}
void main( )
{ int ch;
char c;
stack ob;
int n;
do
{
clrscr( );
cout << \n #######MENU#######;
cout << \n1. PUSH INTO STACK.;
cout << \n 2. POP INTO STACK.;
cout << \n 3. DISPLAY THE STACK.;
cout << \n 4. EXIT.;
cout << \n ENTER YOUR CHOICE?:
cin >> ch;
switch(ch)
{ case 1 : cout << \n Enter a number?;
cin>>n;
ob.push(n);
break;
case 2:
ob.pop( );
break;
case 3:
ob.display( );
break;
case 4:
exit( 0);
}
cout << \ndo you want to continue?;
cin >> c;
}while((c= =y) || (c = = Y ));
getch( );
2
Stack / Queue

Delhi Public School Mathura Road


}
S.2 Stack as a linked list
Dynamic Stack
struct NODE
{
int Data;
NODE *Next;
};
class Stack
{
NODE *Top;
public:
Stack( ) {Top=NULL;}
void Push( );
void Pop( );
void Disp( );
~Stack( );
};
void Stack: :Push( )
{
NODE *Temp;
Temp=new NODE;
if (!Temp)
cout <<Overflow\n;
else
{
cout << Data:;
cin >> Temp Data;
Temp Next = Top;
Top=Temp;
}
}
void Stack: :Pop( )
{
if (Top != NULL)
{
NODE *Temp = Top;
cout << TopData << Deleted..\n;
Top = Top Next;
delete Temp;
}
else
cout << Underflow\n;
}
void Stack : : Disp( )
{
if (Top==NULL)
3
Stack / Queue

Delhi Public School Mathura Road


cout <<Stack is empty\n;
else
{
NODE *Temp = Top;
while (Temp != NULL)
{
cout<<Temp Data<<endl;
Temp=Temp Next;
}
}
}
Stack: : ~Stack( )
//Destructor Function
{
while (Top!=NULL)
{
NODE *Temp=Top;
Top=Top Next;
delete Temp;
}
}
void main( )
{
Stack ST; char Ch;
do
{
cout<< P/O/D/Q; cin>>Ch;
switch (Ch)
{ case P : ST.Push( ); break;
case O: ST.pop( ); break;
case D: ST.Disp( );
}
}
while (Ch! = Q);
}
// Destructor function will be called automatically when the
// scope of the object gets over
S.3 Application of Stacks
1. Arithmetic Expressions
- combination of variables and /or constants connected by arithmetic operators &
brackets.
, - , +, *, /
Arithmetic operators
Precedence
1. Brackets or Parenthesis
2.
Exponentialtion
3. * , /
Multiplication or Division
4. +, Addition or Subtraction
A + B Infix
A B + Postfix
- A B Prefix
4
Stack / Queue

Delhi Public School Mathura Road

S.4 Conversion of Infix to Postfix expression


X arithmetic expression
Y postfix expression
Steps:
1. Push '(' onto the stack and add ')' to end of X
2. Scan X from left to right and repeat step 3 to 6 for each element of X until
Stack is empty
3. If operand is encountered, add it to Y
4. If a '(' push onto stack
5. If operator is encountered a) Repeatedly pop from STACK and add to Y each operator (on the top)
which has same or higher precedence than the operator.
b) Add operator to stack
6. If ) is encountered
a) Repeatedly pop from stack & add to Y until ( is encountered
b) Remove ( from stack
7. Exit

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

Delhi Public School Mathura Road


19.
20.

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

Delhi Public School Mathura Road

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

Delhi Public School Mathura Road


Example :
8 6 20 * Sol. -112
QUEUES
A queue is a FIFO structure and physically it can be implemented either as an array or as
a linked list. Whatever way a queue is implemented, insertions take place at the rear
end and deletions at the front end.
When a queue is created as an array, its number of elements is declared before
processing. The beginning of the array becomes its front end and the end of the array
becomes its rear end.
Front stores the index of first element in the queue and rear stores the index of last
element in the queue. The number of elements in a queue at any given time can be
calculated from the values of the front and the rear.
If front = -1 then no-of-elements = 0
Q.1Queue as an Array
Program to illustrate the array implementation of a queue.
# include<iostream.h>
# include <conio.h>
# include<process.h>
class queue
{
int q[20];
int front, rear, max;
public:
queue( )
{
front = rear = -1;
max = 29;
void qinsert( );
void qdelete( );
void display( );
int isfull( )
{
return (max = = rear);
}
int isempty( )
{
return (front = = -1);
}
}

8
Stack / Queue

Delhi Public School Mathura Road

void queue : : qinsert( )


{
if (isfull ( ) )
cout << \nQUEUE OVERFLOW;
else
{
int elt;
cout << \n ENTER A NUMBER: ;
cin >> elt;
if ( isempty ( ) )
front = rear = 0;
else
rear ++;
q[rear] = elt;
}
}
void queue : : qdelete ( )
{
if (isempty( ) )
cout << \nQUEUE UNDERFLOW;
else
{
int elt = q[front];
cout << \n << elt << REMOVED FROM QUEUE;
if (front = = rear)
front = rear = -1;
else
front ++;
}
}
void queue : : display( )
{
for (int i = front; i < = rear; i++)
cout << \n << q[i];
}
void main( )
{
int ch;
char c;
queue ob;
do
{
clrscr( );
cout << \n#########MENU########;
cout << \n1. ADD ELEMENT TO QUEUE;
9
Stack / Queue

Delhi Public School Mathura Road


cout << \n 2. REMOVE ELEMENT FROM QUEUE;
cout << \n 3. DISPLAY THE QUEUE;
cout << \n 4. EXIT;
cout << \n ENTER YOUR CHOICE? ;
cin >> ch;
switch(ch)
{
case 1 :
ob.qinsert( );
break;
case 2 :
ob.qdelete( );
break;
case 3 :
ob.display( );
break;
case 4 :
exit( 0 );
default : cout << \n WRONG CHOICE ! ! ! ;
}
cout << \n \n DO YOU WANT TO CONTINUE?;
cin >> c;
} while ( ( c = = y ) | | ( c = = Y ) );
getch ( );
}
Q.2 DYNAMIC QUEUE
struct NODE
{
int Data; NODE *Next;
};
class Queue
{
NODE *Rear, *Front;
Public:
Queue ( ) {Rear= NULL; Front=NULL;}
void Qinsert( );
void Qdelete( );
void Qdisplay( );
~Queue( ):
};

10
Stack / Queue

Delhi Public School Mathura Road

void Queue: : Qinsert( )


{
NODE *Temp;
Temp=new NODE;
cout<<Data:;
cin>>Temp->Data;
Temp->Next=NULL;
If (Rear ==NULL)
{
Rear=Temp;
Front=Temp;
}
else
{
Rear->Next = Temp;
Rear=Temp;
}
}
void Queue: : Qdelete( )
{
if (Front!=NULL)
{
NODE *Temp=Front;
Cout<<Front->Data<<Deleted\n;
Front=Front->Next;
Delete Temp;
If (Front ==NULL) Real=NULL;
}
else
cout<<Queue Empty..;
}
void Queue : :Qdisplay( )
{
NODE *Temp=Front;
While (Temp!=NULL)
{
cout<<TempData<<endl;
Temp=TempNext;
}
}
Queue: : ~Queue ( )//Destructor Function
{
while (Front!=NULL)
{
11
Stack / Queue

Delhi Public School Mathura Road


NODE *Temp= Front;
Front = FrontNext; delete Temp;
}
}
void main( )
{
Queue QU; char Ch;
Do
{

}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

Delhi Public School Mathura Road

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

Delhi Public School Mathura Road

void queue: :del( )


{
if(isempty( ))
cout<<\n Queue underflow! !;
else
{ employee temp;
temp=a[front];
cout<<\nEmployee number<<temp.emp_no<<remove from queue\n;
if(front= = rear)
front=rear=-1;
else
if(front= = max)
front=0;
else
front++;
}
}
void queue : :display( )
{
if(isempty( ))
cout<<\n Queue underflow!!
else
{
if(front<=rear)
{
for(int i = front; i<= rear;i++)
disp(a[i]);
}
else
{
for(int i=front;i<=max;i++)
disp(a[i]);
for(i=0;i<=rear;i++)
disp(a[i]);
}
}
}

14
Stack / Queue

Delhi Public School Mathura Road

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

You might also like