Results 1 to 3 of 3
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Yakg View Post
    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;
     
         }
        }
    You're testing s != null in your second loop; s will never equal null, it may become empty but that's all.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default 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:

    Java 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;
         }
        }
    Thanks

Similar Threads

  1. Help with stacks
    By kMel90 in forum New To Java
    Replies: 3
    Last Post: 12-05-2010, 01:02 AM
  2. types of stacks
    By vendetta in forum New To Java
    Replies: 3
    Last Post: 02-06-2010, 03:58 AM
  3. Help with stacks
    By Srcee in forum New To Java
    Replies: 5
    Last Post: 11-01-2009, 11:23 AM
  4. Stacks
    By Zosden in forum Advanced Java
    Replies: 15
    Last Post: 05-05-2008, 08:16 AM
  5. Using Stacks
    By ravian in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •