how to use the method

Do you have a compilation error or a difficult problem to solve? Ask for help here
Post Reply
maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

how to use the method

Post by maty.tsoraro@gmail.com » Sat Feb 24, 2018 11:32 pm

Hello ,
this is the URL link
http://www.beta.browxy.com#USER_158259
i want to print a list and detemine if it is palindrom
i have 2 methodes printList & isPalindrom,but i don't know hot to excute them in the main
please help
thanks !

maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

Re: how to use the method

Post by maty.tsoraro@gmail.com » Sun Feb 25, 2018 11:04 am

this is the program...

public class Main{
public static void main(String[] args){
printList(5);
}
}

public class IntList{
private IntNode _head;

public IntList(){
_head = null
}
public IntList (IntNode node){
_head = node;
}
public static boolean isPalindrome(){
int len,count;
IntNode p,prev;
//No.1 - find the lists length
for(p = _head, len = 0; p =! null; p= p.getNext() )
len++;
//list with one element is palindrom
if(len == 1)
return true;
//No.2 - find the middle element in the list
//we use two pointers so we won't loose the end of the list.
p = _head;
prev = null ;
count = 0;
while(count < len/2)
{
prev = p;
p = p.getNext();
count++;
}
//now prev points to the last element in the first half
// p points to the middle element
//No.3 - seperate the two halfs
prev.setNext(null);


//No.4 - reverse the list that starts with p
//(we'll write a method that does it)
p = reverseList(p);

//No.5 - run from both sides and look for unmatched nums

IntNode first ,second ;
first = _head;
second = p;
while(first != null)
{
if(first.getValue() != second.getValue())
return first;
first = first.getNext();
second = second.getNext();
}
//if we arrived this line,the list is palindrome
return true;



}
/**
* Reverse the given list,and return its new head.
*/
private static IntNode reverseList(IntNode h){
//current represnts the element to be reversed
//rev is the reversed list's head
IntNode current,rev;
current = h;
rev = null ;
while(h != null)
{
h = h.getNext();
current.setNext(rev);
rev = current ;
current = h;
}
return rev;
}

public void printList(){
IntNode temp = _head;
while(temp != null)
{
System.out.print(temp.getValue() + " --> ");
temp = temp.getNext();
}
System.out.println(" null");
}
}

public class IntNode {
private int _value;
private IntNode _next;

public IntNode(int val , IntNode n){
_value = val;
_next = n;
}

public int getValue(){
return _value;
}
public IntNode getNext(){
return _next;
}
public void setValue(int v){
_value = v;
}
public void setNext(IntNode node){
_next = node;
}
}

maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

Re: how to use the method

Post by maty.tsoraro@gmail.com » Sun Feb 25, 2018 11:06 am

sorry for the spam
this is the URL link
http://www.beta.browxy.com#ALIEN_10615

dbremmen@gmail.com
Posts: 63
Joined: Sun Jan 07, 2018 6:30 pm

Palindrome Exercise

Post by dbremmen@gmail.com » Mon Feb 26, 2018 1:09 am

Hi!

Thanks for the post, please when making a post try to use a meaningful subject like "Palindrome Exercise"
This way more people will be interested in clicking your post and help. Also please enclose your code with
code tags so your code is formatted and looks pretty (Use the </> button in the editor)

I updated your code and now is working at: http://www.beta.browxy.com#ALIEN_10640

Here is the code (look how pretty is formatted when I use the </> button to enclose each file :) )

Main.java

Code: Select all

public class Main{
    public static void main(String[] args){
        IntNode node1 = new IntNode(1,null);
        IntNode node2 = new IntNode(2,node1);
        IntNode node3 = new IntNode(3,node2);
        IntNode node4 = new IntNode(3,node3);
        IntNode node5 = new IntNode(2,node4);
        IntNode node6 = new IntNode(1,node5);
        IntList intList =  new IntList(node6);
        System.out.println(intList.isPalindrome());
    }
}
IntNode.java

Code: Select all

public class IntNode {
    private int _value;
    private IntNode _next;
    
    public IntNode(int val , IntNode n){
        _value = val;
        _next = n;
    }
    
    public int getValue(){
        return _value;
    }
    public IntNode getNext(){
        return _next;
    }
    public void setValue(int v){
        _value = v;
    }
    public void setNext(IntNode node){
        _next = node;
    }
}
IntList.java

Code: Select all

public class IntList{
    private IntNode _head;
    
    public IntList() {
        _head = null;
    }
    public IntList (IntNode node){
        _head = node;
    }
    public boolean isPalindrome(){
        int len,count;
        IntNode p,prev;
        //No.1 - find the lists length
        for(p = _head, len = 0; p != null; p= p.getNext() )
        len++;
        //list with one element is palindrom
        if(len == 1) {
          System.out.println("len = 1");
          return true;
        }
        //No.2 - find the middle element in the list
        //we use two pointers so we won't loose the end of the list.
        p = _head;
        prev = null ;
        count = 0;
        while(count < len/2)
        {
            prev = p;
            p = p.getNext();
            count++;
        }
        //now prev points to the last element in the first half
        // p points to the middle element
        //No.3 - seperate the two halfs
        prev.setNext(null);
        
        
        //No.4 - reverse the list that starts  with p
        //(we'll write a method that does it)
        p = reverseList(p);
        
        //No.5 - run from both sides and look for unmatched nums
        
        IntNode first ,second ;
        first = _head;
        second = p;
        while(first != null)
        {
            System.out.println("checking: " + first.getValue() + " against: " + second.getValue());
            if(first.getValue() != second.getValue())
            return false;
            first = first.getNext();
            second = second.getNext();
        }
        //if we arrived this line,the list is palindrome
        return true;
    
    
    
}
/**
 * Reverse the given list,and return its new head.
 */
 private static IntNode reverseList(IntNode h){
     //current represnts the element to be reversed
     //rev is the reversed list's head
     IntNode current,rev;
     current = h;
     rev = null ;
     while(h != null)
     {
         h = h.getNext();
         current.setNext(rev);
         rev = current ;
         current = h;
     }
         return rev;
     }
 
 public void printList(){
     IntNode temp = _head;
     while(temp != null)
     {
         System.out.print(temp.getValue() + " --> ");
         temp = temp.getNext();
     }
     System.out.println(" null");
 }
}


maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

Re: how to use the method

Post by maty.tsoraro@gmail.com » Mon Feb 26, 2018 4:39 am

hi

thanks alot for your help
i do have 2 more questions :)
1)can i use the method printList to print alist,and if so how exactly?
2)i didnt understand how to use <> buttons to copy my code
can you post maybe a clip in the help subject ?

thanks !!

dbremmen@gmail.com
Posts: 63
Joined: Sun Jan 07, 2018 6:30 pm

Re: how to use the method

Post by dbremmen@gmail.com » Mon Feb 26, 2018 11:55 am

Hi!

a) Yes of course you can use this line in the main:

Code: Select all

intList.printList();
b) I created a post with the guidelines (there is an screen-shot of the code button)
viewtopic.php?f=2&t=19

Thanks!

maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

Re: how to use the method

Post by maty.tsoraro@gmail.com » Mon Feb 26, 2018 2:20 pm

thank you very much :!:

Post Reply