1 Replies - 16654 Views - Last Post: 24 October 2011 - 09:18 PM Rate Topic: -----

#1 gutchman84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

convert pseudocode to Java code

Posted 24 October 2011 - 09:06 PM

I need help converting this pseudocode into Java code:

public ListNode2 addSortedRecursive (ListNode2 list, int value ) {

if the current node’s value is > value

create a new node and add value into it;

add the new node in front of the current node;

return the new node;

else

return addSortedRecursive ( list.next, value ); //add the value to the linked list after the current node

}

This is my Java code so far:

/* Creation and testing of a generic linked list, ListNode2. */
public class ListNode2 {

    private String data;
    private ListNode2 next;
    private static ListNode2 first; // a class variable that points to the first node in the list
    private static ListNode2 last; // points to the last node in the list
    private static int numberOfNodes = 0;

    public ListNode2() {
        this.data = null;
        this.next = null;
        return;
    }

    // Remove all occurrences of nodes with data equal to key.
    // Return 0 if no such nodes are found;
    // otherwise return the number of nodes found
    public static ListNode2 findAndRemove(ListNode2 list, String key) {

        int foundNodes = 0;
        ListNode2 tempNode = list;
        ListNode2 previous = list;

        for (int i = 0; tempNode != null; tempNode = tempNode.next, i++) {
            if (tempNode.data.equals(key)) { // found
                foundNodes++; // one such node is found
                if (tempNode == previous) { // first node
                    list = tempNode.next; // discard the original first
                } else if (tempNode == last) { //last node
                    last = previous; //discard the original last
                    last.next = null;
                } else { // somewhere in the middle
                    previous.next = tempNode.next; //bypass the current node
                }
                numberOfNodes--;
            } else { // not found in this node
                previous = tempNode; //previous advances only when no node was found and deleted
            }
        } //for
        System.out.println("Found " + foundNodes + " nodes with data being " + key);
        return list;
    }

    public static ListNode2 initializeList() {

        for (int i = 1; i < 5; i++, numberOfNodes++) {
            if (i == 1) { //the first node
                first = new ListNode2();
                last = first;
            } else {
                last.next = new ListNode2();
                last = last.next;
            }
            //last.data = new Integer(i).toString();
            switch (i % 3) {
                case 0:
                    last.data = "Adam";
                    break;
                case 1:
                    last.data = "Eve";
                    break;
                case 2:
                    last.data = "John";
                    break;
                default:
                    last.data = "Peter";
            }
            last.next = null;
        }
        return first;
    }

    // Add value as a new ListNode2 and insert it into the right spot
    // so the data are at ascending order
    /* public ListNode2 addSorted(String name) {
        // case: name should be at front
        if(numberOfNodes == 0) {
            first = new ListNode2();    // ListNode2 constructor
            first.data = name;          // name assigned to first.data
            first.next = null;          // null assigned to first.next
        } else if(first.data.compareTo(name) > 0) {
            // save reference to previous first node
            ListNode2 temp = first;
            // create new first node
            first = new ListNode2();    // ListNode2 constructor
            first.data = name;          // name assigned to first.data
            // update reference to next node
            first.next = temp;
        } else {
            ListNode2 curr = first;
            // loop until the right place is found
            // stop when either there is no next node, or the next node's data is greater than name
            while(curr.next != null && curr.next.data.compareTo(name) < 0)
                curr = curr.next;
            // create new node after curr
            ListNode2 temp = new ListNode2();   // method call
            temp.data = name;                   // assign name to temp.data
            // insert temp between curr and curr.next
            temp.next = curr.next;
            curr.next = temp;
        }
        numberOfNodes++;    // increment numberOfNodes
        return first;       
    } // addSorted() */
    
    public ListNode2 addSortedRecursive (ListNode2 list, int value ) 
    {

     /*if the current node’s value is > value
        if the current node’s value is > value
            create a new node and add value into it;

            add the new node in front of the current node;

            return the new node;*/
      
      if ( Listnode2.list  > value )
      {
            first = new ListNode2();    // ListNode2 constructor
            first.data = name;
      }
      

     else

           return addSortedRecursive ( list.next, value ); //add the value to the linked list after the current node*/

    }

    public void displayAllNodesRecur (int nodeNumber) 
    {

        ListNode2 tempNode = this;


        System.out.println("Node " + nodeNumber + ": " + tempNode.data);

        if (tempNode.next != null)

            tempNode.next.displayAllNodesRecur(nodeNumber+1);

    }

    public static void main(String args[]) {

        ListNode2 list = new ListNode2();
        System.out.println("Creating a linked list using the addSorted( ) method ...");
        list = list.addSortedRecursive(list, 5);
        list = list.addSortedRecursive("Eve");
        list = list.addSortedRecursive("April");
        list = list.addSortedRecursive("Don");
        list = list.addSortedRecursive("Syd");
        list = list.addSortedRecursive("Mary");
        list = list.addSortedRecursive("Peter");
        list = list.addSortedRecursive("April");
        list.displayAllNodesRecur();
        findAndRemove(list, "April");
        System.out.println("After removing \"April\" ...");
        list.displayAllNodesRecur();
    } //main
} // class: ListNode2



The output for this program must be as follows (see attachment):

These are the errors I get:
E:\UHCL Fall 2011\ListNode2\src\ListNode2.java:118: cannot find symbol
symbol : variable Listnode2
location: class ListNode2
if ( Listnode2.list > value )
E:\UHCL Fall 2011\ListNode2\src\ListNode2.java:121: cannot find symbol
symbol : variable name
location: class ListNode2
first.data = name;
E:\UHCL Fall 2011\ListNode2\src\ListNode2.java:150: addSortedRecursive(ListNode2,int) in ListNode2 cannot be applied to (java.lang.String)
list = list.addSortedRecursive("Eve");
E:\UHCL Fall 2011\ListNode2\src\ListNode2.java:151: addSortedRecursive(ListNode2,int) in ListNode2 cannot be applied to (java.lang.String)
list = list.addSortedRecursive("April");
E:\UHCL Fall 2011\ListNode2\src\ListNode2.java:152: addSortedRecursive(ListNode2,int) in ListNode2 cannot be applied to (java.lang.String)
list = list.addSortedRecursive("Don");
E:\UHCL Fall 2011\ListNode2\src\ListNode2.java:153: addSortedRecursive(ListNode2,int) in ListNode2 cannot be applied to (java.lang.String)
list = list.addSortedRecursive("Syd");
E:\UHCL Fall 2011\ListNode2\src\ListNode2.java:154: addSortedRecursive(ListNode2,int) in ListNode2 cannot be applied to (java.lang.String)
list = list.addSortedRecursive("Mary");
E:\UHCL Fall 2011\ListNode2\src\ListNode2.java:155: addSortedRecursive(ListNode2,int) in ListNode2 cannot be applied to (java.lang.String)
list = list.addSortedRecursive("Peter");
E:\UHCL Fall 2011\ListNode2\src\ListNode2.java:156: addSortedRecursive(ListNode2,int) in ListNode2 cannot be applied to (java.lang.String)
list = list.addSortedRecursive("April");
E:\UHCL Fall 2011\ListNode2\src\ListNode2.java:157: displayAllNodesRecur(int) in ListNode2 cannot be applied to ()
list.displayAllNodesRecur();
E:\UHCL Fall 2011\ListNode2\src\ListNode2.java:160: displayAllNodesRecur(int) in ListNode2 cannot be applied to ()
list.displayAllNodesRecur();
11 errors
E:\UHCL Fall 2011\ListNode2\nbproject\build-impl.xml:603: The following error occurred while executing this line:
E:\UHCL Fall 2011\ListNode2\nbproject\build-impl.xml:284: Compile failed; see the compiler error output for details.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: convert pseudocode to Java code

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: convert pseudocode to Java code

Posted 24 October 2011 - 09:18 PM

You need two classes

one for the Node, containg the String data and a forward and a backward Node
one for the ListNode which as the number of nodes the first (no need for the last)

none of these variables should be static in case you want to create multiple List object
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1