The Artima Developer Community
Sponsored Link

Java Answers Forum
need help to practice problem

1 reply on 1 page. Most recent reply: Mar 25, 2003 11:39 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
tiro john

Posts: 6
Nickname: motiero
Registered: Feb, 2003

need help to practice problem Posted: Mar 25, 2003 10:47 AM
Reply to this message Reply
Advertisement
Two stacks, p and q, contain nodes of identical ordered structure ElementType (e.g. long, double, String, etc), with the earliest values in the ordering being at the tops of the stacks. A third stack, r, is available and is initially empty. It is possible to produce the complete ordered set of nodes in stack p, with the earliest value at the top using the following algorithm:

Stack p, q, r
ElementType topp, topq, topr

WHILE (NOT p.empty() AND NOT q.empty()) DO
topp = p.pop()
topq = q.pop()
IF (topp <= topq) THEN
r.push(topp)
q.push(topq)
ELSE
r.push(topq)
p.push(topp)
ENDIF
END

WHILE NOT p.empty() DO
topp = p.pop()
r.push(topp)
END

WHILE NOT q.empty() DO
topq =q.pop()
r.push(topq)
END

WHILE NOT r.empty() DO
topr = r.pop()
p.push(topr)
END

By employing an appropriate STACK ADT and choice of suitable test data, write the above program.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: need help to practice problem Posted: Mar 25, 2003 11:39 AM
Reply to this message Reply
It is already prett much written. You can implement the queues with LinkedLists, fill them up with Random values and run your test. Of course, it would be simpler and more clear to just combine the two queues, then do a sort...

Flat View: This topic has 1 reply on 1 page
Topic: Maths codes Previous Topic   Next Topic Topic: Changing default colors in applet components

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use