Re: [商管][資結] 陣列實作問題!

看板Grad-ProbAsk作者 (綜合水果武士)時間13年前 (2010/12/18 10:39), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
※ 引述《christianSK (AG)》之銘言: : ※ 引述《st84514 (綜合水果武士)》之銘言: : : 題目如下: : : Describe how one can use stacks and the push & pop operations to simulate an : : arrays retrieval and storing operations, respectively : : 大概了解題目的意思也有寫出自己的答案可是不知道對不對... : : 有請高手解題!謝謝!! : 不敢說是高手 : 可是有點想法 想看看跟原po想的有什麼差別 XD" : 用兩個stack B C 來implement array A : //assume array A : 1 ~ n ; : retrieval (array A[i]){ : if (top_B < i){ : return -1 ; //A[i]不存在 : } : else { : while ( 1 ){ : if (0 == top_B - i){ : return pop(B); : } : push(C,pop(B)); : } : while (0 != top_C){ : push(B,pop(C)); : } : } : } : stroing (array A, element item){ : push (A,item); : } : 大家有什麼看法嗎? 這是我的寫法概念跟你差不多 宣告: Array a[0....n]; 兩stack s1[top1],s2[top2];//大小為n,top1ˋtop2初始皆為-1 Storing: store(a,item) { push(a,item); for(i=0;i<n;i++) { push(s1,a[i]);//將陣列內容一一置入s1 } } Retrieval: retrieval(a,i)//存取array第i元素值 { if(i>top1) return -1;不存在 else{ while(i!=top1)//找出i在s1所對應位置並以s2作暫存 { push(s2,pop(s1)) } x=pop(s1); /*找到後存於x push(s1,x); 在push回s1*/ while(top2!=-1)//將s2內容一一push回s1 { push(s1,pop(s2)); } return x;//x即為array第i元素值 } } 我想問你存取那,若事先沒把陣列內容放入stack B的話一開始的 top_b不就錯了嗎?另外我也想問你return後下面還會不會做? 謝謝你的回答!感激不盡! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.85.190.200
文章代碼(AID): #1D31xHh9 (Grad-ProbAsk)
文章代碼(AID): #1D31xHh9 (Grad-ProbAsk)