Results 1 to 12 of 12
- 09-19-2012, 10:53 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
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. I would be really grateful if someone could create these 3 methods for me, since I would have a great example to use. I have to create the 3 methods in bold. Here's the code I have:
Java 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; } } }Last edited by Enirox; 09-19-2012 at 08:23 PM.
- 09-19-2012, 12:55 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)
Please use [code] tags [/code] when posting code.
Unformatted code is hard to read.
This isn't a code factory (see my sig).
If you have a problem then ask a question, posting relevant code and any errors/exceptions you are having.Please do not ask for code as refusal often offends.
- 09-19-2012, 08:25 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)
- 09-20-2012, 09:46 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)
Pick one and then explain what you think you would need to do to fulfill the requirements for the method.
Please do not ask for code as refusal often offends.
- 09-20-2012, 01:22 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)
Could you just 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 ).
- 09-20-2012, 03:35 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)
That page is giving examples of how to use those methods, not what those methods do (ie the code in them).
You have a singly linked list there.
Each Node knows what the next Node in the list is.
The List itself knows what the first, last and current Nodes are.
Not sure whay exactly it needs to know all that, but hey ho.
All three of those methods need you to find a Node at k.
In order to change the Node at k (either replacing, inserting or removing) you'll need to change the Node at k-1 to point at whatever the new Node is.
That is your basic logic.Please do not ask for code as refusal often offends.
- 09-20-2012, 03:55 PM #7
Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)
If you don't understand my response, don't ignore it, ask a question.
- 09-20-2012, 05:26 PM #8
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
- 09-20-2012, 05:34 PM #9
Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)
I suggest that you work on one method at a time. Trying to do several at the same time can make the project more complicated.
If you don't understand my response, don't ignore it, ask a question.
- 09-20-2012, 06:33 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: List methods add(int k, Data data), set(int k, Data data), remove(int k)
As Norma says, do those three methods you originally asked about.
That should give you the ammunition to use for more complex tasks.Please do not ask for code as refusal often offends.
- 09-20-2012, 07:04 PM #11
- 09-21-2012, 09:33 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Similar Threads
-
Getting problem inserting data in data base
By anupama in forum New To JavaReplies: 4Last Post: 12-15-2010, 10:03 PM -
how to store the data in data base
By eclipse3.4ide in forum New To JavaReplies: 5Last Post: 02-03-2009, 04:25 AM -
error while retrieving data from data base
By kirtesh4u in forum New To JavaReplies: 5Last Post: 11-15-2008, 04:10 PM -
Data Pipeline 2 - Data Transformation Toolkit for Java Released
By dele in forum Java SoftwareReplies: 0Last Post: 10-31-2008, 02:13 PM -
Data Sorting in a .data file using java
By stutiger99 in forum New To JavaReplies: 2Last Post: 10-08-2008, 02:52 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks