程序代写代做代考 C Java AI E􏲮e􏲴e􏲭􏲱a􏲪􏲯 Da􏲱a S􏲱􏲪􏲰c􏲱􏲰􏲪e􏲫 Ba􏲫ed 􏲬􏲭 􏲫􏲮ide􏲫 b􏲯 Ha􏲪􏲪􏲯 Zh􏲬􏲰 a􏲭d Da􏲭 S􏲰ci􏲰

E􏲮e􏲴e􏲭􏲱a􏲪􏲯 Da􏲱a S􏲱􏲪􏲰c􏲱􏲰􏲪e􏲫 Ba􏲫ed 􏲬􏲭 􏲫􏲮ide􏲫 b􏲯 Ha􏲪􏲪􏲯 Zh􏲬􏲰 a􏲭d Da􏲭 S􏲰ci􏲰
Read C􏲬􏲪􏲴e􏲭 e􏲱 a􏲮, Cha􏲳􏲱e􏲪 10
1

Li􏲫􏲱􏲫
􏲼 We 􏲹i􏲮􏲮 fi􏲪􏲫􏲱 de􏲫c􏲪ibe 􏲱he􏲴 a􏲫 ADT􏲫 = Ab􏲫􏲱􏲪ac􏲱 Da􏲱a T􏲯􏲳e􏲫
22

Ab􏲫􏲱􏲪ac􏲱 􏲵􏲫. C􏲬􏲭c􏲪e􏲱e Da􏲱a T􏲯􏲳e􏲫
􏲼 Ab􏲫􏲱􏲪ac􏲱 Da􏲱a T􏲯􏲳e (ADT)
􏲲 Ma􏲱he􏲴a􏲱ica􏲮 de􏲫c􏲪i􏲳􏲱i􏲬􏲭 􏲬f a􏲭 􏲬bjec􏲱 a􏲭d 􏲱he 􏲫e􏲱 􏲬f 􏲬􏲳e􏲪a􏲱i􏲬􏲭􏲫 􏲬􏲭 􏲱he 􏲬bjec􏲱
􏲼 Li􏲫􏲱, S􏲱ack, T􏲪ee, Hea􏲳, G􏲪a􏲳h, 􏳈
􏲲 O􏲭e ADT 􏲴a􏲯 􏲫􏲳ecia􏲮i􏲽e a􏲭􏲬􏲱he􏲪 ADT
􏲼 C􏲬􏲭c􏲪e􏲱e Da􏲱a T􏲯􏲳e
􏲲 I􏲴􏲳􏲮e􏲴e􏲭􏲱a􏲱i􏲬􏲭 􏲬f a􏲭 ADT 􏲰􏲫i􏲭g 􏲫􏲬􏲴e 􏲫e􏲱 􏲬f p􏲪imi􏲱i􏲵e da􏲱a 􏲱􏲯􏲳e􏲫 a􏲭d 􏲬􏲳e􏲪a􏲱i􏲬􏲭􏲫 􏲬f k􏲭􏲬􏲹􏲭 c􏲬􏲴􏲳􏲮e􏲶i􏲱􏲯
􏲼 P􏲪i􏲴i􏲱i􏲵e􏲫: i􏲭􏲱ege􏲪􏲫, a􏲪􏲪a􏲯􏲫, 􏲳􏲬i􏲭􏲱e􏲪􏲫 􏲬􏲪 􏲪efe􏲪e􏲭ce􏲫
􏲲 Objec􏲱-􏲬􏲪ie􏲭􏲱ed 􏲳􏲪􏲬g􏲪a􏲴􏲴i􏲭g 􏲮a􏲭g􏲰age􏲫 (Ja􏲵a, C++) 􏲮e􏲱 􏲯􏲬􏲰 e􏲶plici􏲱l􏲯 defi􏲭e 􏲭e􏲹 c􏲬􏲭c􏲪e􏲱e da􏲱a 􏲱􏲯􏲳e􏲫 􏲱ha􏲱 c􏲬􏲪􏲪e􏲫􏲳􏲬􏲭d 􏲱􏲬 ADT’􏲫.
33

Li􏲫􏲱 ADT
( A1 A2 􏳈 A􏲭-1 A􏲭 ) 􏲮􏳉􏲭􏳊􏲱􏳋 = 􏲭
􏲼 Ma􏲱he􏲴a􏲱ica􏲮de􏲫c􏲪i􏲳􏲱i􏲬􏲭:a􏲫e􏲺􏲰ence􏲬fi􏲱e􏲴􏲫 􏲲 Ai 􏲳􏲪ecede􏲫Ai+1 f􏲬􏲪1􏳅i<􏲭 􏲼 O􏲳e􏲪a􏲱i􏲬􏲭􏲫 􏲲 Fi􏲪􏲫􏲱() = 􏲳􏲬􏲫i􏲱i􏲬􏲭 􏲲 Va􏲮􏲰e(􏲳􏲬􏲫i􏲱i􏲬􏲭) = i􏲱e􏲴 􏲲 Ne􏲶􏲱(􏲳􏲬􏲫i􏲱i􏲬􏲭) = 􏲳􏲬􏲫i􏲱i􏲬􏲭 􏲲 Le􏲭g􏲱h() = i􏲭􏲱ege􏲪 􏲲 I􏲭􏲫e􏲪􏲱(i􏲱e􏲴,􏲳􏲬􏲫i􏲱i􏲬􏲭) 􏲲 De􏲮e􏲱e(􏲳􏲬􏲫i􏲱i􏲬􏲭) Wha􏲱 o􏲱he􏲪 ope􏲪a􏲱ion􏲫 migh􏲱 be 􏲰􏲫ef􏲰l? K􏲱h(i􏲭􏲱ege􏲪)=i􏲱e􏲴 Se􏲱K􏲱h(i􏲱e􏲴,i􏲭􏲱ege􏲪) Fi􏲭d(i􏲱e􏲴)=􏲳􏲬􏲫i􏲱i􏲬􏲭 44 S􏲳ecia􏲮i􏲽a􏲱i􏲬􏲭 Hie􏲪a􏲪ch􏲯 Li􏲫􏲱 P􏲪􏲬􏲳e􏲪􏲱􏲯: Se􏲺􏲰e􏲭ce Fi􏲪􏲫􏲱()=􏲳􏲬􏲫 Va􏲮􏲰e(􏲳􏲬􏲫)=i􏲱e􏲴 K􏲱h(i􏲭􏲱ege􏲪)=i􏲱e􏲴 Ne􏲶􏲱(􏲳􏲬􏲫)=􏲳􏲬􏲫 Le􏲭g􏲱h()=i􏲭􏲱ege􏲪 Se􏲱K􏲱h(i􏲱e􏲴,i􏲭􏲱ege􏲪) I􏲭􏲫e􏲪􏲱(i􏲱e􏲴,􏲳􏲬􏲫) De􏲮e􏲱e(􏲳􏲬􏲫) Fi􏲭d(i􏲱e􏲴)=􏲳􏲬􏲫i􏲱i􏲬􏲭 S􏲱ack P􏲪􏲬􏲳e􏲪􏲱􏲯: LIFO P􏲰􏲫h(i􏲱e􏲴) P􏲬􏲳()=i􏲱e􏲴 I􏲫E􏲴􏲳􏲱􏲯()=􏲱􏲪􏲰e/fa􏲮􏲫e Q􏲰e􏲰e P􏲪􏲬􏲳e􏲪􏲱􏲯: FIFO E􏲭􏲺􏲰e􏲰e(i􏲱e􏲴) De􏲺􏲰e􏲰e()=i􏲱e􏲴 I􏲫E􏲴􏲳􏲱􏲯()=􏲱􏲪􏲰e/fa􏲮􏲫e Vec􏲱􏲬􏲪 P􏲪􏲬􏲳e􏲪􏲱􏲯: 􏲪a􏲭d􏲬􏲴 acce􏲫􏲫 K􏲱h(i􏲭􏲱) = i􏲱e􏲴 Se􏲱K􏲱h(i􏲱e􏲴,i􏲭􏲱ege􏲪) 55 I􏲴􏲳􏲮e􏲴e􏲭􏲱a􏲱i􏲬􏲭 Hie􏲪a􏲪ch􏲯 Li􏲫􏲱 C􏲬􏲴􏲳􏲮e􏲶i􏲱􏲯: U􏲭􏲫􏲳ecified Fi􏲪􏲫􏲱()=􏲳􏲬􏲫 Va􏲮􏲰e(􏲳􏲬􏲫)=i􏲱e􏲴 Ge􏲱K􏲱h(i􏲭􏲱ege􏲪) Ne􏲶􏲱(􏲳􏲬􏲫)=􏲳􏲬􏲫 Le􏲭g􏲱h()=i􏲭􏲱ege􏲪 Se􏲱K􏲱h(i􏲱e􏲴,i􏲭􏲱ege􏲪) I􏲭􏲫e􏲪􏲱(i􏲱e􏲴,􏲳􏲬􏲫) De􏲮e􏲱e(􏲳􏲬􏲫) Fi􏲭d(i􏲱e􏲴)=􏲳􏲬􏲫i􏲱i􏲬􏲭 Li􏲭ked Li􏲫􏲱 􏲺(1) f􏲬􏲪: 􏲺(􏲭) f􏲬􏲪: A􏲪􏲪a􏲯 􏲺(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􏲮􏳍􏲫􏲫 􏲮􏳌􏲭􏳏􏳉􏳐􏲮􏳌􏲫􏲱 􏲳􏲰b􏲮ic c􏲮a􏲫􏲫 􏲮i􏲭ked􏲮i􏲫􏲱 􏲷 􏲸 􏲳􏲪i􏲵a􏲱e 􏲫􏲱a􏲱ic 􏲭􏲬de head; 􏲳􏲰b􏲮ic 􏲵􏲬id Di􏲫􏲳􏲮a􏲯􏲮i􏲫􏲱(􏲭􏲬de 􏲺) 􏲷􏳈..􏲸 􏲳􏲰b􏲮ic 􏲵􏲬id B􏲰i􏲮d􏲮i􏲫􏲱() 􏲷..􏲸 􏲳􏲰b􏲮ic 􏲫􏲱a􏲱ic 􏲵􏲬id 􏲴ai􏲭(S􏲱􏲪i􏲭g[] a􏲪g􏲫) 􏲷􏲮i􏲭ked􏲮i􏲫􏲱 􏲴􏲯􏲮i􏲫􏲱 = 􏲭e􏲹 􏲮i􏲭ked􏲮i􏲫􏲱(); 􏲴􏲯􏲮i􏲫􏲱.B􏲰i􏲮d􏲮i􏲫􏲱(); 􏲴􏲯􏲮i􏲫􏲱.Di􏲫􏲳􏲮a􏲯􏲮i􏲫􏲱(); 􏲸 11 12 T􏳋􏳉 􏲴􏳉􏲱􏳋􏲬􏳐 􏲱􏳋􏳍􏲱 􏳒􏲰􏳌􏲮􏳐􏲫 􏳍 􏲮􏳌􏲭􏳏􏳉􏳐 􏲮􏳌􏲫􏲱 􏲳􏲰b􏲮ic 􏲵􏲬id B􏲰i􏲮dLi􏲫􏲱() 􏲷􏲭􏲬de 􏲺 = 􏲭e􏲹 􏲭􏲬de(0,􏲭􏲰􏲮􏲮); head = 􏲺; S􏲱􏲪i􏲭g 􏲬􏲭eLi􏲭e; 􏲱􏲪􏲯􏲷B􏲰ffe􏲪edReade􏲪 i􏲭da􏲱a = 􏲭e􏲹 B􏲰ffe􏲪edReade􏲪(􏲭e􏲹 I􏲭􏲳􏲰􏲱S􏲱􏲪ea􏲴Reade􏲪(S􏲯􏲫􏲱e􏲴.i􏲭)); // 􏲪ead da􏲱a f􏲪􏲬􏲴 􏲱e􏲪􏲴i􏲭a􏲮􏲫 S􏲯􏲫􏲱e􏲴.􏲬􏲰􏲱.􏲳􏲪i􏲭􏲱􏲮􏲭("H􏲬􏲹 􏲴a􏲭􏲯 􏲭􏲬de􏲫?\􏲭"); 􏲬􏲭eLi􏲭e = i􏲭da􏲱a.􏲪eadLi􏲭e(); // a􏲮􏲹a􏲯􏲫 􏲭eed 􏲱he f􏲬􏲮􏲮􏲬􏲹i􏲭g 􏲱􏲹􏲬 􏲮i􏲭e􏲫 􏲱􏲬 􏲪ead da􏲱a head.da􏲱a = I􏲭􏲱ege􏲪.􏲳a􏲪􏲫eI􏲭􏲱(􏲬􏲭eLi􏲭e); f􏲬􏲪 (i􏲭􏲱 i=1; i<=head.da􏲱a; i++) 􏲷S􏲯􏲫􏲱e􏲴.􏲬􏲰􏲱.􏲳􏲪i􏲭􏲱􏲮􏲭("A 􏲭e􏲹 􏲵a􏲮􏲰e 􏲳􏲮ea􏲫e\􏲭"); 􏲬􏲭eLi􏲭e = i􏲭da􏲱a.􏲪eadLi􏲭e(); i􏲭􏲱 􏲭􏲰􏲴 = I􏲭􏲱ege􏲪.􏲳a􏲪􏲫eI􏲭􏲱(􏲬􏲭eLi􏲭e); 􏲭􏲬de 􏲳 = 􏲭e􏲹 􏲭􏲬de(􏲭􏲰􏲴,􏲭􏲰􏲮􏲮); 􏲺.􏲭e􏲶􏲱 = 􏲳; 􏲺 = 􏲳;􏲸 􏲸ca􏲱ch(E􏲶ce􏲳􏲱i􏲬􏲭 e) 􏲷 S􏲯􏲫􏲱e􏲴.􏲬􏲰􏲱.􏲳􏲪i􏲭􏲱􏲮􏲭("E􏲪􏲪􏲬􏲪 --" + e.􏲱􏲬S􏲱􏲪i􏲭g());􏲸 􏲸 12 13 T􏲬 􏲳􏲪􏳌􏲭􏲱 􏳍 􏲮􏳌􏲭􏳏􏳉􏳐 􏲮􏳌􏲫􏲱 􏲭􏲬􏳐􏳉 􏳒􏲯 􏲭􏲬􏳐􏳉 􏲳􏲰b􏲮ic 􏲵􏲬id Di􏲫􏲳􏲮a􏲯Li􏲫􏲱(􏲭􏲬de 􏲺) 􏲷if (􏲺 != 􏲭􏲰􏲮􏲮) 􏲷 S􏲯􏲫􏲱e􏲴.􏲬􏲰􏲱.􏲳􏲪i􏲭􏲱􏲮􏲭(􏲺.da􏲱a); Di􏲫􏲳􏲮a􏲯Li􏲫􏲱(􏲺.􏲭e􏲶􏲱);􏲸 􏲸 13 14 (2) S􏲱􏳍􏳎􏳏􏲫 A 􏲫􏲱ack i􏲫 a 􏲮i􏲭ea􏲪 da􏲱a 􏲫􏲱􏲪􏲰c􏲱􏲰􏲪e i􏲭 􏲹hich e􏲮e􏲴e􏲭􏲱􏲫 a􏲪e added 􏲱􏲬 a􏲭d 􏲪e􏲴􏲬􏲵ed f􏲪􏲬􏲴 􏲬􏲭e de􏲫ig􏲭a􏲱ed e􏲭d ca􏲮􏲮ed 􏲱he 􏲱􏲬􏲳 LIFO 􏲲 La􏲫􏲱 I􏲭 Fi􏲪􏲫􏲱 O􏲰􏲱. Gi􏲵e􏲭 a 􏲫􏲱ack S = (a0, a1, 􏳈 a􏲭-1, a􏲭), 􏲹e 􏲫a􏲯 􏲱ha􏲱 a0 i􏲫 􏲱he b􏲬􏲱􏲱􏲬􏲴 e􏲮e􏲴e􏲭􏲱, a􏲭 i􏲫 􏲱he 􏲱􏲬􏲳 e􏲮e􏲴e􏲭􏲱 if 􏲱he􏲯 a􏲪e added i􏲭 􏲱he 􏲬􏲪de􏲪 􏲬f a0,a1, .. a􏲭d a􏲭, 􏲫h􏲬􏲹􏲭 be􏲮􏲬􏲹: T􏲬􏲳 􏳍0 􏳍1 􏳍2 . 􏳍􏲭-1 􏳍􏲭 an ab􏲫􏲱􏲪ac􏲱 􏲵ie􏲹 􏲬f a 􏲫􏲱ack 􏲬f n elemen􏲱􏲫 T􏳋􏲰􏲫, 􏳍􏲭 􏳌􏲫 􏲱􏳋􏳉 􏳑􏳌􏲪􏲫􏲱 􏲬􏲭􏳉 􏲱􏲬 􏳒􏳉 􏲪􏳉􏲴􏲬􏲵􏳉􏳐 􏳍􏲭􏳐 􏳍0 􏳌􏲫 􏲱􏳋􏳉 􏲮􏳍􏲫􏲱 􏲬􏲭􏳉 􏲱􏲬 􏳒􏳉 􏲪􏳉􏲴􏲬􏲵􏳉􏳐. T􏳋􏳉 􏲪􏳉􏲫􏲱􏲪􏳌􏳎􏲱􏳌􏲬􏲭􏲫 􏲬􏲭 􏲱􏳋􏳉 􏲫􏲱􏳍􏳎􏳏 􏳌􏲴􏲳􏲮􏲯 􏲭􏲬 􏲬􏲱􏳋􏳉􏲪 􏳉􏲮􏳉􏲴􏳉􏲭􏲱􏲫 􏳉􏲶􏳎􏳉􏲳􏲱 􏳍􏲭 􏳎􏳍􏲭 􏳒􏳉 􏲫􏳉􏳉􏲭 􏳍􏲭􏳐 􏳍􏳎􏳎􏳉􏲫􏲫􏳉􏳐. 14 15 S􏲱􏳍􏳎􏳏 O􏲳􏳉􏲪􏳍􏲱􏳌􏲬􏲭􏲫 BASIC OPERATIONS: P􏲰􏲫h: 􏲳􏲮ace a􏲭 e􏲮e􏲴e􏲭􏲱 a􏲱 􏲱he 􏲱􏲬􏲳 􏲬f 􏲱he 􏲫􏲱ack P􏲬􏲳: 􏲪e􏲴􏲬􏲵e 􏲱he 􏲱􏲬􏲳 e􏲮e􏲴e􏲭􏲱 f􏲪􏲬􏲴 􏲱he 􏲫􏲱ack T􏲬􏲳: 􏲪e􏲱􏲪ie􏲵e 􏲱he 􏲵a􏲮􏲰e 􏲬f 􏲱he 􏲱􏲬􏲳 e􏲮e􏲴e􏲭􏲱, a􏲭d 􏲱he 􏲫􏲱ack 􏲪e􏲴ai􏲭􏲫 􏲰􏲭cha􏲭ged OTHER OPERATIONS: I􏲫E􏲴􏲳􏲱􏲯: 􏲪e􏲱􏲰􏲪􏲭 TRUE if 􏲱he 􏲫􏲱ack i􏲫 e􏲴􏲳􏲱􏲯 I􏲫F􏲰􏲮􏲮: 􏲪e􏲱􏲰􏲪􏲭 TRUE if 􏲱he 􏲫􏲱ack i􏲫 f􏲰􏲮􏲮 MakeE􏲴􏲳􏲱􏲯 Ja􏲵a i􏲴􏲳􏲮e􏲴e􏲭􏲱a􏲱i􏲬􏲭􏲫 (􏲰􏲫i􏲭g 􏲮i􏲭ked 􏲮i􏲫􏲱􏲫) 􏲳􏲰b􏲮ic c􏲮a􏲫􏲫 S􏲱ackN􏲬de􏲷 􏲬bjec􏲱 i􏲭f􏲬; 􏲸 S􏲱ackN􏲬de 􏲮i􏲭k; 􏲳􏲰b􏲮ic S􏲱ackN􏲬de()􏲷􏲸 􏲳􏲰b􏲮ic S􏲱ackN􏲬de(Objec􏲱 j, S􏲱ackN􏲬de 􏲳)􏲷i􏲭f􏲬 = j; 􏲮i􏲭k = 􏲳;􏲸 15 16 T􏳋􏳉 􏳎􏲮􏳍􏲫􏲫 S􏲱􏳍􏳎􏳏 16 􏲳􏲰b􏲮ic c􏲮a􏲫􏲫 S􏲱ackLi􏲫􏲱 􏲷􏲳􏲪i􏲵a􏲱e S􏲱ackN􏲬de 􏲱􏲬􏲳; 􏲳􏲰b􏲮ic 􏲵􏲬id 􏲳􏲰􏲫h(Objec􏲱 i􏲱e􏲴)􏲷 􏲱􏲬􏲳 = 􏲭e􏲹 S􏲱ackN􏲬de (i􏲱e􏲴,􏲱􏲬􏲳);􏲸 􏲳􏲰b􏲮ic Objec􏲱 􏲳􏲬􏲳( ) 􏲷 S􏲱ackN􏲬de 􏲬􏲮dT􏲬􏲳 = 􏲱􏲬􏲳; Objec􏲱 i􏲱e􏲴 = 􏲱􏲬􏲳(); 􏲱􏲬􏲳 = 􏲱􏲬􏲳.􏲮i􏲭k; 􏲬􏲮dT􏲬􏲳.􏲮i􏲭k = 􏲭􏲰􏲮􏲮; 􏲪e􏲱􏲰􏲪􏲭 i􏲱e􏲴;􏲸 􏲳􏲰b􏲮ic Objec􏲱 􏲱􏲬􏲳() 􏲷 if (i􏲫E􏲴􏲳􏲱􏲯()) 􏲱h􏲪􏲬􏲹 􏲭e􏲹 N􏲰􏲮􏲮P􏲬i􏲭􏲱e􏲪E􏲶ce􏲳􏲱i􏲬􏲭(); 􏲪e􏲱􏲰􏲪􏲭 􏲱􏲬􏲳.i􏲭f􏲬;􏲸􏲸 17 T􏳋􏳉 􏳎􏲮􏳍􏲫􏲫 􏲫􏲱􏳍􏳎􏳏( 􏲰􏲫􏳌􏲭􏳊 􏲵􏳉􏳎􏲱􏲬􏲪􏲫) 􏲳􏲰b􏲮ic c􏲮a􏲫􏲫 S􏲱ackVec􏲱􏲬􏲪 e􏲶􏲱e􏲭d􏲫 Vec􏲱􏲬􏲪 􏲷 􏲳􏲰b􏲮ic 􏲵􏲬id 􏲳􏲰􏲫h(Objec􏲱 i􏲱e􏲴)􏲷addE􏲮e􏲴e􏲭􏲱(i􏲱e􏲴);􏲸 􏲳􏲰b􏲮ic Objec􏲱 􏲳􏲬􏲳() 􏲷 Objec􏲱 i􏲱e􏲴; i􏲱e􏲴 = 􏲱􏲬􏲳(); 􏲪e􏲴􏲬􏲵eE􏲮e􏲴e􏲭􏲱A􏲱(􏲫i􏲽e()-1); 􏲪e􏲱􏲰􏲪􏲭 i􏲱e􏲴;􏲸 􏲳􏲰b􏲮ic Objec􏲱 􏲱􏲬􏲳() 􏲷 if(i􏲫E􏲴􏲳􏲱􏲯()) 􏲱h􏲪􏲬􏲹 􏲭e􏲹 N􏲰􏲮􏲮P􏲬i􏲭􏲱e􏲪E􏲶ce􏲳􏲱i􏲬􏲭(); 􏲪e􏲱􏲰􏲪􏲭 e􏲮e􏲴e􏲭􏲱A􏲱(􏲫i􏲽e()-1);􏲸 􏲳􏲰b􏲮ic b􏲬􏲬􏲮ea􏲭 i􏲫E􏲴􏲳􏲱􏲯() 􏲷􏲪e􏲱􏲰􏲪􏲭 (􏲫i􏲽e() == 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 􏲺􏲰e􏲰e i􏲫 a􏲭 􏲬􏲪de􏲪ed c􏲬􏲮􏲮ec􏲱i􏲬􏲭 􏲬f i􏲱e􏲴􏲫 f􏲪􏲬􏲴 􏲹hich e􏲮e􏲴e􏲭􏲱􏲫 􏲴a􏲯 be de􏲮e􏲱ed a􏲱 􏲱he f􏲪􏲬􏲭􏲱 􏲬f 􏲱he 􏲺􏲰e􏲰e a􏲭d i􏲭􏲱􏲬 􏲹hich e􏲮e􏲴e􏲭􏲱􏲫 􏲴a􏲯 be i􏲭􏲫e􏲪􏲱ed a􏲱 􏲱he 􏲪ea􏲪 􏲬f 􏲱he 􏲺􏲰e􏲰e. FIFO – Fi􏲪􏲫􏲱-I􏲭, Fi􏲪􏲫􏲱-O􏲰􏲱 beha􏲵i􏲬􏲪.
T􏳋􏳉 Q􏲰􏳉􏲰􏳉 ADT:
1) O􏲪􏳊􏳍􏲭􏳌􏲽􏳍􏲱􏳌􏲬􏲭:
A 􏲺􏲰e􏲰e i􏲫 a 􏲫e􏲺􏲰e􏲭ce 􏲬f e􏲮e􏲴e􏲭􏲱􏲫 􏲬f 􏲱he 􏲫a􏲴e 􏲱􏲯􏲳e, 􏲴ai􏲭􏲱ai􏲭ed i􏲭
fi􏲪􏲫􏲱-i􏲭 / fi􏲪􏲫􏲱-􏲬􏲰􏲱 􏲬􏲪de􏲪 􏲫􏲬 􏲱ha􏲱 􏲬􏲭􏲮􏲯 􏲱he e􏲮e􏲴e􏲭􏲱 a􏲱 􏲱he f􏲪􏲬􏲭􏲱 􏲬f 􏲱he 􏲺􏲰e􏲰e ca􏲭 be 􏲪e􏲴􏲬􏲵ed.
Gi􏲵e􏲭 a 􏲺􏲰e􏲰e Q = (1􏲫􏲱, 2􏲭d, 3􏲪d, .. 􏲭􏲱h), 􏲹e 􏲫a􏲯 􏲱ha􏲱 􏲱he 1􏲫􏲱 e􏲮e􏲴e􏲭􏲱 i􏲫 􏲱he 􏲬􏲭e 􏲳􏲮aced i􏲭􏲱􏲬 􏲱he 􏲺􏲰e􏲰e fi􏲪􏲫􏲱 a􏲭d 􏲱he􏲪ef􏲬􏲪e, 􏲹i􏲮􏲮 be 􏲪e􏲴􏲬􏲵ed fi􏲪􏲫􏲱 f􏲪􏲬􏲴 􏲱he 􏲺􏲰e􏲰e.
a
b
c
d
f􏲪􏲬􏲭􏲱 􏲪ea􏲪
18

19 Q􏲰􏳉􏲰􏳉 􏳌􏲴􏲳􏲮􏳉􏲴􏳉􏲭􏲱􏳍􏲱􏳌􏲬􏲭
Q􏲰e􏲰e 􏲬􏲳e􏲪a􏲱i􏲬􏲭􏲫:
Peek – 􏲪e􏲱􏲪ie􏲵e 􏲱he fi􏲪􏲫􏲱 e􏲮e􏲴e􏲭􏲱 b􏲰􏲱 􏲹i􏲱h􏲬􏲰􏲱 􏲪e􏲴􏲬􏲵i􏲭g i􏲱 E􏲭􏲺 – i􏲭􏲫e􏲪􏲱 􏲱he e􏲮e􏲴e􏲭􏲱 a􏲱 􏲱he e􏲭d 􏲬f 􏲱he 􏲺􏲰e􏲰e
De􏲺 – 􏲪e􏲴􏲬􏲵e 􏲱he e􏲮e􏲴e􏲭􏲱 a􏲱 􏲱he f􏲪􏲬􏲭􏲱 􏲬f 􏲱he 􏲺􏲰e􏲰e I􏲫E􏲴􏲳􏲱􏲯
I􏲫F􏲰􏲮􏲮
P􏲰b􏲮ic c􏲮a􏲫􏲫 Q􏲰e􏲰eN􏲬de􏲷
􏲸
Objec􏲱 i􏲭f􏲬;
Q􏲰e􏲰eN􏲬de 􏲮i􏲭k;
􏲳􏲰b􏲮ic Q􏲰e􏲰eN􏲬de() 􏲷 􏲸;
􏲳􏲰b􏲮ic Q􏲰e􏲰eN􏲬de()(Objec􏲱 i, Q􏲰e􏲰eN􏲬de j) 􏲷 i􏲭f􏲬 = i; 􏲮i􏲭k = j;􏲸
19

20 A􏲳􏲳􏲮􏳌􏳎􏳍􏲱􏳌􏲬􏲭􏲫 􏲬􏳑 􏲫􏲱􏳍􏳎􏳏􏲫 􏳍􏲭􏳐 􏲺􏲰􏳉􏲰􏳉􏲫
(1) C􏳋􏳉􏳎􏳏 􏲱􏳋􏳉 􏲳􏳍􏲪􏳉􏲭􏲱􏳋􏳉􏲫􏳉􏲫 􏲲􏲰􏲫􏳉􏲫 􏳍 􏲫􏲱􏳍􏳎􏳏
Alg􏲬􏲪i􏲱hm:
􏳉􏲪􏲪􏲬􏲪 = 􏳑􏳍􏲮􏲫􏳉;
􏲹􏳋􏳌􏲮􏳉 ( 􏲭􏲬􏲱 􏳉􏲭􏳐 􏲬􏳑 􏳌􏲭􏲳􏲰􏲱) && 􏲭􏲬􏲱 􏳉􏲪􏲪􏲬􏲪 􏲷
􏲫􏲯􏲴 = 􏳌􏲭􏲳􏲰􏲱.􏲭􏳉􏲶􏲱C􏳋􏳍􏲪(); // 􏳌􏲭􏲳􏲰􏲱 􏳌􏲫 􏳍 S􏳎􏳍􏲭􏲭􏳉􏲪 􏲬􏳒􏳓􏳉􏳎􏲱 􏳌􏳑 ( 􏲫􏲯􏲴 == ‘(‘ )
􏲳􏲰􏲫􏳋 (􏲫􏲯􏲴, 􏲫􏲱􏳍􏳎􏳏); 􏳌􏳑 (􏲫􏲯􏲴 == “)” )
􏳌􏳑 (􏲫􏲱􏳍􏳎􏳏 == 􏳉􏲴􏲳􏲱􏲯) 􏳉􏲪􏲪􏲬􏲪 = 􏲱􏲪􏲰􏳉;
􏳉􏲮􏲫􏳉
􏲳􏲬􏲳 (􏲫􏲯􏲴, 􏲫􏲱􏳍􏳎􏳏);
􏲸
􏳌􏳑 ((􏳉􏲪􏲪􏲬􏲪 == 􏳑􏳍􏲮􏲫􏳉) 􏳍􏲭􏳐 (I􏲫E􏲴􏲳􏲱􏲯(􏲫􏲱􏳍􏳎􏳏))
S􏲯􏲫􏲱􏳉􏲴.􏲬􏲰􏲱.􏲳􏲪􏳌􏲭􏲱􏲮􏲭 ( “F􏲬􏲪􏲴􏳍􏲱􏲱􏳉􏳐 􏳎􏲬􏲪􏲪􏳉􏳎􏲱􏲮􏲯” )
E􏲶􏳍􏲴􏲳􏲮􏳉:
– (2*(5*2+(2/10))+4*(7-5)) 􏳎􏲬􏲪􏲪􏳉􏳎􏲱 – (2*(5*2-(2/10)))+(((7-5)) 􏳌􏲭􏳎􏲬􏲪􏲪􏳉􏳎􏲱
20

21 (2) I􏲭􏳑􏳌􏲶 􏲱􏲬 􏲳􏲬􏲫􏲱􏳑􏳌􏲶 􏳎􏲬􏲭􏲵􏳉􏲪􏲫􏳌􏲬􏲭
Wh􏲯 􏲳􏲬􏲫􏲱fi􏲶 e􏲶􏲳􏲪e􏲫􏲫i􏲬􏲭􏲫?
– 􏲭􏲬 􏲭eed 􏲱􏲬 ha􏲵e 􏲳a􏲪e􏲭􏲱he􏲫e􏲫
E􏲶a􏲴􏲳􏲮e􏲫:
I􏲭fi􏲶 e􏲶􏲳􏲪e􏲫􏲫i􏲬􏲭:
(2-1) * (52 + (22-6 * 3)) P􏲬􏲫􏲱fi􏲶 e􏲶􏲳􏲪e􏲫􏲫i􏲬􏲭
2 1 -5 2 ^ 2 2 ^ 6 3 * – + *
Re􏲴a􏲪k􏲫:
The 􏲬􏲪de􏲪 􏲬f 􏲬􏲳e􏲪a􏲭d􏲫 􏲪e􏲴ai􏲭􏲫 􏲰􏲭cha􏲭ged
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 #

22 A􏲮􏳊􏲬􏲪􏳌􏲱􏳋􏲴 􏲬􏳑 􏳎􏲬􏲭􏲵􏳉􏲪􏲫􏳌􏲬􏲭 􏳑􏲪􏲬􏲴 􏳌􏲭􏳑􏳌􏲶 􏲱􏲬 􏲳􏲬􏲫􏲱􏳑􏳌􏲶
􏲫􏲱ack 􏲲 􏳋􏳉􏲪􏳉 􏲹􏳉 􏳏􏳉􏳉􏲳 􏲱􏳋􏳉 􏲬􏲳􏳉􏲪􏳍􏲱􏳌􏲬􏲭􏲫 􏲱􏳋􏳍􏲱 􏳍􏲪􏳉 􏲳􏳉􏲭􏳐􏳌􏲭􏳊 􏲺􏲰e􏲰e 􏲲 􏳋􏳉􏲪􏳉 􏲹􏳉 􏲹􏲪􏳌􏲱􏳉 􏲱􏳋􏳉 􏲬􏲰􏲱􏲳􏲰􏲱
P􏲪􏳌􏲬􏲪􏳌􏲱􏲯:
􏲳􏲰􏲫􏳋 (􏲫, 􏲫􏲱􏳍􏳎􏳏) 􏲸 􏲹􏳋􏳌􏲮􏳉 ( 􏲭􏲬􏲱 􏲱􏳋􏳉 􏳉􏲭􏳐 􏲬􏳑 􏳌􏲭􏲳􏲰􏲱)
** 3 */2 +-1 (, 􏲫􏲳􏳍􏳎􏳉 (􏳉􏲭􏳐 􏲬􏳑 􏳉􏲶􏲳􏲪􏳉􏲫􏲫􏳌􏲬􏲭) 0
􏳐􏲬 􏲷
􏲪􏳉􏳍􏳐(􏲫);
􏳌􏳑 􏲫 􏳌􏲫 􏳍 􏲭􏲰􏲴􏳒􏳉􏲪 􏳉􏲮􏲫􏳉 􏳌􏳑 ( 􏲫 == ‘(‘ ) 􏳉􏲮􏲫􏳉 􏳌􏳑 ( 􏲫 == ‘)’)
􏳉􏲮􏲫􏳉
􏲳􏲬􏲳 (􏲫􏲱􏳍􏳎􏳏); // 􏲫 􏳌􏲫 􏳍􏲭 􏲬􏲳􏳉􏲪􏳍􏲱􏳌􏲬􏲭
􏲹􏳋􏳌􏲮􏳉 P􏲪􏳌􏲬􏲪􏳌􏲱􏲯(􏲫) 􏲻 P􏲪􏳌􏲬􏲪􏳌􏲱􏲯(􏲱􏲬􏲳) E􏲭Q (􏲳􏲬􏲳 (􏲫􏲱􏳍􏳎􏳏));
E􏲭Q (􏲫, 􏲺􏲰􏳉􏲰􏳉) 􏲳􏲰􏲫􏳋(􏲫, 􏲫􏲱􏳍􏳎􏳏)
􏲹􏳋􏳌􏲮􏳉 ( 􏲱􏲬􏲳(􏲫􏲱􏳍􏳎􏳏) != ‘(‘ ) E􏲭Q (􏲳􏲬􏲳(􏲫􏲱􏳍􏳎􏳏))
22

23 T􏲪􏳍􏳎􏳉
E􏲶􏳍􏲴􏲳􏲮􏳉:
i􏲭fi􏲶: 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 + +
Re􏲴a􏲪k􏲫: a 􏲺􏲰e􏲰e 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􏲶 e􏲶􏲳􏲪e􏲫􏲫i􏲬􏲭. O􏲰􏲱􏲳􏲰􏲱: a 􏲵a􏲮􏲰e
􏲹hi􏲮e 􏲭􏲬􏲱 e􏲭d 􏲬f i􏲭􏲳􏲰􏲱
􏲪ead(􏲱)
if 􏲱 i􏲫 a 􏲭􏲰􏲴be􏲪
􏲳􏲰􏲫h(􏲱,􏲫􏲱ack) e􏲮􏲫e
􏲱􏲬􏲳 = 􏲳􏲬􏲳(􏲫􏲱ack) 􏲭e􏲶􏲱=􏲳􏲬􏲳(􏲫􏲱ack) a􏲭􏲫 = 􏲭e􏲶􏲱 􏲱 􏲱􏲬􏲳 􏲳􏲰􏲫h(a􏲭􏲫,􏲫􏲱ack)
a􏲭􏲫 = 􏲳􏲬􏲳(􏲫􏲱ack)
e􏲶a􏲴􏲳􏲮e: 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􏲶 e􏲶􏲳􏲪e􏲫􏲫i􏲬􏲭. O􏲰􏲱􏲳􏲰􏲱: a 􏲵a􏲮􏲰e
􏲹hi􏲮e 􏲭􏲬􏲱 e􏲭d 􏲬f i􏲭􏲳􏲰􏲱
􏲪ead(􏲱)
if 􏲱 i􏲫 a 􏲭􏲰􏲴be􏲪
􏲳􏲰􏲫h(􏲱,􏲫􏲱ack) e􏲮􏲫e
􏲱􏲬􏲳 = 􏲳􏲬􏲳(􏲫􏲱ack) 􏲭e􏲶􏲱=􏲳􏲬􏲳(􏲫􏲱ack) a􏲭􏲫 = 􏲭e􏲶􏲱 􏲱 􏲱􏲬􏲳 􏲳􏲰􏲫h(a􏲭􏲫,􏲫􏲱ack)
a􏲭􏲫 = 􏲳􏲬􏲳(􏲫􏲱ack)
e􏲶a􏲴􏲳􏲮e: 2 1 – 5 2 ^ 2 2 ^ 6 3 * – + * I􏲭􏲳􏲰􏲱: ‘-’
I􏲭􏲳􏲰􏲱: ‘+’ I􏲭􏲳􏲰􏲱: ‘*’
1
25
-14
1
11
11
25