[問題] 兩題計算機的題目(有附我寫的答案)

看板C_and_CPP作者 (..............)時間15年前 (2009/03/25 23:52), 編輯推噓1(103)
留言4則, 3人參與, 最新討論串1/1
這是我門老師出的作業,這兩題我看不懂,麻煩有誰可以交我嗎 (1) write an algorithm segment using while loops to copy the contents of stack S1 to stack S2. Input: Source stack (s1) and destination stack (s2) 1. Allocate memory for a temporary stack (Temp) 2. While s1 is not empty 2.1 push ( Temp, pop(s1) ) End loop 3. While Temp is not empty 3.1 TempValue = pop(Temp) 3.2 push ( s1, TempValue ) 3.3 push ( s2, TempValue ) (2) use while loops to concatenate the contents of queue Q2 and the contents of queue Q1. After the concatenation, the elements of queue Q2 should be at the end of the elements of queue Q1. Queue Q2 should be empty. Input: Source queue (q2) and destination queue (q3) 1. Allocate memory for a temporary queue (Temp) 2. While q2 is not empty 2.1 enqueue ( Temp, dequeue(q2) ) End loop 3. While Temp is not empty 3.1 TempValue = dequeue(Temp) 3.2 enqueue ( q2, TempValue ) 3.3 enqueue ( q3, TempValue ) End loop End 在此放上我的答案,麻煩幫我看看,感謝了 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.170.97.24

03/25 23:56, , 1F
1. 寫個while迴圈複製 stack S1 至 S2
03/25 23:56, 1F

03/25 23:56, , 2F
2. 寫個while迴圈把queue Q2的內容都移到queue Q1的尾端
03/25 23:56, 2F
※ 編輯: acireme 來自: 118.170.97.24 (03/26 00:09)

03/26 08:17, , 3F
3.2不對吧 Queue Q2 should be empty.
03/26 08:17, 3F

03/26 17:36, , 4F
謝謝啦
03/26 17:36, 4F
文章代碼(AID): #19obDE0a (C_and_CPP)