Results 1 to 3 of 3
Thread: Stuck with stacks
- 01-30-2011, 03:57 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
Stuck with stacks
Hello everyone I'm trying to write a palindrome stack method that will check numbers. Something went wrong with an never ending loop. As for any comments on the method like complexity and more I would be glad to hear.
I'm very new to the stack subject so hope it's more or less all right.
Java Code:public static boolean PalindromeStack (Stack s, int num){ int counter = 0; Stack s2 = new Stack (); while (counter < num/2){ s2.push(s.pop()); counter++;} s.pop(); while (s !=null) if (s.equals (s2)){ return false;} else s.pop(); s2.pop(); return true; } }
Thanks.
- 01-30-2011, 04:06 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 01-30-2011, 08:34 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
I re wrote it but still
now I have a problem comparing the values inside the stacks, for some reason I'm getting true even though the values are different.
why is that? isEqual and isEmpty methods are not allowed for me to use.
Here's the code:
ThanksJava Code:public static boolean PalindromeWithStack (Stack s, int numOfElements){ int counter = 0; int first,second; Stack s2=new Stack (); if (numOfElements ==0) return true; if (numOfElements ==1) return false; while (counter<numOfElements/2){ s2.push(s.pop()); counter++;} //devided stacks. if (numOfElements %2 != 0) s.pop(); //if odd or even. while (counter > 0){ first = s.pop(); second = s2.pop(); if (first != second) //after condition return false; //ends here counter--; } return true; } }
Similar Threads
-
Help with stacks
By kMel90 in forum New To JavaReplies: 3Last Post: 12-05-2010, 01:02 AM -
types of stacks
By vendetta in forum New To JavaReplies: 3Last Post: 02-06-2010, 03:58 AM -
Help with stacks
By Srcee in forum New To JavaReplies: 5Last Post: 11-01-2009, 11:23 AM -
Stacks
By Zosden in forum Advanced JavaReplies: 15Last Post: 05-05-2008, 08:16 AM -
Using Stacks
By ravian in forum New To JavaReplies: 7Last Post: 11-28-2007, 09:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks