Eeea Daa Sce Baed ide b Ha Zh ad Da Sci
Read Ce e a, Chae 10
1
Li
We i fi decibe he a ADT = Abac Daa Te
22
Abac . Ccee Daa Te
Abac Daa Te (ADT)
Maheaica decii f a bjec ad he e f eai he bjec
Li, Sack, Tee, Hea, Gah,
Oe ADT a eciaie ahe ADT
Ccee Daa Te
Ieeai f a ADT ig e e f pimiie daa e ad eai f k cei
Piiie: iege, aa, ie efeece
Objec-ieed gaig agage (Jaa, C++) e eplicil defie e ccee daa e ha ced ADT’.
33
Li ADT
( A1 A2 A-1 A ) =
Maheaicadecii:aeencefie Ai ecedeAi+1 f1i<
Oeai
Fi() = ii
Vae(ii) = ie
Ne(ii) = ii Legh() = iege
Ie(ie,ii)
Deee(ii)
Wha ohe opeaion migh be efl?
Kh(iege)=ie SeKh(ie,iege) Fid(ie)=ii
44
Seciaiai Hieach
Li Pe: Seece
Fi()= Vae()=ie Kh(iege)=ie Ne()= Legh()=iege SeKh(ie,iege) Ie(ie,) Deee() Fid(ie)=ii
Sack Pe: LIFO
Ph(ie) P()=ie IE()=e/fae
Qee Pe: FIFO
Eee(ie) Deee()=ie IE()=e/fae
Vec Pe: ad acce
Kh(i) = ie SeKh(ie,iege)
55
Ieeai Hieach
Li Cei: Uecified
Fi()= Vae()=ie GeKh(iege) Ne()= Legh()=iege SeKh(ie,iege) Ie(ie,) Deee() Fid(ie)=ii
Liked Li (1) f:
() f:
Aa (1) f:
() f:
66
7 L D S - I
(1) L J
A . J
T
;
;
()
= 0; = ; ( , ) = ; =;
7
8 T
de = e de();
de = e de(1,); .e = ;
0
1
8
9 (1) T P
.e = e de(15,);
(2) T 2
= e de(2,);
10
20
10
15
20
10
20
0
10
20
9
10 T
10
20
30
.e = .e;
10
11 C
bic ca ikedi
iae aic de head;
bic id Diai(de ) .. bic id Bidi() ..
bic aic id ai(Sig[] ag) ikedi i = e ikedi();
i.Bidi(); i.Diai();
11
12 T
bic id BidLi() de = e de(0,);
head = ;
Sig eLie; BffeedReade idaa = e
BffeedReade(e ISeaReade(Se.i)); // ead daa f eia Se..i("H a de?\");
eLie = idaa.eadLie(); // aa eed he fig ie ead daa head.daa = Iege.aeI(eLie);
f (i i=1; i<=head.daa; i++)
Se..i("A e ae eae\");
eLie = idaa.eadLie();
i = Iege.aeI(eLie); de = e de(,); .e = ;
= ;
cach(Ecei e)
Se..i("E --" + e.Sig());
12
13 T
bic id DiaLi(de ) if ( != )
Se..i(.daa); DiaLi(.e);
13
14 (2) S
A ack i a iea daa ce i hich eee ae added ad eed f
e deigaed ed caed he LIFO La I Fi O.
Gie a ack S = (a0, a1, a-1, a), e a ha a0 i he b eee, a i he eee if he ae added i he de f a0,a1, .. ad a, h be:
T
0
1
2
.
-1
an abac ie f a ack f n elemen
T, 0 .
T .
14
15 S O
BASIC OPERATIONS:
Ph: ace a eee a he f he ack
P: ee he eee f he ack
T: eiee he ae f he eee, ad he ack eai chaged
OTHER OPERATIONS:
IE: e TRUE if he ack i e IF: e TRUE if he ack i f MakeE
Jaa ieeai (ig iked i)
bic ca SackNde bjec if;
SackNde ik;
bic SackNde()
bic SackNde(Objec j, SackNde )if = j; ik = ;
15
16 T S
16
bic ca SackLi
iae SackNde ;
bic id h(Objec ie) = e SackNde (ie,); bic Objec ( )
SackNde dT = ; Objec ie = ();
= .ik; dT.ik = ; e ie;
bic Objec ()
if (iE())
h e NPieEcei(); e .if;
17 T ( )
bic ca SackVec eed Vec
bic id h(Objec ie)addEee(ie);
bic Objec ()
Objec ie;
ie = (); eeEeeA(ie()-1); e ie;
bic Objec () if(iE())
h e NPieEcei(); e eeeA(ie()-1);
bic bea iE() e (ie() == 0);
17
S
TACK
CL
ASS
F
AVA
subc
lass
V
ECT
O
R
IN
of
-
Stack( )
Apart from the methods inherited from its parent class Vector, Stack defines the following methods −
Sr.No. Method & Description 1
boolean empty()
Tests if this stack is empty. Returns true if the stack is empty, and returns false if the stack contains elements.
2
Object peek( )
Returns the element on the top of the stack, but does not remove it.
3
Object pop( )
Returns the element on the top of the stack, removing it in the process.
4
Object push(Object element)
Pushes the element onto the stack. Element is also returned.
5
int search(Object element)
Searches for element in the stack. If found, its offset from the top of the stack is returned. Otherwise, -1 is returned.
import java.util.*;
public class StackDemo {
static void showpush(Stack st, int a) { st.push(new Integer(a)); System.out.println("push(" + a + ")"); System.out.println("stack: " + st);
}
static void showpop(Stack st) { System.out.print("pop -> “); Integer a = (Integer) st.pop(); System.out.println(a); System.out.println(“stack: ” + st);
}
public static void main(String args[]) { Stack st = new Stack(); System.out.println(“stack: ” + st); showpush(st, 42);
showpush(st, 66); showpush(st, 99); showpop(st); showpop(st); showpop(st);
try { showpop(st);
} catch (EmptyStackException e) { System.out.println(“empty stack”);
} }
}
18 Q
A ee i a deed ceci f ie f hich eee a be deeed a he f f he ee ad i hich eee a be ieed a he ea f he ee. FIFO – Fi-I, Fi-O behai.
T Q ADT:
1) O:
A ee i a eece f eee f he ae e, aiaied i
fi-i / fi- de ha he eee a he f f he ee ca be eed.
Gie a ee Q = (1, 2d, 3d, .. h), e a ha he 1 eee i he e aced i he ee fi ad heefe, i be eed fi f he ee.
a
b
c
d
f ea
18
19 Q
Qee eai:
Peek – eiee he fi eee b ih eig i E – ie he eee a he ed f he ee
De – ee he eee a he f f he ee IE
IF
Pbic ca QeeNde
Objec if;
QeeNde ik;
bic QeeNde() ;
bic QeeNde()(Objec i, QeeNde j) if = i; ik = j;
19
20 A
(1) C
Algihm:
= ;
( ) &&
= .C(); // S ( == ‘(‘ )
(, ); ( == “)” )
( == ) = ;
(, );
(( == ) (IE())
S.. ( “F ” )
E:
– (2*(5*2+(2/10))+4*(7-5)) – (2*(5*2-(2/10)))+(((7-5))
20
21 (2) I
Wh fi eei?
– eed hae aehee
Eae:
Ifi eei:
(2-1) * (52 + (22-6 * 3)) Pfi eei
2 1 -5 2 ^ 2 2 ^ 6 3 * – + *
Reak:
The de f ead eai chaged
21
–
IDEA
IN F IX
→
POSTFIX CONVERSION
–
a r e printed immediately operators are placed on the STACK , their
–
O F operands
processing
that have
Cl )
(2)
# 253*t
pending .
higher priority .
is
when a new operator is seen , pop pending operators
↳ Lf # 25*3 +
2 * 5 + 3 I
2+5*3 #
1¥
22 A
ack ee
P:
(, ) ( )
** 3 */2 +-1 (, ( ) 0
();
( == ‘(‘ ) ( == ‘)’)
(); //
P() P() EQ ( ());
EQ (, ) (, )
( () != ‘(‘ ) EQ (())
22
23 T
E:
ifi: 7 ( 2 * 3 + 5 ) + 6
I (B)S
7-(2*3+5)+6 -(2*3+5)+6
(2*3+5)+6 – 2*3+5)+6 -( *3+5)+6 -(
3+5)+6 -(* +5)+6 -(* 5)+6 -(+
)+6 -(+ +6 –
6 + +
Reak: a ee i ed e he
Q
7
7
7
7 2
7 2
7 2 3
7 2 3 *
7 2 3 * 5
7 2 3 * 5 +
7 2 3 * 5 +-
7 2 3 * 5 +-6
7 2 3 * 5 + – 6+
23
24 E
I: a fi eei. O: a ae
hie ed f i
ead()
if i a be
h(,ack) ee
= (ack) e=(ack) a = e h(a,ack)
a = (ack)
eae: 2 1 – 5 2 ^ 2 2 ^ 6 3 * – + * I: 2 1
I: ‘-’ I: 5 2
I: ‘^’ I: 2 2
I: ^
I: 6 3 I: ‘*’
2
1
1
1
5
2
1
25
1
25
2
2
1
25
4
1
25
4
6
3
1
25
4
18
24
25 E
I: a fi eei. O: a ae
hie ed f i
ead()
if i a be
h(,ack) ee
= (ack) e=(ack) a = e h(a,ack)
a = (ack)
eae: 2 1 – 5 2 ^ 2 2 ^ 6 3 * – + * I: ‘-’
I: ‘+’ I: ‘*’
1
25
-14
1
11
11
25