• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Stacks and Queues

 
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recently took a quiz on stacks and queues and I got this question wrong:

Write a method public Queue<Stack <String>> toQueueOfStacks(String[][]data)
that converts a two-dimensional array of strings into a queue of stacks. Each column of the 2D array should become a stack with the value from the top row(row=0) at the top of the stack. The stacks coressponding to the collumns should be stored in the same order. This is my attempt of the code:



Can someone show me how to write it correctly? Thanks
 
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In breaking the problem into smaller pieces you have a sub-problem: for a column in a 2D array of Strings create a stack.

Can you work out just that much?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:In breaking the problem into smaller pieces you have a sub-problem: for a column in a 2D array of Strings create a stack.

Can you work out just that much?



 
Carey Brown
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use 'r' or 'row' and 'c' or 'col' for variable names.

Do you need both row and column loops?
 
Carey Brown
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I say a column, I mean a single column. Pick one, any one (e.g. zero).
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:When I say a column, I mean a single column. Pick one, any one (e.g. zero).


Oops sorry,  
Stack<String> a = new Stack<String>();
for(int r=0;i<data.length;i++)
{
 
     a.push(data[r][0]);
  }

}
 
Carey Brown
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Close. 'r' has to count backwards like you did in your original post and 'i' is incorrect.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Close. 'r' has to count backwards like you did in your original post and 'i' is incorrect.



 
Carey Brown
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Next step is to wrap that in a loop that adds your stack to the queue that you will be returning when you are done.

 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Next step is to wrap that in a loop that adds your stack to the queue that you will be returning when you are done.



 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I edited my post after you looked at it. I used 's' for Stack where you used 'a', and the loop goes through the columns and does not test for isEmpty(). I don't recall testing for empty being part of the requirements.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Sorry, I edited my post after you looked at it. I used 's' for Stack where you used 'a', and the loop goes through the columns and does not test for isEmpty(). I don't recall testing for empty being part of the requirements.




Is this correct?
 
Carey Brown
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not correct. At this point 'a' is not defined. Why would we care if 'a' is empty? And, this doesn't resolve our need to loop through the columns.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Not correct. At this point 'a' is not defined. Why would we care if 'a' is empty? And, this doesn't resolve our need to loop through the columns.



 
Marshal
Posts: 76416
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain exactly what you are counting in those two loops. I haven't followed this thre‍ad closely, but I think your loops are written incorrectly. There is a correct way to iterate an array of arrays which works for “jagged” arrays just as well as with “rectangular” arrays.
It is also possible to nest two for‑each loops, which might work too.
 
Carey Brown
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ana: Looks good, though you'll need to replace your hard coded zero on line 7.

Campbell: There's nothing in the problem statement indicating that the rows could possibly be of differing lengths. If that were allowed then there would be some more difficult issues to resolve. If, for instance, a row was one element shorter that should result in the stack for the last column being one element shorter. Ok, that's doable.  But now if you wanted the second column in to be one element shorter how would you handle that?

To properly address this the data should have been organized where each row is a stack with zero or more entries, and not deal with the concept of columns at all.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ana Smith wrote:

Carey Brown wrote:Not correct. At this point 'a' is not defined. Why would we care if 'a' is empty? And, this doesn't resolve our need to loop through the columns.







Does this look good?
 
Sheriff
Posts: 17086
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may not have been taught how but my answer to "Does this looks good?" is "Does it pass all its tests?"
 
Carey Brown
Saloon Keeper
Posts: 9558
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks good to me. The real question is does it make sense to you? Do you understand how each statement fulfills part of the requirements? Do you see why your two loops for row and column needed to be swapped?
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic