Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-01-2012, 08:58 PM   PM User | #1
Spartan
New to the CF scene

 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Spartan is an unknown quantity at this point
Question Palindrome using stack,queue

Need some help with a little coding problem.
I need to check if it is a Palindrome using a queue and a stack, by implementing a method that tests whether a sequence of numbers is a palindrome, that is, equal to the sequence in reverse.
the code in [...] is My Code, while everything else is unchangeable any help is appreciated.
Code:
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class QueueStackUtil
{
   public static boolean isPalindrome(int[] values)
   {
      Queue<Integer> queue = new LinkedList<Integer>();
      Stack<Integer> stack = new Stack<Integer>();
      for (int s : values)
      {
         // insert s into the queue and stack
	[//*MyCode//ins instack
		s[++queue]=values;
	//ins in queue
		s[++stack]=values;]
      }
      while (queue.size() > 0)
      {
        [MyCode// remove an element from the queue
         	if(!isPaindrome()){
		for(int i=0; i<tail;i++)
		queue[i] = queue[i+1];
		tail--;
		}
         // remove an element from the stack
		if(!isPalindrome()) stack--;
         
         // compare the removed elements
		if(stack==queue)
		!isPalindrome]

      }
      return true;
   }
}
Spartan is offline   Reply With Quote
Old 10-01-2012, 10:31 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Making use of both a queue and a stack, this is the shortest I could come up with:
PHP Code:
import java.util.Deque;
import java.util.LinkedList;

public class 
Palindrome
{
    private 
Deque<Characterstack = new LinkedList<Character>();
    private 
Deque<Characterqueue = new LinkedList<Character>();
    private 
String s;
    public 
Palindrome(String s)
    {
        
this.s;
        for (
Character c s.toCharArray())
        {
            
Character.toLowerCase(c);
            if (
Character.isLetterOrDigit(c))
            {
                
this.stack.addFirst(c);
                
this.queue.addLast(c);
            }
        }
    }
    
    public 
boolean isPalindrome()
    {
        return 
this.stack.equals(this.queue);
    }
    
    public 
String getString()
    {
        return 
this.s;
    }
    
    public static 
void main(String[] argv)
    {
        
Palindrome p = new Palindrome("Madam I'm Adam");
        
System.out.println("'" p.getString() + "' is" + (p.isPalindrome() ? "" " not") + " a palendrome");        
    }

Realistically, if I were to check for a palindrome I would would grab a string from the middle and compare outside moving both backwards and forwards, and skip over any non letter or digit. That would let me drop the iterative count to 1/2 that of the string length if it is a palindrome.

Since your code is clearly homework, you'll need to do some adaptations here. First, you're stuck using the queue and stack classes (which are not recommended). LinkedList is easy since it implements the Deque, so you can actually treat it as both a stack and a queue.
This makes no sense: s[++queue]=values. Values represents the entire int[], and s[x] represents an invalid offset to a numerical data. This syntax: for (int s : values) is java's equivalent to a foreach. So the 's' here represents a single integer value which you need to push onto the lists. To add items to your stack, use the stack.push method. To add to the queue, use the queue.offer or queue.offerLast method.

Now in the while loop, you simply capture the results of queue.poll and stack.pop. If they don't match, you need to return false.
That's a little curious I have to admit, more often than not educational facilities prefer a single point of return and opt for a variable instead of an inline return.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:41 PM.


Advertisement
Log in to turn off these ads.