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();
}
}


Sign In
Create Account


Back to top









