List methods add(int k, Data data), set(int k, Data data), remove(int k)
Hello,I've recently started studying Java, and I've encountered some problems with lists. There's pretty much close to none information about this where I am studying at, so I would be really grateful if someone could create these 3 methods for me, since I would have a great example to use later on. I have to create the 3 methods add(int k, Data data), set(int k, Data data), remove(int k) . Here's the code I have : ( although I think the main part needed for these methods is:
private Node<Data> first;
private Node<Data> last;
private Node<Data> current;
Code :
package studijosKTU;
public class ListKTU<Data> implements ListADT<Data> {
private Node<Data> first;
private Node<Data> last;
private Node<Data> current;
private int size;
/**
* Constructs an empty list.
*/
public ListKTU() {
}
public boolean add(Data data) { // adds an element to the end of the list
if(data==null) return false; // doesnt take null objects
if (first == null) {
first = new Node<Data>(data, first);
last = first;
} else {
Node<Data> e1 = new Node(data, null);
last.next = e1;
last = e1;
}
size++;
return true;
}
public boolean add(int k, Data data){ // inserts before k position
if(data==null) return false; // doesnt take null bojects
if (k<0||k>=size)return false; // returns null if k isn't correct.
throw new UnsupportedOperationException
("Have to create this one");
public int size() {
return size;
}
public boolean isEmpty() {
return first == null;
}
public void clear() {
size = 0;
first = null;
last = null;
current = null;
}
public Data get(int k){ // returns the value of k
if (k<0||k>=size)return null;
current=first.findNode(k);
return current.element;
}
public Data set(int k, Data data){ // sets the value of element k
if(data==null) return null;
throw new UnsupportedOperationException
("Have to create this one");
}
public Data getNext(){
if(current==null) return null;
current=current.next;
if(current==null) return null;
return current.element;
}
public Data remove(int k) {
throw new UnsupportedOperationException
("Have to create this one");
}
class Node<Data> { // class that ensures incapsulation
public Data element;
public Node<Data> next;
Node(Data data, Node<Data> next) {
this.element = data;
this.next = next;
}
public Node<Data> findNode(int k){
Node<Data> e=this;
for(int i=0;i<k;i++){
e=e.next;
}
return e;
}
}
}
Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)
To see what a list looks like and how to manipulate it, use a piece of paper and a pencil. Draw a list with some nodes connected by lines representing next pointers and having data.
Then work on the logic of how to do the functions of the three methods you need.
Do them one at a time. If you have problems working out the logic, post some pseudo code for what you think a method should do, and we'll help you get the logic right.
Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)
Quote:
Originally Posted by
Norm
To see what a list looks like and how to manipulate it, use a piece of paper and a pencil. Draw a list with some nodes connected by lines representing next pointers and having data.
Then work on the logic of how to do the functions of the three methods you need.
Do them one at a time. If you have problems working out the logic, post some pseudo code for what you think a method should do, and we'll help you get the logic right.
Could you tell me what should I pick of THIS list to get the examples for the methods I need? None of the "List" or "ArrayList" methods look even close to the code I have, or I'm not looking at the right place. I really cannot spend whole day trying to get one method to work, considering I have 5 more things to work on at the university, so I just wanted to get an example which I could use. For me, it makes more sense to analyze a working method so I can use it in the future, instead of trying to write one without any kind of knowledge about lists in java ( but hey, that's what the professors at the damn university is asking me to do ).
Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)
Post your code that you are having problems with and ask questions about it.
Also posted at: http://www.java-forums.org/new-java/...ove-int-k.html