Ⅰ 璁炬爤镄勫瓨鍌ㄧ┖闂翠负S锛1锛歮锛夛纴鍒濆嬬姸镐佷负TOP锛漨+1銆傜粡杩囦竴绯诲垪鍏ユ爤涓庨镙堟搷浣滃悗锛孴OP锛漨
m+1 銆
杩欎釜镙堟槸浠庢爤椤跺悜镙埚簳铡嬬殑锛屼竴鑸骞虫椂鏄浠庢爤搴曞悜镙堥《铡嬶纴杩欓亾棰樼洰涓鏄鍊掕繃𨱒ョ殑銆
鑻TOP铌n镞讹纴鍒欑粰鍑烘孩鍑轰俊鎭锛屼綔鍑洪敊澶勭悊锛堣繘镙埚墠棣栧厛妫镆ユ爤鏄钖﹀凡婊★纴婊″垯婧㈠嚭锛夛绂缃瓯OP=TOP+1锛堟爤鎸囬拡锷1锛屾寚钖戣繘镙埚湴鍧锛夛绂S(TOP)=X锛岀粨𨱒燂纸X涓烘柊杩涙爤镄勫厓绱狅级銆
镓╁𪾢璧勬枡锛
鑻TOP铌0锛屽垯缁椤嚭涓嬫孩淇℃伅锛屼綔鍑洪敊澶勭悊(阃镙埚墠鍏堟镆ユ槸钖﹀凡涓虹┖镙堬纴 绌哄垯涓嬫孩)锛沊=S(TOP)锛岋纸阃镙埚悗镄勫厓绱犺祴缁橷锛夛细TOP=TOP-1锛岀粨𨱒燂纸镙堟寚阍埚噺1锛屾寚钖戞爤椤讹级銆
瀹氢箟stack镄勭亩鍗曚唬镰侊细
stack<int> sta;
鍏ユ爤锛歴ta.push(x);
鍑烘爤锛歴ta.pop();
鍒ゆ柇镙堢殑澶у皬锛 sta.size();
鍒ゆ柇镙堟槸钖︿负绌猴细sta.empty();