Jump to content

List help

- - - - -

  • Please log in to reply
2 replies to this topic

#1
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Hello, I am having trouble inserting data in a singly linked list.The insert method is not correct, but Im not sure why....either displays 1234 3x, 24 3x, or e33 3x.

Also can I get advice for the other methods? (I want to be efficient)

Thanks in advance, this will help me expand my knowledge so I can create doubly linked list I need for a turing machine simulator.

Here is the interface:

public interface MyLinkedList {


    public void insert(Object x);

    public void delete(Object x);

    public Object lookup(Object x);

    public boolean isEmpty();

    public void printList();


}


Here is the node class

public class MyNode {


    public Object data;

    public MyNode next;


}


Here is where help is needed:

public class OwnLinkedList implements MyLinkedList {


    MyNode mn;


    public OwnLinkedList() {

        mn = null;


    }


    public void insert(Object x) {


        if (isEmpty()) {

            mn = new MyNode();

            mn.next = null;

            mn.data = x;

        } else {

            // the problem i am having is here

            // maybe

            newData.data = x;

            newData.next = mn;

            mn = newData;

        }

    }


    public void delete(Object x) {


             

         // use lookup method here

    }


    public Object lookup(Object x) {

        MyNode looker;

        looker = mn;


        while (looker != null) {

            if (looker.data.equals(x)) {

                return looker.data;

            }

        }

        return x + ": Not Found";

    }


    public boolean isEmpty() {

        return mn == null;

    }


    public void printList() {

        MyNode print = mn;

        while (print != null) {

            System.out.println(mn.data);

            print = mn.next;

        }


    }

}


Here is the test class(driver) that just test the insert method:

public class TestSingly {


    public static void main(String [] args){

        OwnLinkedList oll = new OwnLinkedList();

        oll.insert("1234");

        oll.insert("24");

        oll.insert("e33");

        oll.printList();

    }

}



#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
:laugh: Your problem is in the printList() method, I believe:
    public void printList() {

        MyNode print = mn;

        while (print != null) {

            System.out.println(mn.data);

            print = mn.next;

        }
This won't print the results you expect, instead it should result in an infinite loop! Should be this:
    public void printList() {

        MyNode print = mn;

        while (print != null) {

            System.out.println(print.data);

            print = print.next;

        }
I also notice in this else block:
else {

            // the problem i am having is here

            // maybe

            newData.data = x;

            newData.next = mn;

            mn = newData;

        }
That you did not declare nor create a new instance of MyNode for newData, but I assume that you just omitted it when you included the comment:
else {

            // the problem i am having is here

            // maybe

            MyNode newData = new MyNode();

            newData.data = x;

            newData.next = mn;

            mn = newData;

        }

Wow I changed my sig!

#3
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
@ZekeDragon
:lol:Ha, thanks for the look.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users