Results 1 to 9 of 9
  1. #1
    nomss is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default Java Linked List

    public class MyIntNode
    {
    private int num;
    private MyIntNode next;

    public MyIntNode(int n)
    {
    num = n;
    next = null;
    }

    public MyIntNode(int[] numArray)
    {
    num = numArray[0];
    MyIntNode current = this;
    MyIntNode toAdd = null;
    for(int i=1; i < numArray.length; i++)
    {
    toAdd = new MyIntNode(numArray[i]);
    current.setNext(toAdd);
    current = toAdd;
    }
    current.next = null;
    }

    public int getNum()
    {
    return num;
    }

    public void setNum(int n)
    {
    num = n;
    }

    public MyIntNode getNext()
    {
    return next;
    }

    public void setNext(MyIntNode nextNode)
    {
    next = nextNode;
    }

    public void insertAfter(MyIntNode toInsert)
    {
    MyIntNode oldNext = this.getNext();
    this.setNext(toInsert);
    toInsert.setNext(oldNext);
    }

    public void deleteAfter()
    {
    MyIntNode current = this;
    current.setNext(getNext());
    }

    public int size()
    {
    int count = 0;
    MyIntNode current = this;
    while(current != null)
    {
    current = current.getNext();
    count++;
    }
    return count;
    }

    public int get(int pos)
    {
    MyIntNode current = this;
    int n = 0;
    for(int i=0; i <= pos; i++)
    {
    current = current.getNext();
    n = getNum();
    }
    return n;
    }

    public MyIntNode getLast()
    {
    MyIntNode current = this;
    while(current != null)
    {
    current = current.getNext();
    }
    return current;
    }

    public void add(int n)
    {
    MyIntNode current = getLast();
    MyIntNode nodeX = new MyIntNode(n);
    current.setNext(nodeX);
    }
    /*
    public void add(int n)
    {
    MyIntNode current = this;
    MyIntNode nodeX = new MyIntNode(n);
    current.getLast().setNext(nodeX);
    }
    */
    public void insert(int n, int pos)
    {
    MyIntNode current = this;
    for(int i=0; i < pos; i++)
    {
    current = current.getNext();
    }
    insertAfter(current);
    setNum(n);
    }

    public void remove(int pos) {
    MyIntNode current = this;
    int counter = 0;
    boolean found = false;
    while (current != null || !found) {
    if (counter == pos-1) {

    current.setNext(current.getNext().getNext());
    found = true;
    }
    if(current != null) {

    current = current.getNext();
    }
    counter++;
    }
    }

    // This method will return a string describing the list.
    // It should include the number of elements, and then
    // output the entire list. For Example:
    // A List with 8 integers: 1 2 3 4 5 6 7 8
    public String toString() {
    String description = "A List with " + size() + " integers:";
    String s = "";
    MyIntNode current = this;
    while (current != null) {
    s = s + " " + current.getNum();
    current = current.getNext();
    }
    return description + s;
    }

    public static void main(String[] args)
    {
    int[] list = {1,2,3,4,5,6,7};
    MyIntNode node = new MyIntNode(list);
    System.out.println(node);
    node.add(8);
    System.out.println(node);
    node.insert(9,4);
    System.out.println(node);
    node.remove(6);
    System.out.println(node);
    }
    }

    I don't understand why my add() is not working.

    https://docs.google.com/viewer?a=v&p...MmU0MGFh&hl=en
    Last edited by nomss; 12-09-2010 at 12:00 PM.

  2. #2
    banhbaochay is offline Member
    Join Date
    Apr 2010
    Posts
    32
    Rep Power
    0

    Default

    You should place your codes in <code> tag, it's more readable for everybody

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    And also you should explain what you mean by "not working", otherwise I might simply blame it on chipmunks.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    And also you should explain what you mean by "not working", otherwise I might simply blame it on chipmunks.
    No, its bitrot; it's always bitrot.

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    You can get an ointment for that, you know.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    You can get an ointment for that, you know.
    Yes, but that ointment smells funny and I don't want everybody in the street pointing at me yelling "he's got bitrot! he's got bitrot!".

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    i couldn't find out the reason, but also the insert() is not working properly. only the remove() method seems to produce plausible results, for example

    A List with 7 integers: 1 2 3 4 5 6 7
    remove 6
    A List with 6 integers: 1 2 3 4 5 6
    remove 2
    A List with 5 integers: 1 2 4 5 6

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Tolls View Post
    And also you should explain what you mean by "not working", otherwise I might simply blame it on chipmunks.
    ok, the call node.add(8); produce a java.lang.NullPointerException

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    So node is null.
    Easy!

    And if anyone thinks I'm going to sift through unformatted code to try and figure out why node is null then they have another thing coming.

Similar Threads

  1. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  2. Linked List in java
    By zaydz in forum New To Java
    Replies: 14
    Last Post: 06-28-2010, 12:53 PM
  3. Java double linked list
    By Clown in forum New To Java
    Replies: 1
    Last Post: 05-07-2010, 04:04 PM
  4. Linked list
    By rosh72851 in forum New To Java
    Replies: 1
    Last Post: 02-05-2009, 07:21 AM
  5. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM

Posting Permissions

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